[Tex/LaTex] Is it possible to use \newsavebox with more accurate bounding box

boxes

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}

enter image description here

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}

enter image description here

Note: The top and bottom parts get cropped.

Questions:

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

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

Result