# Automatic wrapping of a \multicolumn table cell

I output lots of tables to LaTeX using the estout/esttab package in Stata. I frequently want to have long footnotes on the bottom of the table, explaining coefficients, sources, etc. The estout package inserts footnotes using \multicolumn{#}{l}{...} at the bottom of the table, but requires me to figure out line breaks. That is, it will print several rows of \multicolumn{#}{l}{...} \\, but I have to tell estout what I want on each row. This is a pain.

I would like to find a way to make a \multicolumn wrap at the length of the table. I know that I can make \multicolumn wrap using the p{<width>} alignment specifier, but all the width macros I've been able to come up with (\hsize, \textwidth, \linewidth, etc.) cause the \multicolumn to wrap at the width of the page (or whatever fixed fraction I tell it), thus either stretching out my table or wrapping too short.

Basically I want LaTeX to set the table width automatically (no tabular* or tabularx), but ignore the \multicolumn for the purposes of setting the width of the table, but have the \multicolumn wrap at the full width of the table.

Here's an example of the type of table I'm working with

\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{table}
\centering
\caption{This is a table}
\begin{tabular}{l*{6}c}\toprule
& Something & Something & Something & Something & Something & Something \\\midrule
Amazing regression results & 100 & 100 & 100 & 100 & 100 & 100 \\\bottomrule
\multicolumn{7}{p{\textwidth}}{This is a footnote that's really really  really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really long}\\ %Ideally this should wrap without distorting the table
\end{tabular}
\end{table}
\end{document}


Any thoughts? I feel like there must be some LaTeX parameter containing the width of the table.

-

## migrated from stackoverflow.comAug 20 '11 at 1:49

This question came from our site for professional and enthusiast programmers.

How can I use \tiny also? I want "Comm. Vol." to be in two lines and very small. These are OK: \multicolumn{2}{>{\tiny}c}{{ Comm. Vol.}} and \multicolumn{2}{p{.1cm}}{{ Comm. Vol.}} But I cannot combine both. –  Kadir Apr 17 '13 at 12:23
Just a short comment. I had exactly the same problem, and I am happy to find someone that already asked it. Even tabu package (version 2.8) does not handle long multicolumns (it is a to-do task, see section 10 in the tabu manual). However, I think the final solution for this issue is too complicated for me. I will keep on creating several multicolumn rows, as I do not happen to have many of these tables. –  Vicent Oct 24 '13 at 15:10

Werner's answer is really close to what I needed, but I figured out a more streamlined way to do it that (sort of) avoids the duplication: The \multicolumn is set to be \linewidth, which is initially re-set to be really small. The whole tabular environment (with the \multicolumn) is then put into a macro. The macro is then put in a \setbox, \linewidth is reset to the width of the box, and then the table is printed as a macro. Like so:

\documentclass{article}
\usepackage{booktabs,calc}
\begin{document}
\begin{table}
\centering
\caption{This is a table}
\setlength{\linewidth}{.1cm}\newcommand{\contents}{\begin{tabular}{l*{4}c}\toprule
& Something & Something & Something & Something  \\\midrule
Amazing regression results & 100 & 100 & 100 & 100  \\\bottomrule
\multicolumn{5}{p{\linewidth}}{This is a footnote that's really really  really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really long}\\ %Ideally this should wrap without distorting the table
\end{tabular}}
\setbox0=\hbox{\contents}
\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}
\contents
\end{table}
\end{document}


It's a similar idea to Werner's answer, in that the table is first set in a box that is not printed, and the size of the box is used to set the \multicolumn. But this way while the table is technically duplicated from the perspective of the compiler, you don't have to duplicate the source code, and it's easier to put together (and to output from Stata, my goal here), since you're really just surrounding the tabular environment with a few commands.

-

LaTeX does not typeset the contents of tabular (or array) on a row-by-row basis. The obvious reason for this is that columns should line up (otherwise they won't be referred to as columns...). And, the only way of lining these up properly is if you - the LaTeX compiler - could see the entire table. So, here's my suggestion...

Store an entire copy of the table without the \multicolumn row in a box. Then, use the width of the box to set the width in your \multicolumn{<#>}{p<width>}{...} column. Here's a minimal working example:

\documentclass{article}
\usepackage[landscape]{geometry}% http://ctan.org/pkg/geometry
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
\usepackage{calc}% http://ctan.org/pkg/calc
\begin{document}

\begin{table}
\centering
\caption{This is a table}
\setbox0=\hbox{% Store table in box0
\begin{tabular}{l*{6}c}
\toprule
& Something & Something & Something & Something & Something & Something \\\midrule
Amazing regression results & 100 & 100 & 100 & 100 & 100 & 100 \\\bottomrule
\end{tabular}
%}
\begin{tabular}{l*{6}c}\toprule
& Something & Something & Something & Something & Something & Something \\\midrule
Amazing regression results & 100 & 100 & 100 & 100 & 100 & 100 \\\bottomrule
\multicolumn{7}{p{\wd0-2\tabcolsep-.25em}}{This is a footnote that's really really
really really really really really really really really really really really
really really really really really really really really really really really
really really really really really really really really really really really
really really really really really really really really really really really
really really really really really really really really really really really
really really really really really really really really really really really
really really really really really really really really really really really
really really really really really really really really long} %Ideally this should wrap without distorting the table
\end{tabular}
\end{table}

\end{document}


In the above document, the geometry package was loaded merely to change the page orientation to landscape since your MWE didn't fit on a portrait page. The calc package is used to perform infix length calculations - the argument to the p column specification: \wd0-2\tabcolsep-.25em which takes the width of box 0 \wd0, subtracts 2 times \tabcolsep and also .25em. The last dimension comes from additional space necessarily inserted by booktabs's rules.

I don't see a way around the "duplication" of the table due to the way LaTeX typesets tables. Of course, tabular* and tabularx environments would be ideal (due to it's width specification in the definition), but that's out of the question.

-

To put the solution by rsandler into an automated setup, one would have the following Stata code:

*Estimate and Store results
local PostFootTxt "\hline\hline \multicolumn{@span}{p{\linewidth}}{\footnotesize @note}\\ \end{tabular}}"
local TblMainTxt = "This is a footnote that's really really really " + ///
"really really really really really really really really really " + ///
"really really really really really long"
esttab stored_results_names using filename.tex, note("TblMainTxt'") postfoot(PostFootTxt')


And then the LaTeX (or similar LyX) file would be:

\documentclass{article}
\usepackage{booktabs,calc}
\begin{document}
\begin{table}
\centering
\caption{This is a table}
\setlength{\linewidth}{.1cm}\newcommand{\contents}{\input{filename.tex}}
\setbox0=\hbox{\contents}
\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}
\contents
\end{table}
\end{document}

-

A few more notes on the automated solution suggested by BeingQuisitive:

For this to work you can't use the title() or longtable options in the estout/esttab command, as they impose LaTeX table or longtable environments that interfere with rsandler's wrapper.

Also, as I've learned the hard way, there's a 502-character limit to footnotes using this method. The reason is actually annotated in Ben Jann's estout.ado program: the estout code uses the -:subinstr- macro function to substitute the text you provide in the note() option into the @note placeholder in postfoot(). This macro function has that 502-character limit, for some reason. If your footnote is too long the @note won't be replaced, and "@note" (literal) will be the text that will show up in your footnote.

The exact place in estout.ado where this comes up is on line 3339 (in v3.13 at least):

capt local value: subinstr local value "@atvar'" "macval(atvar')'"', all
// note: returns error if length of <to> is more than 502 characters


Ben Jann (the author of estout) also discusses the issue on Statalist here: http://www.stata.com/statalist/archive/2007-02/msg00581.html

Moral of the story: Keep your footnotes within the character limit!

-