0

There have been many questions about tabularx and \multicolumn but I haven't found an answer to this one.

One can use X columns for two reasons: the first is to have equal-width paragraph columns, in formats like |X|X|X|; but there is also another reason: to have a table of full text width containing a paragraph column and other non-paragraph columns of variable width, without tedious calculations about the exact width of the paragraph column (which would be necessary in a p type column). This would be a format like |l|l|X|l|.

This is what I'm using, but at some point I need to merge this X column with the neighboring one, which is a regular l column of unknown width:

\documentclass{article}

\usepackage{tabularx}

\begin{document}

\noindent\begin{tabularx}{\textwidth}{|l|l|X|}
A word&Another one&This sentence is set in the remaining space of the first line\\
Something&\multicolumn{2}{X|}{And here I would like to occupy the space of columns 2 and 3 again for a long sentence that has to be broken into a paragraph}
\end{tabularx}

\end{document}

enter image description here

In the screen capture I have placed a red frame around the space I would like to occupy.

Reading other answers I noticed that I can change the width of the multicolumn by using the following code in the format: >{\hsize=1.33\hsize}X. But this means that for every table and column combination I will have to fiddle around to find the exact value of the factor of \hsize.

What I need is a way to obtain it automatically, without the necessity to run the document a dozen of times until I get the correct value of the \hsize coefficient. I.e., a paragraph column that will have the width of the X column plus the one of the neighboring l column (plus the intercolumn width).

Is there a way to get that?

(Notice that contrarily to other questions on stack exchange, this is not X-cell arithmetic, where the multicolumn replaces n X columns so that its width is simply n times the width of the X column. Here the width of the l column is variable and not related to the width of the X column in any way.)

1
  • 2
    some things you can't have.... In most (or some) real cases you can fix the middle column say |l|wl{2cm}|X| then your multcol is easy \multicolumn{2}{X|}{\hsize=\dimexpr\hsize+2cm+2\tabcolsep+\arrayrulewidth\relax ....} or if you know the longest entry in the middle column measure that first instead of guessing 2cm Commented Aug 23, 2022 at 16:07

1 Answer 1

1

Based on David's suggestion, here is a solution to the problem. The only compromise is that you need to provide the contents of the widest cell of the column in each newly defined \multicolumn command. But this is by far easier than fiddling with coefficient values (and not getting it right in the end…).

Here is my code:

\newlength{\yhwidth}
\def\yhmulticolumn#1#2#3#4{\multicolumn{#2}{#3}{\settowidth{\yhwidth}{#1}\hsize=\dimexpr\hsize+\yhwidth+2\tabcolsep+\arrayrulewidth\relax #4}}

\noindent\begin{tabularx}{\textwidth}{|l|l|X|}
A word&Another one&This sentence is set in the remaining space of the first line\\
Something&\yhmulticolumn{Another one}{2}{X|}{And here I would like to occupy the space of columns 2 and 3 again for a long sentence that has to be broken into a paragraph}
\end{tabularx}

and the result:

enter image description here

Thanks David!

(Now if anyone has a solution without the need of giving the largest cell's contents, don't hesitate to share it! After all, longtable does such largest cell calculations to obtain tables over several pages, so it should be possible to do it…)

You must log in to answer this question.

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