How to fix differents columns width with tabularx?

by Nulji   Last Updated November 09, 2018 01:23 AM

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 :

\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


\end{tabularx}

Correct

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

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

That gives me :

Not correct



Answers 1


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.

David Carlisle
David Carlisle
November 09, 2018 01:01 AM

Related Questions


Last column does not have the desired width

Updated July 19, 2018 18:23 PM

table overflow page

Updated October 18, 2018 04:23 AM

FP calcule problem with multicolumn

Updated December 17, 2017 23:23 PM


Multi-column table width issue

Updated July 03, 2017 18:23 PM