# How to wrap text in a cell (without any explicit lengths)

I'm hoping to wrap text in a single "mega-cell" at the end of a table.

 ----------------------
----------------------
| entry1  | entry 2  |
----------------------
| entry3  | entry 4  |
----------------------
| big cell at the end|
| with multicolumn / |
| wrapping text      |
----------------------


There's plenty of good solutions here but they require fixed lengths to be specified, either for the column width or cell width or an embedded minipage. Problem is, I want the "mega-cell" to inherit the table width given naturally by the rest of the table. (I have a very very very large auto-generated table and would prefer not to have to tweak widths manually if possible.)

Is it possible to "hook" the current width of the table for the last cell, or is there another solution for this?

• Having the mega-cell centre-aligned would also be great. Thanks for any help! :) Nov 7 '11 at 14:21

I think you will have to use something similar like tabularx, i.e. measure the width yourself and then apply it to the column. One way to do this is to actually use two tabulars which look like one:

\documentclass{article}

\begin{document}

\vbox{%
\begin{lrbox}{0}
\begin{tabular}{|c|c|}    \hline
entry 1  & entry  2 \\\hline
entry 3  & entry  4 \\\hline
\end{tabular}
\end{lrbox}
\hbox{\usebox0}%
\vskip-1pt
\hbox{%
\begin{tabular}{|p{\dimexpr\wd0-2\tabcolsep\relax}|}
big cell at the end
with multicolumn /
wrapping text \\\hline
\end{tabular}%
}%
}

\end{document}


Centering the multi-column is best done using the standard array package, which improves also the lines of the table but requires a little adjustment:

\documentclass{article}

\usepackage{array}

\begin{document}

\vbox{%
\begin{lrbox}{0}
\begin{tabular}{|c|c|}    \hline
entry 1  & entry  2 \\\hline
entry 3  & entry  4 \\\hline
\end{tabular}
\end{lrbox}
\hbox{\usebox0}%
\vskip-1pt
\hbox{%
\begin{tabular}{|>{\centering\arraybackslash}p{\dimexpr\wd0-2\tabcolsep-2\arrayrulewidth\relax}|}
big cell at the end
with multicolumn /
wrapping text \\\hline
\end{tabular}%
}%
}

\end{document}


• Many thanks Martin! Could you maybe explain the p{\dimexpr\wd0-2\tabcolsep\relax} expression a little? Nov 7 '11 at 18:17
• I store the table in a box register. This register has the number 0. \wd<box> Gives the width of the given box. \dimexpr .. \relax allows you to do calculations. And 2\tabcolsep is 2x the table column separation, i.e. the space which is added to the left and right of a cell. I subtract it because otherwise the lower cell would be too wide. Nov 7 '11 at 18:20