2

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}{>{\raggedright\arraybackslash}X}. Source. Could this declaration involving X be causing this issue?

Dbercules
  • 123
  • columns 1,3,4,5 are l so they are set their natural width on one line with no linebreaking, then any remaining space is allocated to the Xcolumns (or column here) if there is not much space left, it will be narrow. As you have not provided a usable example we can not test this or see how wide is the spae text width so hard to make any real suggestions for change. – David Carlisle Jan 31 '21 at 22:57

1 Answers1

2

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} \setlength\extrarowheight{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}

David Carlisle
  • 757,742
  • I apologise for the non-usable example given - this is the second of two times in my life that I've had to engage with Latex, and I usually spend my time on StackOverflow so excuse my warped mind! This is perfect - definitely a technique I'll look to use again. – Dbercules Jan 31 '21 at 23:28
  • 1
    @Dbercules well I was a bit hard on you it wasn't exactly hard for me to guess how to define \cellcolor (as I wrote it) but in general a working example is good (and means you don't miss things like missing \ in \end{tabularx} – David Carlisle Jan 31 '21 at 23:30