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}
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 :
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
(The first cell of the last row is a multirow 2)
In that case, how to do this with tabular and not tabularx ?