[Tex/LaTex] Table column widths disproportionate due to multicolumn cell being too long

multicolumntables

I'm trying to typeset a table that would have a header in the first row (I'm not looking for a different/better solution to adding a header, but for a fix to this solution. Thanks). I want to achieve this by having a multicolumn cell spanning all the columns of the first row. The problem is that if the text is too long, the column widths are uneven, with the last column accumulating excess width. I would expect all of the columns to be of approximately the same width (although I cannot find a decent source that would say they should), given the fact that all the text in the second row is of approximately the same width.

I find this behaviour quite puzzling and have no idea what the cause could be. I know I could perhaps remedy this by manually setting the column widths, but am curious as to how to fix this without doing that (I do not need or indeed want the columns to be of the same width, but roughly proportional to the length of their content), preferably but not necessarily by using the tabular environment only (some other conflict with the document class I am using).

What is it that I'm doing or assuming wrong? And why?

MWE:

\documentclass{article}
\begin{document}

\begin{tabular}{ccccc}
\multicolumn{5}{c}{This is a piece of text. The longer it is, the longer the last column.} \\ 
1 & 2 & 3 & 4 & 5 \\
\hline
\end{tabular}

\end{document}

Resulting table. Notice how the last column of the second row accumulates excess width.

Best Answer

It's a feature of the \halign primitive:

enter image description here

\documentclass{article}
\usepackage{tabularx}
\begin{document}

\centering
\parskip2\baselineskip

\begin{tabular}{*5{c}}
\multicolumn{5}{c}{This is a piece of text. The longer it is, the longer the last column.} \\ 
1 & 2 & 3 & 4 & 5 \\
\hline
\end{tabular}


\begin{tabular}{*5{c}}
\multicolumn{5}{c}{\makebox[0pt]{This is a piece of text. The longer it is, the longer the last column.}} \\ 
1 & 2 & 3 & 4 & 5 \\
\hline
\end{tabular}


\begin{tabular*}{\textwidth}{@{\extracolsep{\textwidth minus\textwidth}}*5{c}@{}}
\multicolumn{5}{c}{This is a piece of text. The longer it is, the longer the last column.} \\ 
1 & 2 & 3 & 4 & 5 \\
\hline
\end{tabular*}


\begin{tabularx}{\textwidth}{*{5}{>{\centering\arraybackslash}X}}
\multicolumn{5}{c}{This is a piece of text. The longer it is, the longer the last column.} \\ 
1 & 2 & 3 & 4 & 5 \\
\hline
\end{tabularx}


\end{document}
Related Question