TransWikia.com

Tabularx X column doing the opposite of expected and becoming incredibly narrow

TeX - LaTeX Asked by Dbercules on August 7, 2021

I’m a bit of a noob to Latex. I’m using tabularx to create a table of many rows, for which I’m also using ltablex to extend over multiple pages. The below code represents the issue I’m facing, where the column containing the consistently largest amounts of text should expand and force the rest to fit around it is actually being rendered as the narrowest! Any help is appreciated.

usepackage{ltablex}
begin{tabularx}{textwidth}{|l|X|l|l|l|}
hline
 &   & multicolumn{3}{c|}{textbf{TEST}}hline
 textbf{ID} & textbf{CRITERIA}  & textbf{(A)}  & textbf{(B)}   & textbf{(C)}  hline
 multicolumn{5}{|c|}{cellcolor[HTML]{FFFFFF}{color[HTML]{000000} textbf{Category}}}  hline
 #1 & Very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text
 &  Not so long text & Bit longer text than was before, but still short & Usually empty  hline
end{tabularx}

The closest I’ve been able to get is:

begin{tabularx}{textwidth}{|l|L|X|X|X|}

where L is from newcolumntype{L}{>{raggedrightarraybackslash}X}. Source. Could this declaration involving X be causing this issue?

One Answer

If you have

documentclass{article}

usepackage[table]{xcolor}
usepackage{tabularx}

begin{document}

noindent
begin{tabularx}{textwidth}{|l|X|l|l|l|}
hline
 &   & multicolumn{3}{c|}{textbf{TEST}}hline
 textbf{ID} & textbf{CRITERIA}  & textbf{(A)}  & textbf{(B)}   & textbf{(C)}  hline
 multicolumn{5}{|c|}{cellcolor[HTML]{FFFFFF}{color[HTML]{000000} textbf{Category}}}  hline
 #1 & Very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text
 &  Not so long text & Bit longer text than was before, but still short & Usually empty  hline
end{tabularx}
end{document}

then you are specifying that 4 of the columns are set in left aligned single column cells at the natural width of the largest entry in that column. After those widths are determined TeX will then try to make the total width of the table the specified textwidth by adjusting the width of the X columns, and allowing lien breaking within those cells.

You gave no indication of the text width (or how cellcolor was defined) but assuming a standard article and xcolor then the above example produces

enter image description here

You could replace l by L as you suggest in the question or for a hand written document table (as opposed to an automated database dump) it is often better to choose the column widths manualy using p{4cm} or whatever width you want and then just leave one column X to fill out the remaining space if you want the table full width.

enter image description here

documentclass{article}

usepackage[table]{xcolor}
usepackage{tabularx}

begin{document}

begin{center}
setlengthextrarowheight{2pt}
begin{tabularx}{textwidth}{|l|X|>{raggedright}p{2cm}|>{raggedright}p{3cm}|l|}
hline
 &   & multicolumn{3}{|c|}{textbf{TEST}}hline
 textbf{ID} & textbf{CRITERIA}  & textbf{(A)}  & textbf{(B)}   & textbf{(C)}  hline
 multicolumn{5}{|c|}{cellcolor[HTML]{FFFFFF}{color[HTML]{000000} textbf{Category}}}  hline
 #1 & Very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long text
 &  Not so long text & Bit longer text than was before, but still short & Usually empty  hline
end{tabularx}
 
end{center}

end{document}

Correct answer by David Carlisle on August 7, 2021

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