0
  • I save a box containing a \parbox.
  • I set height of other \parboxes to the height of my saved box using TeX \the\ht\box

Code

For some reason, not all boxes are of the same size.

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

enter image description here

3
  • your first box says it is top aligned but it is vertically centred and you are just taking the height not the depth, so around half its vertical extent. Commented Oct 8, 2018 at 12:52
  • @DavidCarlisle Ah, so I need both height and depth. Commented Oct 8, 2018 at 12:54
  • yes and then probably subtract boxmaxdepth, the inner height argument to parbox doesn't directly relate to either the height or height+depth of the resulting box, just as \textheight doesn't relate to the height+depth of a page as by design the depth is ignored so you get even bottom baselines. Commented Oct 8, 2018 at 12:56

1 Answer 1

1

The main error is that , as your image shows, the box you save is vertically centred not top aligned so by using \ht you are getting around half the required value. You could use \ht plus \dp but that would in fact be too much as what you need to specify for the height argument to \parbox is the target height of the text, not including the depth of the final row beyond the final baseline.

So best would be to use a \parbox[b] to save the box (and don't use \fbox as that would hide the last real row of text) and then use \ht of the saved box.

2
  • Could you please explain the first part about \ht and my box being vertically centered and not top aligned? Why is \ht about half? Just want to make sure I know what is being measured here... Commented Oct 9, 2018 at 6:08
  • Side note, if I use a new dimen register, \boxsize=\dimexpr\the\ht\templatexbox+\dp\templatexbox\relax, and feed that into my \parbox[b]{\boxsize}{...}, the \fbox surrounds the dips in letters like "p". (I took \fbox out of the \savebox) Commented Oct 9, 2018 at 6:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .