11

Let's assume that I have a table containing two l type columns that both contain rather short amounts of text. In one of the following rows I want to use a \multicolumn that merges both columns. I want it to automatically be as wide as both above mentioned columns together.

I can either achieve this using \multicolumn{2}{p{<width>}} and visually estimate the needed with. As can be seen from the following example, this is not very accurate:

enter image description here

\documentclass{article}
\usepackage{lipsum}
\begin{document}

\begin{tabular}{|ll|}
Some text & some more text \\ \hline
\multicolumn{2}{|p{4.2cm}|}{\lipsum[4]}\\
\end{tabular}
\end{document}

To get a better result, I can use the \widthof command from the calc package in order to measure the widths of the widest entries in both columns and calculate the required width for the multicolumn based on that.

\documentclass{article}
\usepackage{lipsum}
\usepackage{calc}
\begin{document}
\begin{tabular}{|ll|}
Some text & some more text \\ 
text      & a significantly longer line of text\\ \hline
\multicolumn{2}{|p{\widthof{Some text}+\widthof{a significantly longer line of text}+2\tabcolsep}|}{\lipsum[4]}\\
\end{tabular}
\end{document} 

enter image description here

However, this requires to manually determine the widest entry and entering it twice in the table (once in the column and once in the \widthof command) which makes this approach somewhat error-prone and not too flexible.

Being aware of the tabularx package that is somehow able to automatically calculate the required width that is needed to make a table as wide as a specified length, I wonder if there is a more automated solution than the above described ones.

0

3 Answers 3

3

This is a proof of principle and all the conceptional work has been done by David Carlisle in this answer. You need to run twice. I am sure some TeX Guru can write a nicer code, but this shows that it is doable.

\documentclass{article}
\usepackage{array}
\usepackage{lipsum}
\newlength{\cwidth}
\makeatletter% https://tex.stackexchange.com/a/152842/121799
\protected\def\z#1{\pdfsavepos\write\@auxout{\gdef\string\tpos#1{\the\pdflastxpos}}}%
\def\foo#1#2{\ifcsname tpos#1\endcsname\the\dimexpr\csname tpos#2\endcsname sp -\dimexpr\csname tpos#1\endcsname sp\relax\fi}
\makeatother

\begin{document}
\edef\numA{\foo{a}{c}}
\ifx\numA\empty
\typeout{rerun}
\else
\cwidth=\numA
\advance\cwidth by-2\tabcolsep
\fi

\begin{tabular}{|!{\z{a}}l!{\z{b}}l!{\z{c}}|}
some text & some more text \\ 
text      & a significantly longer line of text\\
\hline
\multicolumn{2}{|p{\cwidth}|}{\lipsum[1]}\\
\end{tabular}
\end{document}

enter image description here

6

It's easy peasy with tblr environment in LaTeX3 package tabularray:

\documentclass{article}
\usepackage{tabularray}
\usepackage{lipsum}
\begin{document}
\SetTblrInner{hspan=minimal}
\begin{tblr}{|l|l|}
\hline
 Some text & some more text \\
\hline
 \SetCell[c=2]{l} \lipsum[4] & \\
\hline
\end{tblr}
\end{document}

enter image description here

1

Only using one column and splitting the first row manually will avoid this problem:

\documentclass{article}
\usepackage{lipsum}
\begin{document}

\begin{tabular}{|p{4.5cm}|}
\begin{tabular}{@{}ll@{}}
Some text & some more text\\
\end{tabular}\\
\hline
\lipsum[4]\\
\end{tabular}
\end{document}
2
  • 1
    Thanks for your answer that definitely contains an interesting idea. However, this still requires to manually determine the width of the p type column. As you can see, the text still does not line up perfectly: i.sstatic.net/btUeY.png
    – leandriis
    Commented Jul 7, 2019 at 15:00
  • If you add more text to the inner tabular, you'll find out, that the first line will be to wide.
    – Jan
    Commented May 23, 2021 at 5:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .