我有一个复杂的长表,其中包含多个级别的嵌套表格环境。为了让文本在单元格内换行并使内容在顶部对齐,我使用\parbox[t][][t],但是,计算 parbox 的高度时没有任何边距,因此以下\hline 与文本重叠。
重现此行为的最小示例是
\documentclass{article}
\begin{document}
\begin{tabular} {|p{0.2\textwidth}|}
\hline
This cell looks good. \\
\hline
\parbox[t][][t]{1.0\linewidth}{
Not so happy with this.
} \\
\hline
\end{tabular}
\end{document}
这会产生以下输出(抱歉,还不能发布图片):
image of generated output
当然,在上面的例子中没有理由使用parbox,但我在实际文档中需要它们。
我想避免提供 parbox 的高度(例如\parbox[t][5cm][t])。有没有一种干净的方法可以在 parbox 的底部或 hline 之前添加边距?
最佳答案
很抱歉回答我自己的问题,但我找到了一个解决方案,将 vspace 添加到 parbox 外的每个单元格。
这是代码:
\documentclass{article}
\begin{document}
\newcommand{\pb}[1]{\parbox[t][][t]{1.0\linewidth}{#1} \vspace{-2pt}}
\begin{tabular} {|p{0.2\textwidth}|}
\hline
This cell looks good. \\
\hline
\pb{
Now I'm happy with this.
} \\
\hline
\end{tabular}
\end{document}
输出:image of generated output
我之前错过了,因为我在 parbox 的右大括号和 vspace 之间没有空格。事实证明,空间至关重要。
关于height - 包含 parbox 的单元格的垂直间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18100186/