# Is it possible to use \newsavebox with more accurate bounding box?

I have 2 cases here. First case, I want to set the paper size to the size of $\displaystyle E=mc^2$ and second case it is set to the size of \fbox{$\displaystyle E=mc^2$}.

Unfortunately, in both cases I got outputs that got cropped at the top and bottom.

Note: The gray frame in the following screenshot is not a part of my drawing. Instead it is the background color of Adobe Reader X.

## Case 1: Without using \fbox

\documentclass[cmyk]{minimal}

\usepackage{pstricks}

\newsavebox\IBox
\savebox\IBox{$\displaystyle E=mc^2$}

\newdimen\mywidth
\newdimen\myheight
\mywidth=\wd\IBox
\myheight=\ht\IBox

\paperwidth=\mywidth
\paperheight=\myheight
\voffset=-1in
\hoffset=-1in
\topskip=0bp

\special{papersize=\the\mywidth,\the\myheight}

\parindent=0cm
\pagecolor{cyan}

\begin{document}
\begin{pspicture}(\mywidth,\myheight)
\psframe[linecolor=red](\mywidth,\myheight)
\rput[lb](0,0){\usebox{\IBox}}
\end{pspicture}
\end{document}


Note: The top and bottom parts get cropped.

## Case 2: Using \fbox

\documentclass[cmyk]{minimal}

\usepackage{pstricks}

\newsavebox\IBox
\savebox\IBox{\fbox{$\displaystyle E=mc^2$}}

\newdimen\mywidth
\newdimen\myheight
\mywidth=\wd\IBox
\myheight=\ht\IBox

\paperwidth=\mywidth
\paperheight=\myheight
\voffset=-1in
\hoffset=-1in
\topskip=0bp

\special{papersize=\the\mywidth,\the\myheight}

\parindent=0cm
\pagecolor{cyan}

\begin{document}
\begin{pspicture}(\mywidth,\myheight)
%\psframe[linecolor=red](\mywidth,\myheight)
\rput(0.5\mywidth,0.5\myheight){\usebox{\IBox}}
\end{pspicture}
\end{document}


Note: The top and bottom parts get cropped.

## Questions:

How to increase the accuracy of \newsavebox? Is it possible?

-
BTW: you can also set the background of Adobe Acrobat Reader to white for your screenshots, if you want. –  Martin Scharrer Jul 13 '11 at 10:35
Oh, you are right. I thought it works also for normal mode. Sorry. –  Martin Scharrer Jul 13 '11 at 10:51

## 1 Answer

The savebox dimensions are as accurate as they get. The issue is caused because you only use the height not the total height (= height + depth) of the box. Any box as a width, a height and a depth (the amount it goes below the baseline). You are setting the page size to its height only so cropping off an amount equal of the depth. Because you center the box then vertically and horizontally the cropping affects top and bottom.

You can fix this by adding either \dp\IBox to \myheight (which might require adjustments to the vertical centering, because it might be baseline specific) or simply ensure that there is no depth by using e.g. \raisebox:

## Second Case:

For your second case simply do:

\savebox\IBox{\raisebox{\depth}{\fbox{$\displaystyle E=mc^2$}}}


A more low level way to push the depth to the height would be:

\savebox\IBox{...}
\sbox\IBox{\raise\dp\IBox\box\IBox}


This has the benefit that you can use the lrbox environment instead of \savebox to allow verbatim or other special material which doesn't allow an macro wrapped around it.

## First Case:

For your first case you still have to plan in the rule width and the separation (as \fbox does), which should be added to \mywidth and \myheight. You should then center the box vertically and horizontally.

Alternatievly add the extra height and width manually:

\documentclass[cmyk]{minimal}

\usepackage{pstricks}

\newsavebox\IBox

\newlength\mymargin
\setlength\mymargin{2pt}
\savebox\IBox{%
\raisebox{\dimexpr\depth+\mymargin\relax}
[\dimexpr\totalheight+2\mymargin\relax]{\hspace\mymargin$\displaystyle E=mc^2$\hspace\mymargin}}

\newdimen\mywidth
\newdimen\myheight
\mywidth=\wd\IBox
\myheight=\ht\IBox

\paperwidth=\mywidth
\paperheight=\myheight
\voffset=-1in
\hoffset=-1in
\topskip=0bp

\special{papersize=\the\mywidth,\the\myheight}

\parindent=0cm
\pagecolor{cyan}

\begin{document}
\begin{pspicture}(\mywidth,\myheight)
\psframe[linecolor=red](\mywidth,\myheight)
\rput[lb](0,0){\usebox{\IBox}}
\end{pspicture}
\end{document}


-
@xport: If it is about the overstanding elements of the letters than this only indirectly helps. –  Martin Scharrer Jul 13 '11 at 10:52
@xport: Just set the margin to zero then or remove the code which adds it. I thought you need the margin. Otherwise \psframe will draw inside the original box, not around it like \fbox does. You need to set at least a small margin or use pdfcrop or use gs -sDEVICE=bbox directly or (even more work) read the font paths out directly. –  Martin Scharrer Jul 13 '11 at 11:26
@xport: \fbox does basically the same. –  Martin Scharrer Jul 13 '11 at 11:30
@xport: Ok, I thought you want to have frame. My basic answer then stays: You forgot to take the depth into account! The second issue that some small overhang which isn't inside the official bounding box can't be fixed by TeX directly. I suggest adding a small margin (not 2pt, but 0.1pt or something) to compensate for this. –  Martin Scharrer Jul 13 '11 at 11:44
@xport: The \hspace\mymargin belongs indeed direct after the \$ but also works after the }. In this case its not part of the \raisexbox which doesn't hurt. –  Martin Scharrer Jul 13 '11 at 11:46