包含parbox的单元格的垂直间距

包含parbox的单元格的垂直间距

本文介绍了包含parbox的单元格的垂直间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的longtable,其中包含多层嵌套表格环境。为了使文本在单元格内换行并使内容在顶部对齐,我使用\parbox [t] [] [t],但是,计算出parbox的高度时没有任何边距,因此以下\hline与文本。

I have a complicated longtable with several levels of nested tabular environments. To get text wrapping inside cells and have the contents aligned at the top I use \parbox[t][][t], however, the height of the parbox is computed without any margin such that the following \hline overlaps with the text.

重现此行为的最小示例是

A minimal example to reproduce this behavior is

\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}

这将产生以下输出(抱歉,无法发布图像):

This produces the following output (sorry, can't post images yet):image of generated output

当然,没有理由使用parbox在上面的示例中,但我在实际文档中需要它们。

Of course, there is no reason to use a parbox in example above, but I need them in the actual document.

我想避免提供parbox的高度(例如\parbox [t] [5cm ] [t])。有没有一种干净的方法可以在parbox的底部或hline之前添加边距?

I would like to avoid providing the height of the parbox (such as \parbox[t][5cm][t]). Is there a clean way to add a margin either to the bottom of a parbox or before an hline?

推荐答案

很抱歉回答我自己的问题,但是我找到了一个解决方案,方法是在parbox的外部的每个单元格中添加vspace。

Sorry to answer my own question, but I have found a solution by adding vspace to each cell outside the 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}

输出:

我之前错过了,因为我在parbox的右花括号和vspace之间没有空格。事实证明,空间至关重要。

I missed that before because I didn't have a space between the closing brace of the parbox and the vspace. Turns out that space is crucial.

这篇关于包含parbox的单元格的垂直间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:15