在多个表中,如何在可变宽度的列之后再次对齐列?

在多个表中,如何在可变宽度的列之后再次对齐列?

有多个(例如 2 个)表格包含类似信息。它们之间有一个表格行的空白。

所有表格都有 3 列。前两列属于同一列,因此第二列没有标题。\qquad第一列最长的文本与第二列最左边的字符之间应该有一个空格。(可以手动找出最长的文本并手动添加\qquad,如下所示。)

\phantom有没有一种很好的方法来对齐第三列,如下图所示?如果存在一种无需在源中摆弄或完全拆分表格信息的解决方案,那就太好了。

\documentclass{article} 
\usepackage{tabularx}

\begin{document}
\begin{tabular}[t]{@{}l@{}}
\begin{tabular}[t]{@{}l@{}p{5cm}@{}|>{\raggedleft}p{1.5cm}@{}}
\textbf{Col1}            &                  & \textbf{Col3} \tabularnewline
Text1                    & Text1Description & 0.1  \tabularnewline
VerylongText2\qquad\null & Text2Description & 3    \tabularnewline
Text3                    & Text3Description & 2    \tabularnewline
\end{tabular}
\\
\\
\begin{tabular}[t]{@{}l@{}p{5cm}@{}|>{\raggedleft}p{1.5cm}@{}}
\textbf{Col1}            &                  & \textbf{Col3} \tabularnewline
Text4                    & Text4Description & 3    \tabularnewline
LongText5\qquad\null     & Text5Description & 0.2  \tabularnewline
Text6                    & Text6Description & 0.34 \tabularnewline
\end{tabular}
\end{tabular}
\end{document}

在此处输入图片描述

答案1

在此处输入图片描述

\documentclass{article} 


\begin{document}
\begin{tabular}[t]{@{}l|l@{}}
\begin{tabular}[t]{@{}l@{\quad}p{5cm}@{}}
\textbf{Col1}            &                  \tabularnewline
Text1                    & Text1Description \tabularnewline
VerylongText2            & Text2Description \tabularnewline
Text3                    & Text3Description 
\end{tabular}
&\begin{tabular}[t]{@{}l@{}}                             
\textbf{Col3} \tabularnewline  
0.1  \tabularnewline           
3    \tabularnewline           
2
\end{tabular}\\
\multicolumn{2}{c}{}\\
\begin{tabular}[t]{@{}l@{\quad}p{5cm}@{}}
\textbf{Col1}            &                  \tabularnewline
Text4                    & Text4Description \tabularnewline
LongText5                & Text5Description \tabularnewline
Text6                    & Text6Description 
\end{tabular}
&\begin{tabular}[t]{@{}l@{}}                             
 \textbf{Col3} \tabularnewline   
 3    \tabularnewline            
 0.2  \tabularnewline            
 0.34 
\end{tabular}
\end{tabular}
\end{document}

或者使用更简单的标记,但需要运行几次才能稳定下来:

在此处输入图片描述

\documentclass{article} 
\usepackage{array}
\makeatletter
\def\zz#1#2{%
\pdfsavepos\write\@auxout{\gdef\string#1{\the\pdflastxpos sp}}%
\ifx\@undefined#1%
\else
\ifx\@undefined#2%
\else
\ifdim#2<#1\relax
\kern\dimexpr#2-#1\relax
\fi
\fi
\fi}


\makeatother

\begin{document}

\begin{tabular}[t]{@{}l@{\qquad}p{5cm}@{\zz\za\zb}|p{1.5cm}@{}}
\textbf{Col1}     &                  & \textbf{Col3} \tabularnewline
Text1             & Text1Description & 0.1  \tabularnewline
VerylongText2     & Text2Description & 3    \tabularnewline
Text3             & Text3Description & 2    \tabularnewline
\end{tabular}

\vspace*{\baselineskip}
\begin{tabular}[t]{@{}l@{\qquad}p{5cm}@{\zz\zb\za}|p{1.5cm}@{}}
\textbf{Col1}     &                  & \textbf{Col3} \tabularnewline
Text4             & Text4Description & 3    \tabularnewline
LongText5         & Text5Description & 0.2  \tabularnewline
Text6             & Text6Description & 0.34 \tabularnewline
\end{tabular}

\end{document}

答案2

或者,也可以使用包\widthof中的选项。(当我在问题中calc提到“摆弄”时,我实际上想到的就是这个。)\phantom

在此处输入图片描述

\documentclass{article} 
\usepackage{tabularx}
\usepackage{calc}

\begin{document}
\begin{tabular}[t]{@{}l@{}}
\begin{tabular}[t]{@{}l@{\qquad}p{5cm}@{}|>{\raggedleft}p{1.5cm}@{}}
\textbf{Col1}     &                  & \textbf{Col3} \tabularnewline
Text1             & Text1Description & 0.1  \tabularnewline
VerylongText2     & Text2Description & 3    \tabularnewline
Text3             & Text3Description & 2    \tabularnewline
\end{tabular}
\\
\\
\begin{tabular}[t]{@{}l@{\qquad}p{5cm+\widthof{VerylongText2}-\widthof{LongText5}}@{}|>{\raggedleft}p{1.5cm}@{}}
\textbf{Col1}     &                  & \textbf{Col3} \tabularnewline
Text4             & Text4Description & 3    \tabularnewline
LongText5         & Text5Description & 0.2  \tabularnewline
Text6             & Text6Description & 0.34 \tabularnewline
\end{tabular}
\end{tabular}
\end{document}

相关内容