1

As far as I understood how tabulars are build, you declare how many columns you need and how to format them. In some columns (notably l, c and r) LaTeX computes the needed width of those columns. On other columns the author defines the width (by using p{<width>} and similar declarations). LaTeX also adds intercolumn space. This results in the total width of the table.

I also think, I understand, how LaTeX computes the resulting width of a X column, when used in a tabularx environment, fitting the column, so that the total width reaches the defined width of the table.

My question is: is it allowed, to use a p{<width>} declaration within a \multicolumn-command? As far as I understand, LaTeX must get confused if the author inserts such a \multicolumn, which defines its own width, instead of using the LaTeX computed width of the combined columns in question.

For example's sake: lets say, we have two columns like a l-column and a p{2cm}. LaTeX found out, that the longest entry in the first column is 1cm so, the total usable width of these two columns is 1 cm + 2\tabcolsep + 2cm. Suppose now that the author orders \multicolumn{2}{p{5cm}}{...}, which is obviously much wider than the LaTeX-computed width of 3cm + 2\tabcolsep.

But what should authors use instead, if they want to be able to insert lots of text, being distributed into several lines as in a regular p-column?

Mico
  • 506,678
Jan
  • 5,293
  • 1
    yes it is allowed – David Carlisle May 22 '21 at 07:52
  • 1
    As `\documentclass{article} \begin{document}

    \begin{tabular}{l|l|l|} text & \multicolumn{2}{|l|}{text} \ text & text & text \ \end{tabular}\bigskip

    \begin{tabular}{l|l|l|} text & \multicolumn{2}{|p{6cm}|}{multicolumn text multicolumn text } \ text & text & text \ \end{tabular}\bigskip

    \begin{tabular}{l|l|l|} text & \multicolumn{2}{|p{1.25cm}|}{\hspace{0pt}multicolumn text multicolumn text } \ text & text & text \ \end{tabular} \end{document}shows, using aptype column in a\multicolumn` command definitely works.

    – leandriis May 22 '21 at 08:08
  • 2
    How to determine the required width forr such a p type column is a different problem, about which I asked the following question How to automatically calculate the width of a multicolumn in a table based on the combined widths of the columns that are merged. You may be interested in the answers to this, as well. – leandriis May 22 '21 at 08:09
  • Note that when the multicolumn is wider than the combined columns, the excess width is added to the last column replaced. Narrower is not a problem, although putting a \parbox inside a c column might look better. – John Kormylo May 22 '21 at 14:16
  • @DavidCarlisle thank you. Short but unambiguous answer :-) – Jan May 23 '21 at 05:11
  • @leandriis Thank you for your examples. My search haven't found them. :-( – Jan May 23 '21 at 05:12
  • @JohnKormylo using a parbox might help, as long, as you know how wide the box may be. Another solution for this kind of problem would be a tabular in a tabular?! – Jan May 23 '21 at 05:14
  • As @leandriis pointed out: my question is kind of duplicate of his question and might hence be closed. That would somehow add an answer it. – Jan May 23 '21 at 05:15

1 Answers1

1

With tabularray, you can choose the width of the multicolumn cell and also the spanning algorithm:

\documentclass{article} 
\usepackage{tabularray}
\usepackage{caption}

\begin{document}
In Table \ref{tab:default} (default span), the multicolumn cell is 5cm wide. The third single cell is enlarged to reach, together with the other two cells, the total width of 5cm.
\begin{table}[h] \centering \caption{\label{tab:default}Default span} \begin{tblr}{ hlines, vlines, cell{1}{1}={c=3}{c,5cm} } Multicolumn cell with default span \ First & Second & Third \ \end{tblr} \end{table}

In Table \ref{tab:even} (even span), the multicolumn cell is 5cm wide. The three single cells are enlarged to have the same width and reach, all together, the total width of 5cm.
\begin{table}[h] \centering \caption{\label{tab:even}Even span} \begin{tblr}{ hlines, vlines, hspan=even, cell{1}{1}={c=3}{c,5cm} } Multicolumn cell with even span\ First & Second & Third \ \end{tblr} \end{table}

In Table \ref{tab:minimal} (minimal span), the multicolumn cell is adapted to the total width of the three single cells. In this case, the 5cm width of the multicolumn cell is ignored.
\begin{table}[h] \centering \caption{\label{tab:minimal}Minimal span} \begin{tblr}{ hlines, vlines, cell{1}{1}={c=3}{c,5cm}, hspan=minimal, } Multicolumn cell with minimal span\ First & Second & Third\ \end{tblr} \end{table} \end{document}

enter image description here

P.S.: the vertical rules are added only to show the column width. They should not be used in professional tables.

CarLaTeX
  • 62,716