对齐多个表中的列

对齐多个表中的列

我正在尝试对齐多个表的列。我知道我可以使用p列格式来固定列宽,但我希望 TeX 自动帮我计算出宽度。我希望:

  1. 输出一个表,其中包含每个后续表的所有行,以便组合表的列宽将自动适用于所有后续表
  2. 然后使用相同的设置输出每个后续表,并
  3. 找出某种方法,使得第一个组合表可以生成但不可见。

这是我的尝试。第一个组合表用于\savetabu保存设置,然后每个后续表用于\usetabu检索它们。不幸的是,这并不像我希望的那样工作。列没有排列,如下面的输出(代码之后)所示。有人有实现此目的的方法吗?

\documentclass{article}

\usepackage{tabu}
\begin{document}

Output both tables together as one table and then output each
individual table.  We had hoped that this would cause the subsequent
tables to be the same width as the first since the first contains all
columns of the subsequent tables but it seems that that does not work.

\begin{center}
\begin{tabu}{ll}\savetabu{mytabu2}
1 & 2 \\
3 & 4 \\
abcdefghijklmnopqrstuvwxyz & 10 \\
5 & 6 \\
\end{tabu}
\end{center}

\begin{center}
\begin{tabu}{\usetabu{mytabu2}}
1 & 2 \\
3 & 4 \\
\end{tabu}
\end{center}

\begin{center}
\begin{tabu}{\usetabu{mytabu2}}
abcdefghijklmnopqrstuvwxyz & 10 \\
5 & 6 \\
\end{tabu}
\end{center}

\end{document}

输出如下:

截屏

答案1

您所描述的基本上是longtable内部所做的事情,以便表的各个部分排列起来,因此使用 longtable 相当容易,但重新定义事物以便两个表共享 longtable 宽度数据:

在此处输入图片描述

\documentclass{article}

\usepackage{longtable}
\makeatletter
\def\dontchopLT{\let\LT@entry@chop\relax}
\def\resumeLT{%
\let\zzz\stepcounter
\def\yyy{LT@tables}
\def\stepcounter##1{%
  \def\xxx{##1}%
  \ifx\xxx\yyy\else\zzz{##1}\fi}}
\makeatother

\begin{document}

Output both tables together as one table and then output each
individual table.  We had hoped that this would cause the subsequent
tables to be the same width as the first since the first contains all
columns of the subsequent tables but it seems that that does not work.




\noindent X\dotfill X

{\dontchopLT
\begin{longtable}{ll}
1 & 2 \\
3 & 4 \\
\end{longtable}}

\noindent X\dotfill X





{\resumeLT
\begin{longtable}{ll}
abcdefghijklmnopqrstuvwxyz & 10 \\
5 & 6 \\
\end{longtable}}

\end{document}

答案2

环境tabu将需要使用 X 列以便能够缩放它们并记住计算savetabu

由于您不希望表格占据整个行宽,因此可以使用spread值为 a 的定义0pt并让X列具有负系数:

\begin{tabu} spread 0pt  {|X[-1]|X[-1]|}\savetabu{mytabu2}
1 & 2 \\
3 & 4 \\
abcdefghijklmnopqrstuvwxyz & 10 \\
5 & 6 \\
\end{tabu}
\end{center}

\begin{center}
\begin{tabu}{\usetabu{mytabu2}}
1 & 2 \\
3 & 4 \\
\end{tabu}
\end{center}

\begin{center}
\begin{tabu}{\usetabu{mytabu2}}
abcdefghijklmnopqrstuvwxyz & 10 \\
5 & 6 \\
\end{tabu}

在此处输入图片描述

相关内容