Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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}
share|improve this answer
    
So, \halign is the TeX primitive tabular is built on, right? I'll have to take a better look at it then. The tabularx example is close to what I want, but when I tried it in the real document (as opposed to the MWE), I found out it conflicts with the class that I have to use (or supertabular, which I use in several places). I've tried modifying the second-to-last example to look like the last, but to no avail. Ideas or a webpage that would explain this in a bit more detail? Thanks! –  Jan Benes Jan 25 '13 at 15:00
    
yes any latex thing that uses & is most likely using \halign somewhere. supertabular has a multipage version of tabular* so the version with extracolsep should work there. –  David Carlisle Jan 25 '13 at 15:04

Thank you so much David, this answer has helped me so much in using Excel2Latex. I now use this in my preamble

\usepackage{tabularx}
\usepackage{array}
\newcommand{\deftab}[2]{\begin{tabularx}{#1}{*{#2}{|>{\centering\arraybackslash}X}|}

and then use

\deftab{width}{no. of columns}

in place of the

\begin{tabular}

that Excel2Latex uses, and then I replace the necessary \end{tabularx} at the end of the table. It automatically fixes all of these types of problems in tables of any size and does so without disrupting any formatting.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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