1

I want to create a table with 100% width of the page so i'm using the package tabularx. I need to set the width of the columns, so I decided to separate the table into 20 separate columns. This allows me to assign a number of columns for a single cell using a multicolumn. I found an example and I adapted it to my case.

For example when I've :

\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax

There are 3 numbers in this :

  • 7 hsize : 7 columns
  • 12 tabcolsep : there are 12 tabcolsep inside the new multicolumn
  • 6 arrayrulewidth : there are 6 arrayrulewidth inside the new multicolumn

We can simplify this :

If we have we want a multicolumn{N}, we'll have N hsize, (N-1)*2 tabcolsep and N-1 arrayrulewidth

For the following example, it works :

\documentclass{article}

    % Français
    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc}
    \usepackage[francais]{babel}

    % Images
    \usepackage{graphicx}
    \usepackage{float}

    % Tables
    \usepackage{array}
    \usepackage{tabularx}
    \usepackage{ragged2e}
    \usepackage{multirow}

    % Custom Header
    \usepackage{fancyhdr}
    \usepackage[headheight=55pt]{geometry}
    \renewcommand{\headrulewidth}{1pt}
\fancypagestyle{plain}{%
 \fancyhead[L]{
      \begin{tabular}[b]{l}
        \textbf{BLABLABLA}  \tabularnewline
        BLABLABLA \tabularnewline
        BLABLABLA \tabularnewline
    \end{tabular}   
  }%
  \fancyhead[R]{
      \begin{tabular}[t]{c}
        \centering\includegraphics[width=2.2cm]{example-image}%
      \end{tabular}
  }%
}
\pagestyle{plain}


\usepackage{textcomp}   


\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|X|X|X|X|X|X|X|X|X|X|X|X|X|X|X|X|X|X|X|X|}
\hline
1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18&19&20 \\ \hline

\multicolumn{12}{|>{\bfseries\centering\hsize=\dimexpr12\hsize+22\tabcolsep+11\arrayrulewidth\relax}X|}{TEST}
&
\multicolumn{8}{>{\bfseries\centering\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{TEST} \\ \hline

\textbf{N°}
&
\multicolumn{7}{>{\bfseries\centering\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Tests}
&
\multicolumn{7}{>{\bfseries\centering\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Res}
&
\multicolumn{3}{>{\bfseries\centering\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth\relax}X|}{Modif}
&
\multicolumn{2}{>{\bfseries\centering\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}{OK} \\ \hline



1
&
\multicolumn{7}{>{\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum}
&
\multicolumn{7}{>{\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum}
&
\multicolumn{3}{>{\centering\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth\relax}X|}{Lorem ipsum}
&
\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}{OK} \\ \hline

2
&
\multicolumn{7}{>{\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum}
&
\multicolumn{7}{>{\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum}
&
\multicolumn{3}{>{\centering\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth\relax}X|}{Lorem ipsum}
&
\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}{OK} \\ \hline



\end{tabularx}

\end{document}

Tab is working

But now, if we try to put another similar row like that :

3
&
\multicolumn{7}{>{\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum}
&
\multicolumn{7}{>{\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum}
&
\multicolumn{3}{>{\centering\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth\relax}X|}{Lorem ipsum}
&
\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}{OK} \\ \hline

That gives me :

Tab is not working

EDIT : You can now just copy paste my document code.

If you find a solution, please put other columns to check if it's working great.

If you're asking why 20 columns, it's because I want to assign a number of columns for a certain cell. (Like 12/20 of the page width or 17/20 of the page width)

The first row with columns (from 1 to 20), it's just to check if columns are not bigger than normal size for nothing. (debug)

EDIT-2 :

I agree to use tabular rather than tabularx but how to do this :

  • Table full width of the page
  • Cells must accept multiple lines
  • The number inside cells correspond to the ratio of the cell to the table

Cells ratio to the table

(The first cell of the last row is a multirow 2)

In that case, how to do this with tabular and not tabularx ?

1
  • please make it easier for people to help by posting complete aritcle class documents that show the problem, not just fragments Commented Nov 9, 2018 at 0:55

2 Answers 2

3

If I add the 4th row I get

enter image description here

\documentclass{article}

\DeclareUnicodeCharacter{00B0}{??}
\usepackage{tabularx}

\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|X|X|X|X|X|X|X|X|X|X|X|X|X|X|X|X|X|X|X|X|}
\hline
1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18&19&20 \\ \hline
\textbf{N°}
&
\multicolumn{7}{>{\bfseries\centering\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Tests}
&
\multicolumn{7}{>{\bfseries\centering\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Res}
&
\multicolumn{3}{>{\bfseries\centering\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth\relax}X|}{Modif}
&
\multicolumn{2}{>{\bfseries\centering\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}{OK} \\ \hline


1
&
\multicolumn{7}{>{\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum}
&
\multicolumn{7}{>{\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum}
&
\multicolumn{3}{>{\centering\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth\relax}X|}{Lorem ipsum}
&
\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}{OK} \\ \hline


2
&
\multicolumn{7}{>{\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum}
&
\multicolumn{7}{>{\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum}
&
\multicolumn{3}{>{\centering\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth\relax}X|}{Lorem ipsum}
&
\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}{OK} \\ \hline

3
&
\multicolumn{7}{>{\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum}
&
\multicolumn{7}{>{\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum}
&
\multicolumn{3}{>{\centering\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth\relax}X|}{Lorem ipsum}
&
\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}{OK} \\ \hline

4
&
\multicolumn{7}{>{\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum}
&
\multicolumn{7}{>{\hsize=\dimexpr7\hsize+12\tabcolsep+6\arrayrulewidth\relax}X|}{Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum}
&
\multicolumn{3}{>{\centering\hsize=\dimexpr3\hsize+4\tabcolsep+2\arrayrulewidth\relax}X|}{Lorem ipsum}
&
\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}{OK} \\ \hline
\end{tabularx}
\end{document}

But do you really need 20 columns? if the 1-20 is just for debugging rather than data it would seem that you only really have 5 columns.

7
  • Thanks but that's not exactly what I was looking for, I updated my used packages to correctly explain my problem. I completely updated my post.
    – Nulji
    Commented Nov 9, 2018 at 9:41
  • @Nulji that's why questions should always have a test document:-) OK I get same now. Looks odd, I'll try to debug later (I would never use tabularx for this anyway, even though I wrote it, but it probably should work...) You know the column widths you want so it's simpler (and a lot quicker) to simply use a tabular. Commented Nov 9, 2018 at 9:51
  • I did a second edit to know how to do after this change.
    – Nulji
    Commented Nov 9, 2018 at 13:31
  • @Nulji just use arithmetic as you were doing before if \zz is textwidth after subtracting the right number of tabcolsep and arrayrulewidth you want a preamble of |p{.05\zz}|p{.35\zz}|p{.35\zz}|p{.15\zz}|p{.1\zz}|} Commented Nov 9, 2018 at 13:35
  • Thanks but how can I create 12/20 and 8/20 column at the first row THEN you're code line ? This is my biggest question (how to have 12/20 and 8/20 column at the first row, then 5 differents widths columns at the next row of the same table)
    – Nulji
    Commented Nov 9, 2018 at 14:24
2

Thanks the @david-carlisle 's help and for other people who would eventually look for a solution to this. Let's see the following code and picture :

\noindent\begin{tabular}{| 
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    p{\dimexpr 0.05\linewidth-2\tabcolsep-\arrayrulewidth} |
    }
\hline
1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18&19&20 \\ \hline
\multicolumn{12}{|p{\dimexpr 0.6\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l}
&
\multicolumn{8}{p{\dimexpr 0.4\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l} \\ \hline

\multicolumn{20}{|p{\dimexpr \linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l lLorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l} \\ \hline

\multicolumn{20}{|p{\dimexpr \linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l lLorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l} \\ \hline

N°
&
\multicolumn{7}{p{\dimexpr 0.35\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l}
&
\multicolumn{7}{p{\dimexpr 0.35\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l}
&
\multicolumn{3}{p{\dimexpr 0.15\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum}
&
\multicolumn{2}{p{\dimexpr 0.1\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum} \\ \hline

N°
&
\multicolumn{7}{p{\dimexpr 0.35\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l}
&
\multicolumn{7}{p{\dimexpr 0.35\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l}
&
\multicolumn{3}{p{\dimexpr 0.15\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum}
&
\multicolumn{2}{p{\dimexpr 0.1\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum} \\ \hline

N°
&
\multicolumn{7}{p{\dimexpr 0.35\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l}
&
\multicolumn{7}{p{\dimexpr 0.35\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l}
&
\multicolumn{3}{p{\dimexpr 0.15\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum}
&
\multicolumn{2}{p{\dimexpr 0.1\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum} \\ \hline

N°
&
\multicolumn{7}{p{\dimexpr 0.35\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l}
&
\multicolumn{7}{p{\dimexpr 0.35\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l}
&
\multicolumn{3}{p{\dimexpr 0.15\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum}
&
\multicolumn{2}{p{\dimexpr 0.1\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum} \\ \hline

N°
&
\multicolumn{7}{p{\dimexpr 0.35\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l}
&
\multicolumn{7}{p{\dimexpr 0.35\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l}
&
\multicolumn{3}{p{\dimexpr 0.15\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum}
&
\multicolumn{2}{p{\dimexpr 0.1\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum} \\ \hline

N°
&
\multicolumn{7}{p{\dimexpr 0.35\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l}
&
\multicolumn{7}{p{\dimexpr 0.35\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l l}
&
\multicolumn{3}{p{\dimexpr 0.15\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum}
&
\multicolumn{2}{p{\dimexpr 0.1\linewidth-2\tabcolsep-\arrayrulewidth}|}{Lorem ipsum} \\ \hline
\end{tabular}

The code produces :

Tab corresponding to the code

You must log in to answer this question.

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