4

I wanted to create a custom table with latex but I have an issue concerning the size of my table which not ends at the end of the width of the document. I don't understand why because 5x0.2\textwidth = 1 \textwidth.

\documentclass{article}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{tabularx} % in the preamble
\usepackage{multirow}

\usepackage{geometry}
 \geometry{
 a4paper,
 total={170mm,257mm},
 left=10mm,
 right= 10mm,
 top=10mm,
 }

\begin{document}

\noindent\begin{tabularx}{\textwidth} { 
  | >{\centering\arraybackslash}m{.2\textwidth}
  | >{\centering\arraybackslash}m{.2\textwidth}
  | >{\centering\arraybackslash}m{.2\textwidth}
  | >{\centering\arraybackslash}m{.2\textwidth}
  | >{\centering\arraybackslash}m{.2\textwidth} | 
  }
\hline
\multirow{5}{*}{logo} & \multicolumn{3}{c|}{C A}                  & M     \\ \cline{2-5} 
                      & \multicolumn{3}{c|}{Titre}                          & R    \\ \cline{2-5} 
                      & a & b & c & B   \\ \cline{2-5} 
                      & d & e & f & Autres \\ \cline{2-5} 
                      & h & i & j & DATE   \\ \hline
\end{tabularx}


\end{document}

How can I solve this issue ?

3
  • Does the table slightly stick out into the margin? Maybe the cell size doesn't factor in the seps on the left and right. Commented Jan 25, 2023 at 14:59
  • 1
    Just change m{.2\textwidth} with X (there is no sense in using tabularx without automatically-adjustable columns marked by X). You also have a warning from geometry you should consider
    – Rmano
    Commented Jan 25, 2023 at 15:01
  • 3
    "5x0.2\textwidth = 1 \textwidth" -> this does not take into account the spacing before/after and in between columns Commented Jan 25, 2023 at 15:02

5 Answers 5

5
  • tabularx table should has at least one column of X or from it derived type
  • if some columns should have different width, you can define ratios between them or write some columns as p{<width>} or m{<width>}
  • in your case I would rather use tabularray package instead of tabularx since
    • code is shorter,
    • cells' contents is better vertical spaced (centered)
    • if needed, it is simple change ratio between columns width

Example, when all columns has the same width:

\documentclass{article}
\usepackage{geometry}
\geometry{a4paper,
          total={170mm,257mm},
          hmargin=10mm,
          top=10mm,
          }
\usepackage{tabularray}

\begin{document}

\noindent\begin{tblr}{hlines, vlines,
                      colspec = {*{5}{X[c]}}
                      }
\SetCell[r=5]{c}    logo
    &   \SetCell[c=3]{c} C A                 
        &   &   &   M               \\  
    &   \SetCell[c=3]{c} Titre
        &   &   &   R               \\
    & a & b & c &   B               \\
    & d & e & f &   Autres          \\
    & h & i & j &   DATE            \\ 
\end{tblr}
\end{document}

enter image description here

and a case that cells has different widths:

\documentclass{article}
\usepackage{geometry}
\geometry{a4paper,
          total={170mm,257mm},
          hmargin=10mm,
          top=10mm,
          }
\usepackage{tabularray}

\begin{document}

\noindent\begin{tblr}{hlines, vlines,
                      colspec = {X[0.8, c] X[c] X[2, c] X[c] X[1.2, c]}
                      }
\SetCell[r=5]{c}    logo
    &   \SetCell[c=3]{c} C A                 
        &   &   &   M               \\  
    &   \SetCell[c=3]{c} Titre
        &   &   &   R               \\
    & a & b & c &   B               \\
    & d & e & f &   Autres          \\
    & h & i & j &   DATE            \\ 
\end{tblr}
\end{document}

enter image description here

1
  • Yes, I agree: the new tabular packages (tabularray and nicematrix especially) are the way to go for "fancy" tabular structures.
    – Rmano
    Commented Jan 25, 2023 at 17:06
6

I have a working (not perfect) code that done this output:

enter image description here

The code:

\documentclass{article}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{tabularx} % in the preamble
\usepackage{multirow}

\usepackage{geometry}
\geometry{
    a4paper,
    total={170mm,257mm},
    left=10mm,
    right= 10mm,
    top=10mm,
}

\begin{document}
    
    \noindent\begin{tabularx}{\textwidth} { 
            | >{\centering\arraybackslash}m{.18\textwidth}
            | >{\centering\arraybackslash}m{.18\textwidth}
            | >{\centering\arraybackslash}m{.18\textwidth}
            | >{\centering\arraybackslash}m{.18\textwidth}
            | >{\centering\arraybackslash}m{.18\textwidth} | 
        }
        \cline{1-5}
        \multirow{5}{*}{logo} & \multicolumn{3}{c|}{C A}      & M     \\ \cline{2-5} 
        & \multicolumn{3}{c|}{Titre}                          & R    \\ \cline{2-5} 
        & a & b & c & B   \\ \cline{2-5} 
        & d & e & f & Autres \\ \cline{2-5} 
        & h & i & j & DATE   \\ \cline{1-5}
    \end{tabularx}
        
\end{document}
1
  • 2
    tabularx table should has at least one column of X or from it derived type.
    – Zarko
    Commented Jan 25, 2023 at 15:09
5

This works:

\documentclass{article}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{tabularx} % in the preamble
\usepackage{multirow}

\usepackage{geometry}
 \geometry{
 a4paper,
 %total={170mm,257mm},
 left=10mm,
 right= 10mm,
 top=10mm,
 }

\begin{document}

\noindent\begin{tabularx}{\textwidth} { 
  *{5}{| >{\centering\arraybackslash}X}| 
  }
\hline
\multirow{5}{*}{logo} & \multicolumn{3}{c|}{C A}                  & M     \\ \cline{2-5} 
                      & \multicolumn{3}{c|}{Titre}                          & R    \\ \cline{2-5} 
                      & a & b & c & B   \\ \cline{2-5} 
                      & d & e & f & Autres \\ \cline{2-5} 
                      & h & i & j & DATE   \\ \hline
\end{tabularx}
\end{document}

The X column will stretch to fill the available space, which must take into account the vertical lines and the intercolumn space too. It's the only reason for the existence of the tabularx package... otherwise you can just go with a normal tabular* environment.

You can also use multipliers to avoid re-type the same thing 5 times.

I also removed one the total specifier in geometry: if you give paper size, left and right margin, you have fully specified the dimensions...

enter image description here

4
  • Your approach works perfectly but if I want to specify the size of each column how can I do ?
    – Mo0nKizz
    Commented Jan 25, 2023 at 15:07
  • 2
    You must choose a size that fits, taking into account the intercolumn space and the width of the vertical rules. See, for example Raffaele's answer. If it doesn't fit it doesn't fit... 😉
    – Rmano
    Commented Jan 25, 2023 at 15:09
  • My solution fit. But Rmano solution is more elegant. Commented Jan 25, 2023 at 15:14
  • 1
    @Mo0nKizz If you want to specify the width of all columns, maybe a normal tabular would be better suited than a tabularx? Commented Jan 25, 2023 at 15:15
4

tabularx provides X-type column, which adapts its width to the table. I would additionally suggest to define a custom column e.g. Y to avoid unnecessary repetition

\newcolumntype{Y}{>{\centering\arraybackslash}X}

The code:

\documentclass{article}
% \usepackage{graphicx}
% \usepackage[export]{adjustbox}
\usepackage{tabularx} % in the preamble
\usepackage{multirow}
\usepackage{geometry}
 \geometry{
     a4paper,
     % total={170mm,257mm},
     left=10mm,
     right= 10mm,
     top=10mm,
}
\newcolumntype{Y}{>{\centering\arraybackslash}X}


\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|*5{Y|}}
    \hline
    \multirow{5}{*}{logo} & \multicolumn{3}{c|}{C A}   & M \\ \cline{2-5} 
                          & \multicolumn{3}{c|}{Titre} & R \\ \cline{2-5} 
                          & a & b & c                  & B \\ \cline{2-5} 
                          & d & e & f                  & Autres \\ \cline{2-5} 
                          & h & i & j                  & DATE   \\ \hline
\end{tabularx}
\end{document}

and screenshot:

enter image description here

2

You don't seem to really want a tabularx environment. In particular, you don't seem to require automatic line breaking in any cells. Instead, it looks like you "just" want to create a table with five equal-width columns.

If this interpretation is correct, I suggest you (a) employ a tabular environment instead of tabularx environment and (b) load the array package and employ its w column type, which lets you specify the column's desired usable width. (In a LaTeX table, the column's total width is generally the sum of (a) the usable width and (b) any whitespace padding on either side, given by the parameter \tabcolsep.)

The following table has fives columns with usable widths of 2cm.

enter image description here

\documentclass{article}
\usepackage{array}    % for 'w' column type
\usepackage{multirow}
\usepackage{geometry}
\geometry{a4paper, total={170mm,257mm}, top=10mm}

\begin{document}

\begin{center}
\setlength\extrarowheight{2pt} % for a less cramped "look"
\begin{tabular}{ | *{5}{w{c}{2cm}|} }
\hline
\multirow{5}{*}{logo} 
   & \multicolumn{3}{c|}{C A}   & M \\ \cline{2-5} 
   & \multicolumn{3}{c|}{Titre} & R \\ \cline{2-5} 
   & a & b & c                  & B \\ \cline{2-5} 
   & d & e & f                  & Autres \\ \cline{2-5} 
   & h & i & j                  & DATE   \\ \hline
\end{tabular}
\end{center}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .