Tableaux (cellule barrée, multilignes, ...)

Tout ce qui concerne le langage TeX/LaTeX et ses variantes. Ce langage est utilisable sur le forum via les balises tex.
[participation réservée aux utilisateurs inscrits]
Règles du forum
Merci de soigner la rédaction de vos messages et de consulter ce sujet avant de poster. Pensez également à utiliser la fonction recherche du forum.
ezize
Utilisateur éprouvé
Utilisateur éprouvé
Messages : 107
Inscription : samedi 05 août 2006, 13:01

[Résolu] Tableaux (cellule barrée, multilignes, ...)

Message non lu par ezize »

Bonjour,

Voici mon code :

Code : Tout sélectionner

\documentclass[A4paper,french]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[francais]{babel}
\usepackage{array,multirow}
\usepackage{diagbox}

\begin{document}
\renewcommand{\arraystretch}{2} %définir la hauteur des lignes
%\begin{tabular}{|c|>{\centering}m{1.5cm}|>{\centering}m{1.5cm}|}
\begin{tabular}{|c|m{1.5cm}|m{1.5cm}|}
\hline 
\backslashbox{Lignes}{Variable}  & \begin{center}$x$\end{center}  \\ \hline 
\textbf{1} & 5  \\ \hline 
\textbf{2} & 10 \\ \hline 
\textbf{3} & 7 \\ \hline 
\end{tabular}
\renewcommand{\arraystretch}{1} %redéfinir la hauteur des lignes

\vspace*{1cm} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{tabular}{|>{\centering}m{1.1cm}|>{\centering}m{1.9cm}|}
\hline 
\textit{Lignes} & \textit{Variables}\tabularnewline
\hline 
\textbf{1} & $a$\tabularnewline
\hline 
\textbf{2} & barrée \tabularnewline
\hline 
\multirow{2}{1.1cm}{\textbf{3}} & $b$\tabularnewline
\cline{2-2} 
 & $c$\tabularnewline
\hline 
\textbf{4} & barrée \tabularnewline
\hline 
\end{tabular}

\end{document}
Et le résultat :
Image
J'ai plusieurs questions :

1- Comment définir la largeur de la première colonne du premier tableau sans déformer la diagonale ?
2- Comment centrer la deuxième colonne de ce premier tableau ?
3- Comment centrer la cellule "3" (tableau 2, colonne 1) ?
4- Comment tracer deux diagonales pour barrer les cellules "barrée (tableau 2, colonne2) ?

Merci bien pour toute aide !
Arathorn
Utilisateur éprouvé
Utilisateur éprouvé
Messages : 284
Inscription : mercredi 24 avril 2013, 19:47
Statut actuel : Enseignant

Re: Tableaux (cellule barrée, multilignes, ...)

Message non lu par Arathorn »

Bonsoir,
Pour 1, 2 et 3, je propose ceci

Code : Tout sélectionner

\documentclass[a4paper,french]{article}
\usepackage[T1]{fontenc}
\usepackage{multirow}
\usepackage{diagbox}
\usepackage{babel}
\begin{document}
%\renewcommand{\arraystretch}{2} %définir la hauteur des lignes
%\begin{tabular}{|c|>{\centering}m{1.5cm}|>{\centering}m{1.5cm}|}
\begin{tabular}{|c|>{\centering\arraybackslash} m{1.5cm}|}
\hline 
\diagbox[width=8cm]{Lignes}{Variable}  & $x$  \\ \hline 
\textbf{1} & 5  \\ \hline 
\textbf{2} & 10 \\ \hline 
\textbf{3} & 7 \\ \hline 
\end{tabular}
\renewcommand{\arraystretch}{1} %redéfinir la hauteur des lignes
	
\vspace*{1cm} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
\begin{tabular}{|>{\centering}m{1.1cm}|>{\centering}m{1.9cm}|}
\hline 
\textit{Lignes} & \textit{Variables}\tabularnewline
\hline 
\textbf{1} & $a$\tabularnewline
\hline 
\textbf{2} & barrée \tabularnewline
\hline 
\multirow{2}*{\textbf{3}} & $b$\tabularnewline
\cline{2-2} 
& $c$\tabularnewline
\hline 
\textbf{4} & barrée \tabularnewline
\hline 
\end{tabular}

\end{document}
Et pour le 4 https://tex.stackexchange.com/questions ... in-tabular que je n'ai pas compris pour adapter.
Arathorn
Utilisateur éprouvé
Utilisateur éprouvé
Messages : 284
Inscription : mercredi 24 avril 2013, 19:47
Statut actuel : Enseignant

Re: Tableaux (cellule barrée, multilignes, ...)

Message non lu par Arathorn »

Pour le 4, j'ai adapté comme ceci

Code : Tout sélectionner

\documentclass[a4paper,french]{article}
\usepackage[T1]{fontenc}
\usepackage{multirow}
\usepackage{diagbox}
\usepackage{babel}
\begin{document}
%	\renewcommand{\arraystretch}{2} %définir la hauteur des lignes
%\begin{tabular}{|c|>{\centering}m{1.5cm}|>{\centering}m{1.5cm}|}
\begin{tabular}{|c|>{\centering\arraybackslash} m{1.5cm}|}
\hline 
\diagbox[width=8cm]{Lignes}{Variable}  & $x$  \\ \hline 
\textbf{1} & 5  \\ \hline 
\textbf{2} & 10 \\ \hline 
\textbf{3} & 7 \\ \hline 
\end{tabular}
\renewcommand{\arraystretch}{1} %redéfinir la hauteur des lignes
	
\vspace*{1cm} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
\begin{tabular}{|>{\centering}m{1.1cm}|>{\centering}m{1.9cm}|}
\hline 
\textit{Lignes} & \textit{Variables}\tabularnewline
\hline 
\textbf{1} & $a$\tabularnewline
\hline 
\multicolumn{2}{r}{\diagbox[innerwidth=\dimexpr1.9cm+\arrayrulewidth, height=\dimexpr\line+0.5 \arrayrulewidth , dir=SW]{}{}} \tabularnewline[-2.8ex]
\multicolumn{2}{r}{\diagbox[innerwidth=\dimexpr1.9cm+\arrayrulewidth, height=\dimexpr\line+0.5 \arrayrulewidth , dir=NW]{}{}} \tabularnewline[-2.8ex]	
\textbf{2} & barrée \tabularnewline
\hline 
\multirow{2}*{\textbf{3}} & $b$\tabularnewline
\cline{2-2} 
& $c$\tabularnewline
\hline 
\multicolumn{2}{r}{\diagbox[innerwidth=\dimexpr1.9cm+\arrayrulewidth, height=\dimexpr\line+0.5 \arrayrulewidth , dir=SW]{}{}} \tabularnewline[-2.8ex]
\multicolumn{2}{r}{\diagbox[innerwidth=\dimexpr1.9cm+\arrayrulewidth, height=\dimexpr\line+0.5 \arrayrulewidth , dir=NW]{}{}} \tabularnewline[-2.8ex]	
\textbf{4} & barrée \tabularnewline
\hline 
\end{tabular}
\end{document}
Le [-2.8ex] est trouvé après essais/erreurs mais j'ignore comment trouver automatiquement la hauteur d'une ligne.
J'ai l'impression que la ligne 4 est plus haute que la 2.
MB
Administrateur
Administrateur
Messages : 8058
Inscription : samedi 28 mai 2005, 14:23
Statut actuel : Enseignant
Contact :

Re: Tableaux (cellule barrée, multilignes, ...)

Message non lu par MB »

Bonjour, voici une proposition basée sur tikz et nicematrix (que j'en profite pour expérimenter).

Code : Tout sélectionner

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}

\begin{NiceTabular}{wc{3cm}c}[hvlines,cell-space-limits=5pt]
\Block{2-1}{\diagbox{Lignes}{Variable}} & \Block{2-1}{$x$} \\
& \\
\textbf{1} & 5 \\
\textbf{2} & 10 \\
\textbf{3} & 7 \\
\end{NiceTabular}

\bigskip

\begin{NiceTabular}{wc{3cm}wc{3cm}}[hvlines,cell-space-limits=5pt]
\textit{Lignes} & \textit{Variables} \\
\textbf{1} & $a$ \\
\textbf{2} & \\
\Block{2-1}{\textbf{3}} & $b$ \\
 & $c$ \\
\textbf{4} & \\
\CodeAfter
\tikz \draw [red] (3-|2) -- (4-|3) ;
\tikz \draw [red] (4-|2) -- (3-|3) ;
\tikz \draw [red] (6-|2) -- (7-|3) ;
\tikz \draw [red] (7-|2) -- (6-|3) ;
\end{NiceTabular}

\end{document}
Et voici le résultat obtenu.
nicematrix.png
MB. (rejoignez pCloud et bénéficiez de 10Go de stockage en ligne gratuits)
Pas d'aide en message privé. Merci de consulter ce sujet avant de poster votre premier message.
ezize
Utilisateur éprouvé
Utilisateur éprouvé
Messages : 107
Inscription : samedi 05 août 2006, 13:01

Re: Tableaux (cellule barrée, multilignes, ...)

Message non lu par ezize »

Merci infiniment pour votre aide !
F. Pantigny
Utilisateur confirmé
Utilisateur confirmé
Messages : 19
Inscription : dimanche 07 juillet 2019, 14:37

Re: Tableaux (cellule barrée, multilignes, ...)

Message non lu par F. Pantigny »

D'autres très bonnes réponses à la question ont été proposées en utilisant nicematrix. Je propose ici de montrer que l'on peut, avec nicematrix, programmer une commande \crossbox qui, quand elle sera utilisée dans une cellule, ajoutera une croix dans cette cellule. Pour un résultat parfait dans le cas où on demande une croix rouge (par exemple), les traits seront tracés avant le filets noirs du tableau. Pour cela, on utilise la liste de tokens \g_nicematrix_code_before_tl. Ce paramètre est interne mais public (du fait que son nom commence par \g_nicematrix et non \g__nicematrix).

Code : Tout sélectionner

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{nicematrix}
\usepackage{tikz}

\ExplSyntaxOn
\cs_new_protected:Nn \__pantigny_crossbox:nnn
  {
    \tikz \draw [ #3 ]
          ( #1 -| \int_eval:n { #2 + 1 } ) -- ( \int_eval:n { #1 + 1 } -| #2 )
          ( #1 -| #2 ) -- ( \int_eval:n { #1 + 1 } -| \int_eval:n { #2 + 1 } ) ;
  }


\NewDocumentCommand \crossbox { ! O { } }
  {
    \tl_gput_right:Nx \g_nicematrix_code_before_tl
      {
        \__pantigny_crossbox:nnn
          { \int_use:c { c@iRow } }
          { \int_use:c { c@jCol } }
          { \exp_not:n { #1 } }
      }
  }
\ExplSyntaxOff


\begin{document}

\begin{NiceTabular}{>{\bfseries}wc{3cm}wc{3cm}}[hvlines,cell-space-limits=5pt]
\RowStyle{\normalfont\itshape}
Lignes         & Variables      \\
1              & $a$            \\
2              & \crossbox[red] \\
\Block{2-1}{3} & $b$            \\
               & $c$            \\
4              & \crossbox[red] \\
\end{NiceTabular}

\end{document}
Pièces jointes
essai.png
Répondre
  • Sujets similaires
    Réponses
    Vues
    Dernier message