Automatic wrapping of a \multicolumn table cell
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.
Update 11/16
Since I wrote this post there have been a couple other answers regarding the Stata implementation of this wrapper approach. My approach (devised back when I wrote the question and answer) addresses the concerns BeingQuisitive and grossdpg mention, while preserving most of the functionality of the estout options. Specifically, I use the substitute()
option on estout/esttab, which replaces a given string in the LaTeX output with another. Doing it this way rather than using postfoot()
and prehead()
allows you to employ the various estout options that add content to those areas, without having to set them manually (including e.g. title()
and longtable()
). Example Stata code:
#delimit ;
esttab * using output.tex, replace booktabs
substitute(\begin{tabular} \setlength{\linewidth}{.1cm}\newcommand{\contents}{\begin{tabular}
\end{tabular} \end{tabular}}\setbox0=\hbox{\contents}\setlength{\linewidth}{\wd0-2\tabcolsep-.25em}\contents
{l}{\footnotesize {p{\linewidth}}{\footnotesize )
addnote{"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")
;
Note: I seem to recall that the default for substitute()
replaces _ and $ with _ and \$, respectively. If you need this, simply add that to the substitute option.
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.