12

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.

8

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}
  • 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
  • 1
    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
  • @DavidCarlisle do you know if my solution can be done in one tabularx environment? – Ben Mar 28 at 16:41
3

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.

0

I present a solution, where the size of the table fits its content:

\documentclass{article}

\usepackage{array}
\usepackage{booktabs}
\usepackage{threeparttable}

\begin{document}


\newlength\widthSubTabOne
\settowidth\widthSubTabOne{This is a piece of text. The longer}
\newlength\lengthOne
\setlength\lengthOne{\dimexpr(\widthSubTabOne+2\tabcolsep)/5-2\tabcolsep\relax}

\newlength\widthSubTabTwo
\settowidth\widthSubTabTwo{it is, the longer the last column.}
\newlength\lengthTwo
\setlength\lengthTwo{\dimexpr(\widthSubTabTwo+2\tabcolsep)/2-2\tabcolsep\relax}


\begin{table}[h!]
\centering
\begin{threeparttable}
\caption{title}
\begin{tabular}{*{5}{w{c}{\lengthOne}}*{2}{w{c}{\lengthTwo}}}
\toprule
\multicolumn{5}{c}{This is a piece of text. The longer} & \multicolumn{2}{c}{it is, the longer the last column.}\\
\cmidrule(r){1-5} \cmidrule(l){6-7}
a & b\tnote{$\ast$} & c & d & e & f & g\\
\midrule
1 & 2 & 3 & 4 & 5 & 6 & 7\\
\bottomrule
\end{tabular}
\begin{tablenotes}[flushleft]
\setlength\labelsep{0pt}
\item long long long long long long long long long long long long long long long long long long long long long long long long long long long text
\end{tablenotes}
\begin{tablenotes}
\item [$\ast$] abc
\end{tablenotes}
\end{threeparttable}
\end{table}

\end{document}

output

  • 1
    you could simplify by just using \settowidth\widthSubTabOne{This is a piece of text. The longer} and just adding 2\tabcolsep(+2\rulewidth)` – David Carlisle Mar 28 at 16:49

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.