# How do I find the bounding box that surrounds various Tex elements

Given a tex file like this :

\documentclass{article}
\usepackage{lipsum}

\begin{document}
\lipsum[1]
\end{document}


I'm unable to figure out a way to log the bounding box co-ordinates (x, y, width, height; as shown in the figure) of this paragraph into a log file (say, using \pdfsavepos)?

• do you want to measure a boxed thing or (harder) measure the final position of material on the main vertical list that is penitentially liable for page breaking? It's not clear as your image is not generated by the posted code. – David Carlisle May 30 at 21:40
• @DavidCarlisle My code merely generates the text (without the box). The bounding-box was just an illustration. I want to be able to figure out the extent of the "tightest" bounding-box given the text. A typical document can have latex, figures, text, lists, etc. Is there a way to figure out the rectangular extent of each of these elements? – crazydiamond May 30 at 21:58
• @crazydiamond it is rather hard to do that without affecting the spacing, latex never normally constructs any object that represent those things, lists, paragraphs etc are just ephemeral things that generate lines of text, they do not correspond to any measurable object. that's why I asked about the box, if you have an \fbox or a fancier shaded box as in your image, then you can easily get tex to report its size. – David Carlisle May 30 at 22:05
• If all you want is the width, height and depth, you can use a savebox. – John Kormylo May 31 at 3:19

You can record the start and end points (on the baseline) and then make adjustments for the height and depth of a normal line

\documentclass{article}
\usepackage{lipsum}

\begin{document}

\makeatletter

\mbox{}\pdfsavepos\write\@auxout{\gdef\string\Ax{\the\pdflastxpos}\gdef\string\Ay{\the\pdflastypos}}%
\lipsum[1]%
\pdfsavepos\write\@auxout{\gdef\string\Bx{\the\pdflastxpos}\gdef\string\By{\the\pdflastypos}}

\ifx\Ax\@undefined\else

\typeout{Bounding box:
\the\dimexpr \Ax sp -\parindent\relax\space \the\dimexpr\By sp - \dp\strutbox\relax
\space
\the\dimexpr \Ax sp -\parindent+\textwidth\relax\space \the\dimexpr\Ay sp + \ht\strutbox\relax
}

\fi
\end{document}


I get

 Bounding box: 134.26999pt 574.17682pt 479.26999pt 718.17682pt


this assumes that the text doesn't break over a page (in which case you would need to do something more complicated)

• Thank you. This snippet of code is a good place for me to start. – crazydiamond May 31 at 2:50