Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Consider the following code:

\documentclass[a4paper]{letter}
\usepackage{calc}
\usepackage{printlen}

\newsavebox{\foobox}
\newlength{\fooboxheight}

\savebox{\foobox}{
    \parbox[t]{\textwidth}{Line 1\\Line 2\\Line 3\\Line 4}
}
\settoheight{\fooboxheight}{\usebox{\foobox}}

\begin{document}

\usebox{\foobox}


Address box height: \printlength{\fooboxheight} \uselengthunit{mm} \printlength{\fooboxheight}

\end{document}

\fooboxheight is 7pt / 2mm, much smaller than the height of the \parbox! Why is this? What should I do to get the real height of the \parbox?

Interestingly, if the \parbox is surrounded by a \frame, everything works.

share|improve this question
add comment

3 Answers

up vote 14 down vote accepted

Insert the lines

\showthe\ht\foobox
\showthe\dp\foobox

and all will be clear: The height of \foobox is indeed 6.83331pt just as you have found. The depth, on the other hand, is 36pt. Notice that boxes have height and depth both, as measured from the baseline of the box. In this case, this happens because you use \parbox[t]. If you switch to \parbox[b] things might work more like you expect, at least as far as the height calculation goes. But then, of course, the baseline of the box may not be where you need it to be.

share|improve this answer
add comment

The calc package also provides the commands \totalheightof and \settototalheight, "totalheight" being the sum of height and depth.

share|improve this answer
add comment

It's because of the [t] option. It's sets the official parbox baseline to the baseline of the first line. Therefore the height of the box is the height of the first line. The rest is now part of the official depth.

What you need is the so called totalheight which is height+width. There is no primitive for this, therefore there isn't a \settototalheight. However, if you already have the material inside a savebox you don't need to use these macros anyway, but can use \ht, \dp and \wd to get the height, depth and width of the saved box.

The totalheight can be calculated using the 'calc' package:

\usepackage{calc}
%...
\setlength{\fooboxheight}{\ht\foobox+\dp\foobox}

or using the eTeX primitive \dimexpr:

\setlength{\fooboxheight}{\dimexpr\ht\foobox+\dp\foobox}
share|improve this answer
    
Actually, the calc package defines \settototalheight. –  lockstep Feb 24 '11 at 15:01
    
@lockstep: Thanks, I didn't know that. I rarely use it since I know about the eTeX primitives and pgfmath. –  Martin Scharrer Feb 24 '11 at 15:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.