How to get the height of lrbox

I need to know the height of lrbox before I will be using it in my code. For some reason, my code is getting me odd non-correct value.

\documentclass{article}
\usepackage[utf8]{inputenc}

\begin{document}

\newsavebox{\jhbin}
\begin{lrbox}{\jhbin}
\par\begin{tabular}{c}
Jim Hef{}feron\\
Many lines is\\ comming.
\end{tabular}
\end{lrbox}

\newlength{\heightofhw}
\settoheight{\heightofhw}{\hbox{\jhbin}}
Wrong Height = \the\heightofhw

\usebox{\jhbin}

\end{document}


Any idea how to get it right?

A \par inside lrbox does nothing at all.

Why is the height “wrong”? Because you're not measuring it. You're just measuring the height of some random character.

If you look in the log file, you'll see the line \jhbin=\box46.

What happens if you try \hbox{\jhbin}? You get a period. Why? Because ASCII 46 is the period. If you define other save boxes, you'd get a different character.

For implementation reasons, \newsavebox{\jhbin} actually stores in \jhbin a number; if used alone, this will print the character with that number in the current font.

You could do \settoheight{\heightofhw}{\usebox{\jhbin}}, but perhaps you don't want this, but rather the real height of the box. A tabular, by default, is typeset half above the baseline, half below it (actually a bit more than half above the baseline).

\begin{lrbox}{\jhbin}
\begin{tabular}[b]{c}
Jim Hef{}feron\\
Many lines is\\ comming.
\end{tabular}
\end{lrbox}

\settoheight{\heightofhw}{\usebox{\jhbin}}
Right Height = \the\heightofhw


you get

Right Height = 32.39996pt

because with the [b] option the tabular is set with its bottom row sitting on the baseline. Of course

\setlength{\heightofhw}{\ht\jhbin}


is simpler.

This is however not the full vertical dimension of the tabular, because of the “g” in the bottom line. You can get the full vertical size with

\setlength{\heightofhw}{\dimexpr\ht\jhbin+\dp\jhbin\relax}


and this would give 36pt (because the tabular has three rows).

• They should add a \settototalheight to the kernel... May 22 '20 at 22:59

Why do you put the box in a \hbox? The \hbox obviously "swallows" the height. The height of a box can be retrieved with \ht. \settoheight uses this internally, and there is no need to use it if you already have a box. So the upshot is that you only need

\heightofhw=\ht\jhbin


as in

\documentclass{article}
\usepackage[utf8]{inputenc}

\begin{document}

\newsavebox{\jhbin}
\begin{lrbox}{\jhbin}
\par\begin{tabular}{c}
Jim Hef{}feron\\
Many lines is\\ comming.
\end{tabular}
\end{lrbox}
\newlength{\heightofhw}
\heightofhw=\ht\jhbin
Height = \the\heightofhw

\usebox{\jhbin}

\end{document}