![如何充分对齐表格列](https://linux22.com/image/298879/%E5%A6%82%E4%BD%95%E5%85%85%E5%88%86%E5%AF%B9%E9%BD%90%E8%A1%A8%E6%A0%BC%E5%88%97.png)
我想要一个tabular
具有完全对齐列的环境。我猜想如何实现这一点是使用一个tabularx
环境(这样表格就知道宽度),然后在所有相邻列之间放置\hilll
(以适当重复字母l
)。但是,这行不通。请参阅下面的 MWE。
\documentclass{article}
\usepackage{tabularx}
\begin{document}
Text before. Text before. Text before. Text before. Text before. Text before. Text before.
\begin{tabularx}{\textwidth}{l@{\hfill}c@{\hfill}r}
On the LEFT & In the middle & On the RIGHT
\end{tabularx}
Text between. Text between. Text between. Text between. Text between. Text between. Text between.
\def\magicNumber{50pt}
\begin{tabularx}{\textwidth}{lcr}
On the LEFT & \hspace*{\magicNumber} In the middle \hspace*{\magicNumber} & On the RIGHT
\end{tabularx}
Text after. Text after. Text after. Text after. Text after. Text after. Text after. Text after.
\end{document}
第一个tabularx
环境是我的失败尝试。第二个tabularx
环境排版(大致)正确,但使用魔法数字是一种黑客行为。
问题:
如何正确使用无限粘连(如\hfill
)来指定列之间的空间tabularx
?更一般地说,如何获得tabular
具有完全对齐列的环境?
答案1
tabularx
仅在使用 -column 时才有效X
。您感兴趣的可能是设置\extracolsep{\fill}
,如表格中的列和行填充:
\documentclass{article}
\begin{document}
Text before. Text before. Text before. Text before. Text before. Text before. Text before.
\noindent
\begin{tabular*}{\linewidth}{@{}@{\extracolsep{\fill}}lcr@{}}
On the LEFT & In the MIDDLE & On the RIGHT
\end{tabular*}
Text after. Text after. Text after. Text after. Text after. Text after. Text after. Text after.
\noindent
\begin{tabular*}{\linewidth}{@{}@{\extracolsep{\fill}}lcr@{}}
On the LEFT & In the very MIDDLE & On the RIGHT
\end{tabular*}
\end{document}
请注意,以上内容并不代表“完全对齐”的列。为此,您可以使用
\documentclass{article}
\usepackage{tabularx}
\begin{document}
Text before. Text before. Text before. Text before. Text before. Text before. Text before.
\noindent
\begin{tabularx}{\linewidth}{@{}XXX@{}}
On the LEFT & In the MIDDLE & On the RIGHT
\end{tabularx}
Text after. Text after. Text after. Text after. Text after. Text after. Text after. Text after.
\end{document}
如果需要,您可以使用array
包裹。