Set multicolumn width to default table width
The space available isp{\dimexpr\textwidth-2\tabcolsep\relax}
due to the column padding LaTeX applies, assuming you have \noindent\begin{tabular}
otherwise the table itself will be offset by \parindent
.
However if the span is wider than the columns it spans, TeX puts all the extra width into the last column. If that is a problem, edit your example to be a complete (small) document that shows the problem.
Knowledge of the column entries (especially the widest elements) allows you to calculate the width of the \multicolumn
accurately:
\documentclass{article}
\usepackage{booktabs,calc,lipsum}% http://ctan.org/pkg/{booktabs,calc,lipsum}
\begin{document}
\begin{tabular}{l l}
\toprule
Some text & Some more text \\
Short & Very super super long \\
Longer than others & Shrt \\
\multicolumn
{2}
{p{\widthof{Longer than others}+\widthof{Very super super long}+2\tabcolsep}}
{\lipsum[1]} \\
\bottomrule
\end{tabular}
\end{document}
calc
provides \widthof{<stuff>}
that returns the natural width of <stuff>
. Each column is padded on both sides by \tabcolsep
.
The total width of the table is not available until the whole table has been typeset, unless all columns are specified with an explicit width (p
columns). TeX chooses the column width only when the whole table has been loaded into memory, and so this makes quite difficult to solve your problem, because a text width has to be specified in order to typeset a paragraph.
For this particular problem, where all the columns must be spanned, there's an automatic solution: first typeset the table ignoring the paragraph spanning the columns to get the width, then retypeset it.
The first argument in \multipar
is necessary, because the total number of columns is not available inside a tabular
environment.
\documentclass{article}
\usepackage{booktabs,lipsum}
\usepackage{environ}
\newsavebox{\funnytabularbox}
\newlength{\funnytabularwd}
\makeatletter
\NewEnviron{funnytabular}[1]
{\begin{lrbox}{\funnytabularbox}
\let\multipar\@gobbletwo
\begin{tabular}{#1}\BODY\end{tabular}
\end{lrbox}%
\setlength{\funnytabularwd}{\wd\funnytabularbox}%
\addtolength{\funnytabularwd}{-2\tabcolsep}%
\let\multipar\@multipar
\begin{tabular}{#1}\BODY\end{tabular}}
\newcommand\@multipar[2]{\multicolumn{#1}{p{\funnytabularwd}}{#2}}
\makeatother
\begin{document}
\begin{funnytabular}{l l}
\toprule
Some text & Some more text \\
\midrule
Short & Very super super long \\
\midrule
Longer than others & Shrt \\
\midrule
\multipar{2}{\lipsum*[1]} \\
\bottomrule
\end{funnytabular}
\end{document}