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}
2
  • 2
    You should use the xltabular environment.
    – Bernard
    Commented Apr 29, 2021 at 13:27
  • Indeed this seems to solve the problem. Many thanks.
    – Geoff
    Commented Apr 29, 2021 at 13:49

2 Answers 2

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

2
  • -- is there a rotate option in tabularray for text in a single cell -- I could not find it in the documentation
    – js bibra
    Commented Sep 23, 2021 at 1:36
  • There is no rotate option at this time, but it is in the todo list: github.com/lvjr/tabularray/issues/98
    – L.J.R.
    Commented Sep 23, 2021 at 2: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

You must log in to answer this question.

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