通过 ltablex 结合 tabularx 和 longtable

通过 ltablex 结合 tabularx 和 longtable

我正在读取用户生成的数据,并尝试自动化布局。我希望表格能够拉伸页面宽度,因此使用tabularx。此外,有时可能需要将某些表格分成两页,因此我需要longtable。然后出现了一个问题,答案帮助我找到了ltablex。但问题又出现了。

下面的 MWE 演示了我的问题。

仅使用tabularx一切看起来都不错,但切换到ltablex会导致短表的宽度变短,如下所示。在此处输入图片描述

tabularx问题: 我怎样才能同时获得和的最佳效果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}

答案1

我将提供另一种解决方案tabularray包裹:

\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}

在此处输入图片描述

答案2

使用包的解决方案非常优雅(+1),但您的问题也可以通过在文档序言中tabularray插入来解决:\keepXColumns

\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}

高于 MWE 产生

在此处输入图片描述

相关内容