0

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

LaTeX Table

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.

7
  • 1
    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 Nov 14, 2014 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 Nov 14, 2014 at 0:24
  • Please put full MWE, with preamble containing all called packages. Guessing them speeds down thinking on your problem. Nov 14, 2014 at 0:24
  • Thank you David for helping! I tried your solutions, then I put:
    – Dawson
    Nov 14, 2014 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, 2014 at 7:24

2 Answers 2

1

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}
\renewcommand\theadfont{\sffamily\bfseries}
\renewcommand\theadset{\setstretch{1.333}}
\newcommand\multicolthead[2]{\multicolumn{#1}{c}{\thead{#2}}}

\begin{document}

\noindent
\begin{table}[h!]
    \begin{tabularx}{\textwidth}{|l|*{20}{>{\arraybackslash}X|}}
 \multicolumn{1}{c}{}& \multicolthead{4}{GENNAIO}& \multicolthead{4}{FEBBRAIO} & \multicolthead{4}{MARZO} & \multicolthead{4}{APRILE} & \multicolthead{4}{MAGGIO} \\
\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} 

enter image description here

9
  • 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, 2014 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, 2014 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, 2014 at 23:42
  • 1
    @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, 2014 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, 2014 at 15:59
0

I come back in this thread to ask something really related.

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|}}

\multicolumn{1}{c}{}& \multicolthead{3}{\color{RoyalBlue}\textbf{I}}& \multicolthead{3}{\color{RoyalBlue}\textbf{II}} & \multicolthead{3}{\color{RoyalBlue}\textbf{III}} & \multicolthead{3}{\color{RoyalBlue}\textbf{IV}} & \multicolthead{3}{\color{RoyalBlue}\textbf{V}} & \multicolthead{3}{\color{RoyalBlue}\textbf{VI}} \\

\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:

Table Wrong Last Column

Thanks for help! R.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.