TransWikia.com

tabularx and textwidth

TeX - LaTeX Asked on December 26, 2020

I’d like to insert a 3-columm table in my project, but I am facing problems trying to fit text in a multicolumm cell. Here is my code:

begin{center}
begin{tabularx}{textwidth}{|c X c|}
hline

multicolumn{1}{|c}{} & 
multicolumn{2}{>{hsize=dimexpr2hsize+2tabcolsep+arrayrulewidthrelax}X|}{ cell0: text text text text text text text text text text text text text text text text }
hline

cell1 & cell2: text text text text text text text text & cell3

hline
end{tabularx}
end{center}

The problem is, whenever I use the hsize command to make the text fit into cell0 properly, cell2 in the following line loses its automatic adjustment to text and acquires fixed width (which I do not want). So, how can I adjust the text in cell0 at the same time that cell2 keeps the automatic adjustment to text?

One Answer

Your calculation of the usable width of the "cell0" object is incorrect. It would be (almost) correct if both underlying columns were of type X; however, that's not the case, is it?

To calculate the usable combined width of columns 2 and 3 correctly, I suggest you proceed as follows.

  • First, find the cell in column 3 that determines the natural width of that column. This is usually done easily and reliably by visual inspection.

  • Suppose the cell that defines the natural width of column 3 consists of the string aratherlongword. Its width may be determined by running

   newlengthcolwidth
   settowidthcolwidth{aratherlongword}
  • Then the usable combined width of columns 2 and 3 may be calculated as
   hsize=dimexprhsize+colwidth+2tabcolseprelax

A full MWE (note that I've discarded multicolumn{1}{|c}{} since it's redundant.)

enter image description here

documentclass{article}
usepackage{tabularx}
begin{document}

begin{center}
newlengthcolwidth
settowidthcolwidth{aratherlongword}
begin{tabularx}{textwidth}{|c X c|}
hline
& multicolumn{2}{>{hsize=dimexprhsize+colwidth+2tabcolseprelax}X|}{%
  cell0: text text text text text text text text text text text text text text text text }
hline
cell1 & cell2: text text text text text text text text & aratherlongword
hline
end{tabularx}
end{center}
end{document}

Correct answer by Mico on December 26, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP