为了允许在表格单元格内换行,我使用\parbox
。但是当我想要换行的单元格位于表格的最后一列时,最好的办法是在文档的边缘处换行。我如何指定 的宽度,以便它\parbox
自动执行此操作,而不需要每次都根据我所在的表格手动指定它?
\documentclass{article}
\newcommand{\breakcell}[1]{\parbox[t]{22em}{#1}}
\begin{document}
\hrule
\begin{tabular}{ll}
A short 1st column & \breakcell{A considerably longer second column where I write way too much stuff}\\
A short 1st column & Blah blah\\
\end{tabular}
\begin{tabular}{ll}
A considerably longer first column & \breakcell{A considerably longer second column where I write way too much stuff}\\
A considerably longer first column & Blah blah\\
\end{tabular}
\hrule
\end{document}
答案1
随着表格型包,可以这样指定表格的宽度,然后使用X
来告诉它哪一列应该可以跨多行拆分。如果将表格的宽度设置为\linewidth
(或\textwidth
),最后一列将自动在文档的边缘拆分。但是,为了使其正常工作,必须在环境前面加上\begin{tabularx}
命令\noindent
,或者从表格的宽度中减去缩进的宽度:
\documentclass{article}
\usepackage{tabularx}
\begin{document}
\hrule
\noindent \begin{tabularx}{\linewidth}{lX}
A short 1st column & A considerably longer second column where I write way too much stuff\\
A short 1st column & Blah blah\\
\end{tabularx}
\newlength{\mylength}
\setlength{\mylength}{\linewidth}
\addtolength{\mylength}{-\parindent}
\begin{tabularx}{\mylength}{lX}
A considerably longer first column & A considerably longer second column where I write way too much stuff\\
A considerably longer first column & Blah blah\\
\end{tabularx}
\hrule
\end{document}
答案2
您能用得起吗linegoal
?
\documentclass{article}
\usepackage{linegoal}
\newcommand{\breakcell}[1]{\parbox[t]{\linegoal}{#1}}
\begin{document}
\hrule
\begin{tabular}{ll}
A short 1st column & \breakcell{A considerably longer second column where I write way too much stuff}\\
A short 1st column & Blah blah\\
\end{tabular}
\begin{tabular}{ll}
A considerably longer first column & \breakcell{A considerably longer second column where I write way too much stuff}\\
A considerably longer first column & Blah blah\\
\end{tabular}
\hrule
\end{document}