代码

代码
  • 我保存了一个包含 的盒子\parbox
  • 我使用 TeX 将其他 es 的高度设置\parbox为我保存的框的高度\the\ht\box

代码

由于某种原因,并非所有盒子都是相同的尺寸。

\documentclass{article}
\usepackage{fontspec}

\setlength\fboxsep{0pt}% perfectly size box around content
\setlength\fboxrule{.1pt}

\begin{document}

% Our goal is to specify the height dynamically as a function of the width and content of the box, which we set explicitly here:
\newsavebox{\tmpparbox}
\savebox{\tmpparbox}{\fbox{\parbox{40mm}{This parbox is top aligned with top-aligned content. And more text to fill it up.}}}

% I can use TeX or LaTeX to get the height of the saved box.
%\the\ht\tmpparbox % TeX

%\newlength{\tmpparboxht} % LaTeX
%\settoheight{\tmpparboxht}{\usebox{\tmpparbox}}

Here is the \string\savebox: \usebox{\tmpparbox}
\bigbreak

Target height: \the\ht\tmpparbox

\kern20pt\hrule\kern20pt

This is not a parbox. \fbox{\parbox[t][\the\ht\tmpparbox][t]{40mm}{This parbox is top aligned with top-aligned content. And more text to fill it up.}}
\bigbreak
This is not a parbox. \fbox{\parbox[b][\the\ht\tmpparbox][t]{40mm}{This parbox is bottom aligned with top-aligned content. And more text to fill it up.}}
\bigbreak
This is not a parbox. \fbox{\parbox[t][\the\ht\tmpparbox][c]{40mm}{This parbox is top aligned with center-aligned content. And more text to fill it up.}}
\bigbreak
This is not a parbox. \fbox{\parbox[t][\the\ht\tmpparbox][b]{40mm}{This parbox is top aligned with bottom-aligned content. And more text to fill it up.}}
\bigbreak
This is not a parbox. \fbox{\parbox[c][\the\ht\tmpparbox][t]{40mm}{This parbox is center aligned with top-aligned content. And more text to fill it up.}}
\bigbreak

\end{document}

在此处输入图片描述

答案1

主要错误在于,如您的图片所示,您保存的框是垂直居中的,而不是顶部对齐的,因此使用\ht得到的值大约是所需值的一半。您可以使用\ht加号\dp,但实际上这太多了,因为您需要为高度参数指定的\parbox是文本的目标高度,不包括超出最终基线的最后一行的深度。

因此最好的方法是使用\parbox[b]来保存该框(不要使用,\fbox因为那样会隐藏最后一行实际的文本),然后使用\ht已保存的框。

相关内容