在此示例中
\documentclass{article}
\usepackage{tabularx}
\newlength{\streetlength}
\settowidth{\streetlength}{Apple street 16-214, Great City, Big Country}
\begin{document}
\begin{center}
Apple street 16-214, Great City, Big Country\\
\noindent
\begin{tabularx}{\streetlength}{Xr}
Ph & 00 322 22299988\\
E-mail & [email protected]
\end{tabularx}
\end{center}
\end{document}
我尝试将表格的宽度设置为等于前一行的长度。
从输出
显然,解决方案必须改进。如何调整解决方案?
答案1
如果在左右添加垂直线,您将看到宽度正好是想要的...如果您希望字母对齐,则必须删除由 \tabcolsep 长度产生的边距:
\documentclass{article}
\usepackage{tabularx}
\newlength{\streetlength}
\begin{document}
\settowidth{\streetlength}{Apple street 16-214, Great City, Big Country}
\begin{center}
Apple street 16-214, Great City, Big Country\\
\noindent
\begin{tabularx}{\streetlength}{|Xr|}
Ph & 00 322 22299988\\
E-mail & [email protected]
\end{tabularx}
\end{center}
\setlength\tabcolsep{0pt}
\begin{center}
Apple street 16-214, Great City, Big Country\\
\noindent
\begin{tabularx}{\streetlength}{Xr}
Ph & 00 322 22299988\\
E-mail & [email protected]
\end{tabularx}
\end{center}
\end{document}
输出:
答案2
如果您知道最宽的元素,则无需测量长度:
\documentclass{article}
\begin{document}
\setlength{\tabcolsep}{0pt}
\begin{tabular}{ r l l }
& Apple street 16-214, Great City, Big Country & \\
\rlap{Ph} & & \llap{00 322 22299988} \\
\rlap{E-mail} & & \llap{[email protected]}
\end{tabular}
\end{document}
我们在左侧和右侧的附加列中设置了较短的长度,并在lap
进入中间的自然宽度l
列。
答案3
您还可以@{}
在第一列之前和最后一列之后使用。
\documentclass{article}
\usepackage{tabularx}
\newlength{\streetlength}
\settowidth{\streetlength}{Apple street 16-214, Great City, Big Country}
\begin{document}
\begin{center}
Apple street 16-214, Great City, Big Country\\
\noindent
\begin{tabularx}{\streetlength}{@{}Xr@{}}
Ph & 00 322 22299988\\
E-mail & [email protected]
\end{tabularx}
\end{center}
\end{document}