问题
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
Of course, there is no reason to use a parbox in example above, but I need them in the actual document.
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?
回答1:
Sorry to answer my own question, but I have found a solution by adding vspace to each cell outside the parbox.
Here's the code:
\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}
The output: image of generated output
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.
来源:https://stackoverflow.com/questions/18100186/vertical-spacing-of-cells-containing-a-parbox