1

I am reading user generated data and am trying to automate the layout. I would like the tables to stretch the width of the page, hence the use of tabularx. Further there may be times when some tables need to be broken over two pages, hence I need longtable. Then there was a problem for which this answer helped by leading me towards ltablex. But once again there is a problem.

The MWE below demonstrates my problem.

Using just tabularx everything looks nice, but switching to ltablex causes the short table to be short in width as shown below. enter image description here

Question: How can I have the best of both tabularx and longtable?

\documentclass{article}
% \usepackage{tabularx} % with this, everything is ok unless there is a table that stretches over two pages
\usepackage{ltablex} % with this, the first table doesn't stretch and is not left aligned

\begin{document}

\begin{tabularx}{\textwidth}{rrX} \hline col1 & col2 & col3 \ \hline 3 & 6 & short comment\ \hline \end{tabularx}

\begin{tabularx}{\textwidth}{rrX} \hline col1 & col2 & col3 \ \hline 3 & 6 & some long comment that should stretch to at least the edge of the page so that it wraps back to a second line. \ \hline \end{tabularx}

\end{document}

Geoff
  • 2,637

2 Answers2

1

I will give an alternative solution with tabularray package:

\documentclass{article}

\usepackage[a6paper,margin=15mm]{geometry} \usepackage{tabularray}

\begin{document}

\begin{longtblr}[ caption = {Long Title}, ]{ colspec = {rrX}, rowhead = 1, } \hline col1 & col2 & col3 \ \hline 3 & 6 & some long comment that should stretch to at least the edge of the page so that it wraps back to a second line. \ \hline 3 & 6 & some long comment that should stretch to at least the edge of the page so that it wraps back to a second line. \ \hline 3 & 6 & some long comment that should stretch to at least the edge of the page so that it wraps back to a second line. \ \hline 3 & 6 & some long comment that should stretch to at least the edge of the page so that it wraps back to a second line. \ \hline 3 & 6 & some long comment that should stretch to at least the edge of the page so that it wraps back to a second line. \ \hline 3 & 6 & some long comment that should stretch to at least the edge of the page so that it wraps back to a second line. \ \hline 3 & 6 & some long comment that should stretch to at least the edge of the page so that it wraps back to a second line. \ \hline 3 & 6 & some long comment that should stretch to at least the edge of the page so that it wraps back to a second line. \ \hline 3 & 6 & some long comment that should stretch to at least the edge of the page so that it wraps back to a second line. \ \hline 3 & 6 & some long comment that should stretch to at least the edge of the page so that it wraps back to a second line. \ \hline \end{longtblr}

\end{document}

enter image description here

L.J.R.
  • 10,932
  • -- is there a rotate option in tabularray for text in a single cell -- I could not find it in the documentation – js bibra Sep 23 '21 at 01:36
  • There is no rotate option at this time, but it is in the todo list: https://github.com/lvjr/tabularray/issues/98 – L.J.R. Sep 23 '21 at 02:07
0

Solution with use of the tabularraypackageis very elegant(+1), but your problem can be solved also with insertion of \keepXColumns in preamble of your document:

\documentclass{article}
\usepackage{ltablex} 
\keepXColumns  % <----

\begin{document}

\begin{tabularx}{\textwidth}{rrX} \hline col1 & col2 & col3 \ \hline 3 & 6 & short comment\ \hline \end{tabularx}

\begin{tabularx}{\textwidth}{rrX} \hline col1 & col2 & col3 \ \hline 3 & 6 & some long comment that should stretch to at least the edge of the page so that it wraps back to a second line. \ \hline \end{tabularx}

Above MWE produce

enter image description here

Zarko
  • 296,517