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.
\documentclass[a4paper,12pt,oneside]{scrbook}
\usepackage[dutch]{babel} %Quotes won't work without babel
%\usepackage[utf8]{inputenc} %This is very important!
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage[pdfborder={0 0 0}, breaklinks=true, pdftex=true, raiselinks=true]{hyperref}
\usepackage{tabularx}
\newcolumntype{Y}{>{\raggedright\arraybackslash}X}
\let\oldmc\multicolumn
\def\multicolumn#1#2#3{%
\oldmc{#1}{>{\hsize=\dimexpr#1\hsize+#1\tabcolsep+#1\tabcolsep-2\tabcolsep\relax\centering}X}{#3}\tabularnewline}
\begin{document}
\section{Description}
\noindent
\begin{tabularx}{\linewidth}{YY}\hline
\multicolumn{2}{c}{a very very very long text that doesn´t even fit in my table, so it must be a way to make the table automatically break the line}
\end{tabularx}
\end{document}
Best Answer
edit:
like this (considering your comment below answer)?
it is obtained by use of
tabularx
table environment, macros\thead
and\makegapedcells
frommakcell
package and appropriate selection of columns type. for underlined text in second column's cells is used\ul{...}
instead of\underbar{...}
(which not enable to break lines) from packagesoul
which enales to brak text in cells. for values with measure i suggest to use\siunitx
package: