[Tex/LaTex] How to wrap text in a cell (without any explicit lengths)

multicolumnmultirowtables

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

 ----------------------
 | header1 | header 2 |
 ----------------------
 | 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?

Best Answer

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
    header 1 & header 2 \\\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}

Result


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
    header 1 & header 2 \\\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}

enter image description here

Related Question