# What kind of “magic” does preview package do to produce a tight paper size?

Actually the following method, that looks like reinventing the wheel, has been discarded once I have known the existence of preview package.

% WithoutPreview.tex
\documentclass{article}
\usepackage{pstricks}

\newcommand\All{0.1}
\newcommand\Left{-\All}
\newcommand\Right{\All}
\newcommand\Bottom{-\All}
\newcommand\Top{\All}

%%%%%%%%%%%% BEGIN SETTINGS %%%%%%%%%%%%
\parindent=0pt
\topskip=0pt

\topmargin=-72.27pt
\oddsidemargin=-72.27pt
\paperwidth=\dimexpr\Right\psxunit-\Left\psxunit\relax
\paperheight=\dimexpr\Top\psyunit-\Bottom\psyunit\relax

\special{papersize=\the\paperwidth,\the\paperheight}
\pagestyle{empty}

\begin{document}
\begin{pspicture}(\Left,\Bottom)(\Right,\Top)
\psframe[linewidth=0.01](\Left,\Bottom)(\Right,\Top)
\end{pspicture}
\end{document}


and when compiled with the same batch file, batch.bat, it produces a tight output as follows.

Note: The random white spaces have gone!

The bad news came yesterday as I cannot use preview package together with PSTricks related packages and animate package.

## Questions

Does preview invoke pdfcrop behind the scene? If preview does not invoke pdfcrop, what kind of magic does it do to produce a tight paper size? I am interested in extracting the fragment codes, that is used in preview to produce a tight paper size, and insert them into my WithoutPreview.tex.

Note: xelatex can give better output than latex-dvips-ps2pdf does, but xelatex runs too slowly.

Another note: preview package ignores \pagecolor{<non-white color>} declared in the preamble. However, it is not a big issue as the non-white background can be obtained by a PSTricks' psframe*[linecolor=<non-white>](...,...)(...,...).

-
Wow. I love the enthusiasm! – user02138 Aug 7 '11 at 2:27
Have a look at the new crop option of standalone v1.0. I implemented some boxing and page resizing for it which should be of interest for you. – Martin Scharrer Dec 21 '11 at 16:24

preview does not invoke pdfcrop. Here is the relevant code from preview inserted into your example. As you can clearly see it simply takes the tex box size in sp and converts that into the postscript page size using the relevant conversion factor (65781.76):

\documentclass{article}
\usepackage{pstricks}

\newcommand\All{0.1}
\newcommand\Left{-\All}
\newcommand\Right{\All}
\newcommand\Bottom{-\All}
\newcommand\Top{\All}

\makeatletter
\newbox\prv

\begin{document}
\setbox\prv\vbox{\noindent
\begin{pspicture}(\Left,\Bottom)(\Right,\Top)
\psframe[linewidth=0.01](\Left,\Bottom)(\Right,\Top)
\end{pspicture}
\par\unskip\setbox0\lastbox
\nointerlineskip\hbox{\unhbox0}%
}
\voffset=-\ht\prv
\hoffset=0pt\relax
\shipout\vbox{%
\@begindvi
\special{!userdict begin/preview-bop-level 0 def%
/eop-hook{/preview-bop-level dup load dup 0 gt{1 sub}if store}bind def
/bop-hook{preview-bop-level 0 le{7{currentfile token not{stop}if
65781.76 div DVImag mul}repeat 72 add 72 2 copy gt{exch}if 4 2 roll
neg 2 copy lt{exch}if dup 0 gt{pop 0 exch}
{exch dup 0 lt{pop 0}if}ifelse 720 add exch 720 add 3 1 roll
4{5 -1 roll add 4 1 roll}repeat
<</PageSize[5 -1 roll 6 index sub 5 -1 roll 5 index sub]%
/PageOffset[7 -2 roll [1 1 dtransform exch]%
{0 ge{neg}if exch}forall]>>setpagedevice}if%
/preview-bop-level dup load dup 0 le{/isls false def%
/vsize 792 def/hsize 612 def}if 1 add store}bind def end}
\special{ps::0 0 0 0
\number\ht\prv \space\number\dp\prv \space\number\wd\prv \space}
\box\prv
}
\end{document}

-
I have tested it for some scenario and it works in the same way the preview package works. It also fails to work with PSTricks and animate package. However, I am satisfied with this answer. :-) – xport Aug 7 '11 at 9:07
I'm glad to help! – Lev Bishop Aug 11 '11 at 18:31