# Table Column Width Error

I'm trying to produce a table in LaTeX and I almost succeed. The table is this one:

My question is: why the fourth column of "Gennaio", "Febbraio" and "Maggio" isn't as the same width as the rest? What do I need to modify in the code to overcome this problem?

The code is the following one:

\begin{table}[h]
\begin{tabular}{cllllllllllllllllllll}
\multicolumn{1}{c}{}                                                              & \multicolumn{4}{c}{GENNAIO}                                                                                                                                                                   & \multicolumn{4}{c}{FEBBRAIO}                                                                                                                                                                  & \multicolumn{4}{c}{MARZO}                                                                                                                                                                     & \multicolumn{4}{c}{APRILE}                                                                                                                                                                    & \multicolumn{4}{c}{MAGGIO}                                                                                                                                                                 \\ \hline
\multicolumn{1}{|c|}{\begin{tabular}[c]{@{}c@{}}Collezione\\ Dati\end{tabular}}   & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{}                         & \multicolumn{1}{l|}{}                         & \multicolumn{4}{l|}{}                                                                                                                                                                         & \multicolumn{4}{l|}{}                                                                                                                                                                      \\ \hline
\multicolumn{1}{|c|}{\begin{tabular}[c]{@{}c@{}}Elaborazione\\ Dati\end{tabular}} & \multicolumn{4}{l|}{}                                                                                                                                                                         & \multicolumn{4}{l|}{}                                                                                                                                                                         & \multicolumn{1}{l|}{}                         & \multicolumn{1}{l|}{}                         & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{4}{l|}{}                                                                                                                                                                        \\ \hline
\multicolumn{1}{|c|}{\begin{tabular}[c]{@{}c@{}}Report\\ Dati\end{tabular}}       & \multicolumn{4}{l|}{}                                                                                                                                                                         & \multicolumn{4}{l|}{}                                                                                                                                                                         & \multicolumn{4}{l|}{}                                                                                                                                                                         & \multicolumn{4}{l|}{}                                                                                                                                                                         & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} & \multicolumn{1}{l|}{\cellcolor[HTML]{9B9B9B}} \\ \hline
\end{tabular}
\end{table}


Thanks to anyone who has the patience to help me! R.

• in the underlying \halign primitive, if a spanning cell is wider than the cells it spans all the width goes in the last spanned column – David Carlisle Nov 14 '14 at 0:21
• change you llllll to *{16}{p{10mm}} or whatever size works to force your columns equal width with 4 of them being just wider than your header text – David Carlisle Nov 14 '14 at 0:24
• Please put full MWE, with preamble containing all called packages. Guessing them speeds down thinking on your problem. – Przemysław Scherwentke Nov 14 '14 at 0:24
• Thank you David for helping! I tried your solutions, then I put: – Dawson Nov 14 '14 at 7:17
• @DavidCarlisle Thank you David for helping! I tried your solution, then I inserted the following lines: \usepackage{array, multirow} \newcolumntype{C}{>{\centering\arraybackslash}p{3em}} \begin{tabular}{*{21}{|C}|} But the table width doesn't change. Maybe I just didn't understand your answer! @PrzemysławScherwentke Thank you for answering! For this table I'm using the following packages: \usepackage{graphicx, array, booktabs, multirow} and I added this line in the preamble: \newcolumntype{C}{>{\centering\arraybackslash}p{3em}} – Dawson Nov 14 '14 at 7:24

You can use the tabularx package. Here is a code, also using the makecell package, in order to have a common formatting of column and row heads, and the possibility of linebreaks inside cells:

\documentclass[a4paper]{article}

\usepackage[nomarginpar, showframe]{geometry}
\usepackage{setspace}
\usepackage[HTML]{xcolor}
\usepackage{array, tabularx, makecell, colortbl}

\begin{document}

\noindent
\begin{table}[h!]
\begin{tabularx}{\textwidth}{|l|*{20}{>{\arraybackslash}X|}}
\hline
\thead{Collezione\\ Dati} & \cellcolor[HTML]{9B9B9B}& \cellcolor[HTML]{9B9B9B} & \cellcolor[HTML]{9B9B9B} & \cellcolor[HTML]{9B9B9B} & \cellcolor[HTML]{9B9B9B} & \cellcolor[HTML]{9B9B9B} & \cellcolor[HTML]{9B9B9B} & \cellcolor[HTML]{9B9B9B} & \cellcolor[HTML]{9B9B9B} &\cellcolor[HTML]{9B9B9B} & & & \multicolumn{4}{l|}{} & \multicolumn{4}{l|}{} \\
\hline
\thead{Elaborazione\\ Dati} & \multicolumn{4}{l|}{} & \multicolumn{4}{l|}{} & & & \cellcolor[HTML]{9B9B9B} & \cellcolor[HTML]{9B9B9B} & \cellcolor[HTML]{9B9B9B} & \cellcolor[HTML]{9B9B9B} & \cellcolor[HTML]{9B9B9B} & \cellcolor[HTML]{9B9B9B} & \multicolumn{4}{l|}{} \\ \hline
\thead{Report\\ Dati} & \multicolumn{4}{l|}{} & \multicolumn{4}{l|}{} & \multicolumn{4}{l|}{} & \multicolumn{4}{l|}{} & \cellcolor[HTML]{9B9B9B} & \cellcolor[HTML]{9B9B9B} & \cellcolor[HTML]{9B9B9B} & \cellcolor[HTML]{9B9B9B} \\
\hline
\end{tabularx}
\end{table}

\end{document}


• Wow. Wonderful. I should learn tabularx, it seems more powerful than tabular. Bernard, you gave me the solution and I have a question related to your code, if I can: what does it produce this code {>{\arraybackslash}X|}} Thank you again, R. – Dawson Nov 14 '14 at 14:26
• \arraybackslash is really needed only in the last column. It says to latex to interpret \ as meaning tabularnewline (in X columns, there can be linebreaks inside cells). – Bernard Nov 14 '14 at 18:20
• I didn't understand, but I think I'm too ignorant. Again, thank you very much Bernard for the help! – Dawson Nov 14 '14 at 23:42
• @user51937: Some cells are \parboxes, hence you can use \ inside them. However, in tables, \ also means really: ‘what follows is a new row’,, so that it can be confusing for latex in some situations, and you can have error messages on compiling. \arraybackslash disambiguates the situation and avoids error messages — at least in most cases. – Bernard Nov 15 '14 at 0:07
• Dear @Bernard, sorry if I come back in this thread, but I have a question that for you will be a lot easier than it would be for me: how can I reduce the size of the boxes in that table? I mean reduce the space between "Collezione" and the upper line, "Elaborazione" and the upper line and "Report" and the upper line. Thank you! R. – Dawson Nov 15 '14 at 15:59

I need to compile the following code, representing a table, quite similar the one you made. But something doesn't work with the last column, I think.

\begin{table}[h!]
\begin{tabularx}{\textwidth}{|l|*{20}{>{\arraybackslash}X|}}

\hline

\thead{\color{RoyalBlue}One\\\color{RoyalBlue} Time} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} \\

\hline

\thead{\color{RoyalBlue}Two\\\color{RoyalBlue} Times} & & & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & & & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \\

\hline

\thead{\color{RoyalBlue}Three\\\color{RoyalBlue}Times} & \multicolumn{3}{l|}{} & & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & & & \multicolumn{3}{l|}{} \\

\hline

\thead{\color{RoyalBlue}Four\\\color{RoyalBlue} \& Times} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & \multicolumn{3}{l|}{} & & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & \cellcolor[HTML]{D3D3D3} & & \\

\hline

\end{tabularx}
\end{table}


Which gives the following output:

Thanks for help! R.