%%\iffalse
%%     preview.dtx for extracting previews from LaTeX documents.  Part of
%%     the preview-latex package.
%%     Copyright (C) 2001, 2002 David Kastrup
%%
%%     This program is free software; you can redistribute it and/or modify
%%     it under the terms of the GNU General Public License as published by
%%     the Free Software Foundation; either version 2 of the License, or
%%     (at your option) any later version.
%%
%%     This program is distributed in the hope that it will be useful,
%%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%%     GNU General Public License for more details.
%%
%%     You should have received a copy of the GNU General Public License
%%     along with this program; if not, write to the
%%     Free Software Foundation, Inc., 59 Temple Place, Suite 330,
%%     Boston, MA 02111-1307  USA
%%\fi
%\CheckSum{1104}
%\def\rcskey#1#2$#3: #4${\edef#1{\rcsstrip #2#4 $}}
%\def\rcsstrip #1 #2${#1}
%\rcskey \version $Name: rel-0-7-5 $
%\ifx\version\empty
%  \rcskey \version CVS-$Revision: 1.70 $
%\else
%  \begingroup
%    \lccode`-=`.
%    \def\next rel-{}
%    \edef\next{\lowercase{\endgroup
%       \def\noexpand\version{\expandafter\next\version}}}
%  \next
%\fi
%\rcskey\next $Date: 2002/11/28 20:42:41 $
%\expandafter\date\expandafter{\next}
%\author{David Kastrup\thanks{\texttt{dakas@users.sourceforge.net}}}
%\title{The \texttt{preview} Package for \LaTeX\\Version \version}
%\maketitle
%\section{Introduction}
%The main purpose of this package is the extraction of certain
%environments (most notably displayed formulas) for use in different
%contexts.  While the erstwhile application has been the embedding of
%those preview fragments into Emacs source buffers under the AUC~\TeX\
%editing environment, other applications are easily imaginable.
%
%In particular it should be noted that producing EPS files with
%Dvips and its derivatives using the \texttt{-E} option is
%not currently well-supported by \LaTeX.  People make do by fiddling
%around with |\thispagestyle{empty}| and hoping for the best (namely,
%that the specified contents will indeed fit on single pages), and
%then trying to guess the baseline of the resulting code and stuff,
%but this is at best dissatisfactory.  The preview package provides an
%easy way to ensure that exactly one page per request gets shipped,
%with a well-defined baseline and no page decorations.  Thus you can
%safely use
%\begin{quote}
%|dvips -E -i|
%\end{quote}
%and get a single EPS file with shrink-wrapped bounding box for every
%generated image of a \LaTeX\ run.
%
%If your ultimate goal is to produce a set of files in a different
%format that can be produced by GhostScript, take a look at the
%|tightpage| option of the preview package.  This will embed the page
%dimensions into the PostScript code, obliterating the need to use the
%|-E -i| options to Dvips.  You can then produce all image files with
%a single run of GhostScript from a single PostScript file for all
%images at once.
%
%Various options exist that will pass \TeX\ dimensions and other
%information about the respective shipped out material (including
%descender size) into the log file, where external applications might
%make use of it.
%
%The possibility for generating a whole set of graphics with a single
%run of \LaTeX, Dvips, and GhostScript increases both speed and
%robustness of applications.  It is to be hoped that applications like
%\LaTeX 2HTML will be able to make use of this package in future.
%
%\section{Package options}
%The package is included with the customary
%\begin{quote}
%|\usepackage|\oarg{options}|{preview}|
%\end{quote}
%You should usually load this package as the last one, since it
%redefines several things that other packages may also provide.
%
%The following options are available:
%\begin{description}
%\item[|active|] is the most essential option.  If this option is not
%specified, the |preview| package will be inactive and the document
%will be typeset as if the |preview| package were not loaded, except
%that all declarations and environments defined by the package are
%still legal but have no effect.  This allows defining previewing
%characteristics in your document, and only activating them by calling
%\LaTeX\ as
%\begin{quote}
%\raggedright
%|latex '\PassOptionsToPackage{active}{preview}| |\input|\marg{filename}|'|
%\end{quote}
%\item[|noconfig|] Usually the file |prdefault.cfg| gets loaded whenever
%the |preview| package gets activated.  |prdefault.cfg| is supposed to
%contain definitions that can cater for otherwise bad results, for
%example, if a certain document class would otherwise lead to
%trouble.  It also can be used to override any settings made in this
%package, since it is loaded at the very end of it.  In addition,
%there may be configuration files specific for certain |preview|
%options like |auctex| which have more immediate needs.  The
%|noconfig| option suppresses loading of those option files, too.
%\item[|psfixbb|] Dvips determines the bounding boxes from the
%material in the DVI file it understands.  Lots of PostScript specials
%are not part of that.  Since the \TeX\ boxes do not make it into the
%DVI file, but merely characters, rules and specials do, Dvips might
%include far too small areas.  The option |psfixbb| will include
%|/dev/null| as a graphic file in the ultimate upper left and lower
%right corner of the previewed box.  This will make Dvips generate an
%appropriate bounding box.
%\item[|dvips|] If this option is specified as a class option or to
%other packages, several packages pass things like page size
%information to Dvips, or cause crop marks or
%draft messages written on pages.  This seriously hampers the
%usability of previews.  If this option is specified, the changes will
%be undone if possible.
%\item[|displaymath|] will make all displayed math environments subject to
%preview processing.  This will typically be the most desired option.
%\item[|floats|] will make all float objects subject to preview
%processing.  If you want to be more selective about what floats to
%pass through to a preview, you should instead use the
%\cmd{\PreviewSnarfEnvironment} command on the floats you want to have
%previewed.
%\item[|textmath|] will make all text math subject to previews.  Since
%math mode is used throughly inside of \LaTeX\ even for other purposes,
%this works by redefining \cmd\(, \cmd\) and |$|.  Only occurences of
%these text math delimiters in later loaded packages and in the main
%document will thus be affected.
%\item[|graphics|] will subject all \cmd{\includegraphics} commands to
%a preview.
%\item[|sections|] will subject all section headers to a preview.
%\item[|delayed|] will delay all activations and redefinitions the
%|preview| package makes until |\begin{document}|.  The purpose of
%this is to cater for documents which should be subjected to the
%|preview| package without having been prepared for it.  You can
%process such documents with
%\begin{quote}
%|latex '\RequirePackage[active,delayed,|\meta{options}|]{preview}|
%|\input|\marg{filename}|'|
%\end{quote}
%This relaxes the requirement to be loading the |preview| package as
%last package.
%\item[\meta{driver}] loads a special driver file
%|pr|\meta{driver}|.def|.  The remaining options are implemented
%through the use of driver files.
%\item[|auctex|]  This driver will produce fake error messages at the
%start and end of every preview environment that enable the Emacs
%package \previewlatex\ in connection with AUC~\TeX\ to pinpoint the
%exact source location where the previews have originated.
%Unfortunately, there is no other reliable means of passing the
%current \TeX\ input position \emph{in} a line to external programs.
%In order to make the parsing more robust, this option also switches
%off quite a few diagnostics that could be misinterpreted.
%
%You should not specify this option manually, since it will only be
%needed by automated runs that want to parse the pseudo error
%messages.  Those runs will then use \cmd{\PassOptionsToPackage} in
%order to effect the desired behaviour.  In addition, |prauctex.cfg|
%will get loaded unless inhibited by the |noconfig| option.  This
%caters for the most frequently encountered problematic commands.
%\item[|showlabels|] During the editing process, some people like to
%see the label names in their equations, figures and the like.  Now if
%you are using Emacs for editing, and in particular \previewlatex,
%I'd strongly recommend that you check out the ref\TeX\ package which
%pretty much obliterates the need for this kind of functionality.  If
%you still want it, standard \LaTeX\ provides it with the |showkeys|
%package, and there is also the less encompassing |showlabels|
%package.  Unfortunately, since those go to some pain not to change
%the page layout and spacing, they also don't change |preview|'s idea
%of the \TeX\ dimensions of the involved boxes.  So if you are using
%|preview| for determing bounding boxes, those packages are mostly
%useless.  The option |showlabels| offers a substitute for them.
%\item[|tightpage|] It is not uncommon to want to use the results of
%|preview| as graphic images for some other application.  One
%possibility is to generate a flurry of EPS files with
%\begin{quote}
%  |dvips -E -i -Pwww -o| \meta{outputfile}|.000| \meta{inputfile}
%\end{quote}
%However, in case those are to be processed further into graphic image
%files by GhostScript, this process is inefficient since all of those
%files need to be processed one by one.  In addition, it is necessary
%to extract the bounding box comments from the EPS files and convert
%them into page dimension parameters for GhostScript in order to avoid
%full-page graphics.  This is not even possible if you wanted to use
%GhostScript in a~\emph{single} run for generating the files from a
%single PostScript file, since Dvips will in that case leave no
%bounding box information anywhere.
%
%The solution is to use the |tightpage| option which will write
%additional PostScript code into the produced file that will set the
%device dimensions at the start of each output page.  That way a
%single command line like
%\begin{quote}
%\raggedright
%  \texttt{gs -sDEVICE=png16m -dTextAlphaBits=4 -r300
%    -dGraphicsAlphaBits=4 -dSAFER -q -dNOPAUSE
%    -sOutputFile=\meta{outputfile}\%d.png \meta{inputfile}.ps}
%\end{quote}
%will be able to produce tight graphics from a single PostScript file
%generated with Dvips \emph{without} use of the options |-E -i|, in a
%single run.  If you need this in a batch environment where you don't
%want to use |preview|'s automatic extraction facilities, no problem:
%just don't use any of the special options, and wrap everything to be
%previewed into |preview| environments.
%
%If the pages under the |tightpage|
%option are just too tight, you can adjust by setting the length
%|\PreviewBorder| to a different value by using \cmd{\setlength}.  The
%default value is |0.50001bp|, which is half of a usual PostScript
%point, rounded up.  If you go below this value, the resulting page
%size may drop below |1bp|, and GhostScript does not seem to like that.
%If you need finer control, you can adjust the bounding box dimensions
%individually by changing the macro |\PreviewBbAdjust| with the help
%of |\renewcommand|.  Its default value is
%\begin{quote}
%\raggedright
%  |\newcommand| |\PreviewBbAdjust|
%     |{-\PreviewBbAdjust| |-\PreviewBbAdjust|
%       |\PreviewBbAdjust|  |\PreviewBbAdjust}|
%\end{quote}
%This adjusts the left, lower, right and upper borders by the given
%amount.  The macro must contain 4~\TeX\ dimensions after another, and
%you may not omit the units if you specify them explicitly instead of
%by register.  PostScript points have the unit~|bp|.
%\item[|lyx|] This option is for the sake of LyX developers.  It will
%output a few diagnostics relevant for the sake of LyX' preview
%functionality (at the time of writing, just implemented for math
%insets, in the CVS version of LyX that will eventually be released as
%1.3.0).
%\item[|counters|] This writes out diagnostics at the start and the end
%of previews.  Only the counters changed since the last output get
%written, and if no counters changed, nothing get written at all.  The
%list consists of counter name and value, both enclosed in |{}|
%braces, followed by a space.  The last such pair is followed by a
%colon (|:|) if it is at the start of the preview snippet, and by a
%period (|.|) if it is at the end.  The order of different diagnostics
%like this being issued depends on the order of the specification of
%the options when calling the package.
%\end{description}
%The following options are just for debugging purposes of the package
%and similar to the corresponding \TeX\ commands they allude to:
%\begin{description}
%\item[|tracingall|]
%causes lots of diagnostic output to appear in the log file during the
%preview collecting phases of \TeX's operation.  In contrast to the
%similarly named \TeX\ command, it will not switch to
%|\errorstopmode|, nor will it change the setting of |\tracingonline|.
%\item[|showbox|] This option will show the contents of the boxes
%shipped out to the DVI files.  It also sets |\showboxbreadth| and
%|\showboxdepth| to their maximum values at the end of loading this
%package, but you may reset them if you don't like that.
%\end{description}
%\section{Provided Commands}
%\DescribeEnv{preview} The |preview| environment causes its contents to
%be set as a single preview image.  Insertions like figures and
%footnotes (except those
%included in minipages) will typically lead to error messages or
%be lost.  In case the |preview| package has not been activated, the
%contents of this environment will be typeset normally.
%
%\DescribeEnv{nopreview} The |nopreview| environment will cause its
%contents not to undergo any special treatment by the |preview|
%package.  When |preview| is active, the contents will be discarded
%like all main text that does not trigger the |preview| hooks.  When
%|preview| is not active, the contents will be typeset just like the
%main text.
%
%Note that both of these environments typeset things as usual when
%preview is not active.  If you need something typeset conditionally,
%use the \cmd{\ifPreview} conditional for it.
%
%\DescribeMacro{\PreviewMacro} If you want to make a macro like
%\cmd{\includegraphics} (actually, this is what is done by the
%|graphics| option to |preview|) produce a preview image, you put a
%declaration like
%\begin{quote}
%|\PreviewMacro[*[[!]{\includegraphics}|
%\end{quote}
%or, more readable,
%\begin{quote}
%|\PreviewMacro[{*[][]{}}]{\includegraphics}|
%\end{quote}
%into your preamble.  The optional argument to \cmd{\PreviewMacro}
%specifies the arguments \cmd{\includegraphics} accepts, since this is
%necessary information for properly ending the preview box.  Note that
%if you are using the more readable form, you have to enclose the
%argument in a |[{| and |}]| pair.  The inner braces are necessary to
%stop any included |[]| pairs from prematurely ending the optional
%argument, and to make a single |{}| denoting an optional argument not
%get stripped away by \TeX's argument parsing.
%
%The letters simply mean
%\begin{description}
%\item[|*|] indicates an optional |*| modifier, as in
%|\includegraphics*|.
%\item[|[|] indicates an optional argument in brackets.  This syntax
%is somewhat baroque, but brief.
%\item[{|[]|}] also indicates an optional argument in brackets.  Be
%sure to have encluded the entire optional argument specification in
%an additional pair of braces as described above.
%\item[|!|] indicates a mandatory argument.
%\item[|\char`{\char`}|] indicates the same.  Again, be sure to have that
%additional level of braces around the whole argument specification.
%\end{description}
%\DescribeMacro{\PreviewMacro*}
%A similar invocation \cmd{\PreviewMacro*} simply throws the macro and
%all of its arguments declared in the manner above away.  This is
%mostly useful for having things like \cmd{\footnote} not do their
%magic on their arguments.  More often than not, you don't want to
%declare any arguments to scan to \cmd{\PreviewMacro*} since you would
%want the remaining arguments to be treated as usual text and typeset in
%that manner instead of being thrown away.  An exception might be,
%say, sort keys for \cmd{\cite}.
%
%\DescribeMacro{\PreviewEnvironment} The macro
%\cmd{\PreviewEnvironment} works just as
%\cmd{\PreviewMacro} does, only for environments.
%\DescribeMacro{\PreviewEnvironment*} And the same goes for
%\cmd{\PreviewEnvironment*} as compared to \cmd{\PreviewMacro*}.
%
%\DescribeMacro{\PreviewSnarfEnvironment} This macro does not typeset the
%original environment inside of a preview box, but instead typesets
%just the contents of the original environment inside of the preview
%box, leaving nothing for the original environment.  This has to be
%used for figures, for example, since they would
%\begin{enumerate}
%\item produce insertion material that cannot be extracted to the
%preview properly,
%\item complain with an error message about not being in outer par
%mode.
%\end{enumerate}
%
%\DescribeMacro{\ifPreview} In case you need to know whether |preview|
%is active, you can use the conditional \cmd{\ifPreview} together with
%|\else| and |\fi|.  
%\StopEventually{}
%\section{The Implementation}
%Here we go:  the start is somewhat obtuse since we figure out
%version number and date from RCS strings.  This should really be done
%at docstrip time instead.  Takers?
%    \begin{macrocode}
%<*style>
%<*!active>
\NeedsTeXFormat{LaTeX2e} \def\reserved@a #1#2$#3:
#4${\edef#1{\reserved@c #2#4 $}} \def\reserved@c #1 #2${#1}
\reserved@a\reserved@b $Name: rel-0-7-5 $ \ifx\reserved@b\@empty
\reserved@a\reserved@b CVS-$Revision: 1.70 $ \else \begingroup
\lccode`-=`.  \def\next rel-{} \edef\next{\lowercase{\endgroup
    \def\noexpand\reserved@b{\expandafter\next\reserved@b}}} \next \fi
\reserved@a\next $Date: 2002/11/28 20:42:41 $
\edef\next{\noexpand\ProvidesPackage{preview}%
  [\next\space preview-latex \reserved@b]}
\next
%    \end{macrocode}
%Since many parts here will not be needed as long as the package is
%inactive, we will include them enclosed with |<*active>| and
%|</active>| guards.  That way, we can append all of this stuff at a
%place where it does not get loaded if not necessary.
%
%\begin{macro}{\ifPreview}
%  Setting the \cmd{\ifPreview} command should not be done by the user,
%  so we don't use \cmd{\newif} here.  As a consequence, there are no
%  \cmd{\Previewtrue} and \cmd{\Previewfalse} commands.
%    \begin{macrocode}
\let\ifPreview\iffalse
%</!active>
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\ifpr@outer}
%  We don't allow previews inside of previews.  The macro
%  \cmd{\ifpr@outer} can be used for checking whether we are outside of
%  any preview code.
%    \begin{macrocode}
%<*active>
\newif\ifpr@outer
\pr@outertrue
%</active>
%    \end{macrocode}
%\end{macro}
%
%\begin{macro}{\preview@delay}
%  The usual meaning of \cmd{\preview@delay} is to just echo its
%  argument in normal |preview| operation.  If |preview| is inactive, it
%  swallows its argument.  If the |delayed| option is active, the
%  contents will be passed to the \cmd{\AtBeginDocument} hook.
%\begin{macro}{\pr@advise}
%  The core macro for modifying commands is \cmd{\pr@advise}.  You
%  pass it the original command name as first argument and what should
%  be executed before the saved original command as second argument.
%\begin{macro}{\pr@advise@ship}
%  The most often used macro for modifying commands is
%  \cmd{\pr@advise@ship}.  It receives three arguments.  The first is
%  the macro to modify, the second specifies some actions to be done
%  inside of a box to be created before the original macro gets
%  executed, the third one specifies actions after the original macro
%  got executed.
%\begin{macro}{\pr@loadcfg}
%  The macro \cmd{\pr@loadcfg} is used for loading in configuration
%  files, unless disabled by the |noconfig| option.
%    \begin{macrocode}
%<*!active>
\let\preview@delay=\@gobble
\let\pr@advise=\@gobbletwo
\def\pr@advise@ship#1#2#3{}
\def\pr@loadcfg#1{\InputIfFileExists{#1.cfg}{}{}}
\DeclareOption{noconfig}{\let\pr@loadcfg=\@gobble}
%    \end{macrocode}
%\begin{macro}{\pr@addto@front}
%  This adds code globally to the front of a macro.
%    \begin{macrocode}
\def\pr@addto@front#1#2{%
  \toks@{#2}\toks@\expandafter{\the\expandafter\toks@#1}%
  \xdef#1{\the\toks@}}
%    \end{macrocode}
%\end{macro}
%  These commands get more interesting when |preview| is active:
%    \begin{macrocode}
\DeclareOption{active}{%
  \let\ifPreview\iftrue
  \def\pr@advise#1{%
    \expandafter\pr@adviseii\csname pr@\string#1\endcsname#1}%
  \def\pr@advise@ship#1#2#3{\pr@advise#1{\pr@protect@ship{#2}{#3}}}%
  \let\preview@delay\@firstofone}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%
%\begin{macro}{\pr@adviseii}
%  Now \cmd{\pr@advise} needs its helper macro.  In order to avoid
%  recursive definitions, we advise only macros that are not yet
%  advised.  Or, more exactly, we throw away the old advice and only
%  take the new one.
%    \begin{macrocode}
\def\pr@adviseii#1#2#3{\preview@delay{%
  \ifx#1\relax \let#1#2\fi
  \toks@{#3#1}\edef#2{\the\toks@}}}
%    \end{macrocode}
%\end{macro}
%
%  The |delayed| option is easy to implement: this is \emph{not} done
%  with \cmd{\let} since at the course of document processing, \LaTeX\
%  redefines \cmd{\AtBeginDocument} and we want to follow that
%  redefinition.
%    \begin{macrocode}
\DeclareOption{delayed}{%
  \ifPreview \def\preview@delay{\AtBeginDocument}\fi
}
%    \end{macrocode}
%
%\begin{macro}{\ifpr@fixbb}
%  Another conditional.  \cmd{\ifpr@fixbb} tells us whether we want to
%  surround the typeset materials with invisible rules so that Dvips
%  gets the bounding boxes right for, say, pure PostScript inclusions.
%
%  If you are installing this on an operating system different from
%  the one |preview| has been developed on, you might want to redefine
%  |\pr@markerbox| in your |prdefault.cfg| file to use a file known to
%  be empty, like |/dev/null| is under Unix.  Make this redefinition
%  depend on \cmd{\ifpr@fixbb} since only then |\pr@markerbox| will be
%  defined.
%    \begin{macrocode}
\newif\ifpr@fixbb
\pr@fixbbfalse
\DeclareOption{psfixbb}{\ifPreview%
  \pr@fixbbtrue
  \newbox\pr@markerbox
  \setbox\pr@markerbox\hbox{\special{psfile=/dev/null}\fi}%
}
%    \end{macrocode}
%\end{macro}
%  The |dvips| option redefines the |bop-hook| to reset the page size.
%    \begin{macrocode}
\DeclareOption{dvips}{%
  \preview@delay{\AtBeginDvi{%
    \special{!userdict begin/bop-hook{/isls false def%
    /vsize 792 def/hsize 612 def}def end}}}}
%</!active>
%    \end{macrocode}
%
%\subsection{The internals}
%
%Those are only needed if |preview| is active.
%    \begin{macrocode}
%<*active>
%    \end{macrocode}
%\begin{macro}{\pr@snippet}
%  \cmd{\pr@snippet} is the current snippet number.  We need a
%  separate counter to \cmd{\c@page} since several other commands
%  might fiddle with the page number.
%    \begin{macrocode}
\newcount\pr@snippet
\global\pr@snippet=1
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\pr@protect}
%  This macro gets one argument which is unpacked and executed in
%  typesetting situations where we are not yet inside of a preview.
%    \begin{macrocode}
\def\pr@protect{\ifx\protect\@typeset@protect
  \ifpr@outer \expandafter\expandafter\expandafter
     \@secondoftwo\fi\fi\@gobble}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\pr@protect@ship}
%  Now for the above mentioned \cmd{\pr@protect@ship}.  This
%  gets three arguments.  The first is what to do at the beginning of
%  the preview, the second what to do at the end, the third is the
%  macro where we stored the original definition.
%
%  In case we are not in a typesetting situation,
%  \cmd{\pr@protect@ship} leaves the stored macro to fend for its
%  own.  No better or worse protection than the original.  And we only
%  do anything different when \cmd{\ifpr@outer} turns out to be true.
%    \begin{macrocode}
\def\pr@protect@ship{\pr@protect{\@firstoftwo\pr@startbox}%
   \@gobbletwo}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\pr@box}
%\begin{macro}{\pr@startbox}
%  Previews will be stored in \cmd{\box}\cmd{\pr@box}.
%  \cmd{\pr@startbox} gets two arguments: code to execute immediately
%  before the following stuff, code to execute afterwards.  You have
%  to cater for \cmd{\pr@endbox} being called at the right time
%  yourself.  We will use a \cmd{\vsplit} on the box later in order to
%  remove any leading glues, penalties and similar stuff.  For this
%  reason we start off the box with an optimal break point.
%    \begin{macrocode}
\newbox\pr@box
\def\pr@startbox#1#2{%
  \ifpr@outer
    \toks@{#2}%
    \edef\pr@cleanup{\the\toks@}%
    \setbox\pr@box\vbox\bgroup
    \break
    \pr@outerfalse\@arrayparboxrestore
    \expandafter\expandafter\expandafter
    \pr@ship@start
    \expandafter\@firstofone
  \else
     \expandafter \@gobble
  \fi{#1}}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\begin{macro}{\pr@endbox}
%  Cleaning up also is straightforward.  If we have to watch the
%  bounding \TeX\ box, we want to remove spurious skips.  We also want
%  to unwrap a possible single line paragraph, so that the box is not
%  full line length.  We use \cmd{\vsplit} to clean up leading glue
%  and stuff, and we make some attempt of removing trailing ones.
%  After that, we wrap up the box including possible material from
%  \cmd{\AtBeginDvi}.  If the |psfixbb| option is active, we adorn the
%  upper left and lower right corners with copies of
%  \cmd{\pr@markerbox}.
%  The first few lines cater for \LaTeX\ hiding things like
%  like the code for \cmd{\paragraph} in \cmd{\everypar}.
%    \begin{macrocode}
\def\pr@endbox{%
   \let\reserved@a\relax
   \ifvmode \edef\reserved@a{\the\everypar}%
      \ifx\reserved@a\@empty\else
            \dimen@\prevdepth
            \noindent\par
            \setbox\z@\lastbox\unskip\unpenalty
            \prevdepth\dimen@
            \setbox\z@\hbox\bgroup\penalty-\maxdimen\unhbox\z@
              \ifnum\lastpenalty=-\maxdimen\egroup
              \else\egroup\box\z@ \fi\fi\fi
   \ifhmode \par\unskip\setbox\z@\lastbox
     \nointerlineskip\hbox{\unhbox\z@\/}%
   \else \unskip\unpenalty\unskip \fi
   \egroup
   \setbox\pr@box\vbox{%
       \baselineskip\z@skip \lineskip\z@skip \lineskiplimit\z@
       \@begindvi
       \nointerlineskip
       \splittopskip\z@skip\setbox\z@\vsplit\pr@box to\z@
       \unvbox\z@
       \nointerlineskip
       \color@setgroup
       \box\pr@box
       \color@endgroup}%
%    \end{macrocode}
%  \begin{macro}{\pr@ship@end}
%  \label{sec:prshipend}At this point, \cmd{\pr@ship@end} gets
%  called.  You must not under any circumstances change |\box\pr@box|
%  in any way that would add typeset material at the front of it,
%  except for PostScript header specials, since the front of
%  |\box\pr@box| may contains stuff from \cmd{\AtBeginDvi}.
%  \cmd{\pr@ship@end} contains two types of code additions: stuff that
%  adds to |\box\pr@box|, like the |labels| option does, and stuff
%  that measures out things or otherwise takes a look at the finished
%  |\box\pr@box|, like the |auctex| or |showbox| option do.  The
%  former should use \cmd{pr@addto@front} for adding to this hook, the
%  latter use \cmd{g@addto@macro} for adding at the end of this hook.
%
%  Note that we shift the output box up by its height via
%  \cmd{\voffset}.  This has three reasons: first we make sure that no
%  package-inflicted non-zero value of \cmd{\voffset} or
%  \cmd{\hoffset} will have any influence on the positioning of our
%  box.  Second we shift the box such that its basepoint will exactly
%  be at the (1in,1in)~mark defined by \TeX.  That way we can properly
%  take ascenders into account.  And the third reason is that \TeX\
%  treats a \cmd{\hbox} and a \cmd{\vbox} differently with regard to
%  the treating of its depth.  
%    \begin{macrocode}
   \pr@ship@end
   {\let\protect\noexpand
   \voffset=-\ht\pr@box
   \hoffset=\z@
   \c@page=\pr@snippet
   \pr@shipout
   \ifpr@fixbb\hbox{%
     \dimen@\wd\pr@box
     \@tempdima\ht\pr@box
     \@tempdimb\dp\pr@box
     \box\pr@box
     \llap{\raise\@tempdima\copy\pr@markerbox\kern\dimen@}%
     \lower\@tempdimb\copy\pr@markerbox}%
   \else \box\pr@box \fi}%
   \global\advance\pr@snippet\@ne
   \pr@cleanup
}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%Oh, and we kill off the usual meaning of \cmd{\shipout} in case
%somebody makes a special output routine.  The following is rather
%ugly, but should do the trick most of the time since \cmd{\shipout}
%is most often called in a local group by \cmd{\output}.
%  \begin{macro}{\shipout}
%    \begin{macrocode}
\let\pr@shipout=\shipout
\def\shipout{\deadcycles\z@\setbox\z@\box\voidb@x\setbox\z@}
%    \end{macrocode}
%  \end{macro}
%\subsection{Parsing commands}
%\begin{macro}{\pr@callafter}
%\begin{macro}{\pr@parseit}
%\begin{macro}{\pr@seq}
%\begin{macro}{\pr@endparse}
%  The following stuff is for parsing the arguments of commands we want
%  to somehow surround with stuff.  Usage is
%    \begin{quote}
%    \cmd{\pr@callafter}\meta{aftertoken}\meta{parsestring}\cmd{\pr@endparse}\\
%    \qquad\meta{macro}\meta{parameters}
%    \end{quote}
%  \meta{aftertoken} is stored in \cmd{\pr@seq} and gets placed after
%  the completely parsed macro.  \meta{parsestring} would be, for
%  example for the \cmd{\includegraphics} macro, |*[[!|, an optional
%  |*| argument followed by two optional arguments enclosed in |[]|,
%  followed by one mandatory argument.
%
%  For the sake of a somewhat more intuitive syntax, we now support
%  also the syntax |{*[]{}}| in the optional argument.  Since \TeX\
%  strips redundant braces, we have to write |[{{}}]| in this syntax
%  for a single mandatory argument.  Hard to avoid.  We use an unusual
%  character for ending the parsing.
%  The implementation is rather trivial.
%    \begin{macrocode}
\def\pr@callafter{%
  \afterassignment\pr@parseit
  \let\pr@seq= }
\def\pr@parseit#1{\csname pr@parse#1\endcsname}
\let\pr@endparse=\@percentchar
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\begin{macro}{\pr@parse*}
%  Straightforward, same mechanism \LaTeX\ itself employs.
%    \begin{macrocode}
\expandafter\def\csname pr@parse*\endcsname#1\pr@endparse#2{%
  \@ifstar{\pr@parseit#1\pr@endparse{#2*}}%
          {\pr@parseit#1\pr@endparse{#2}}}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\pr@parse[}
%\begin{macro}{\pr@brace}
%  Copies optional parameters in brackets if present.  The additional
%  level of braces is necessary to ensure that braces the user might
%  have put to hide a~|]| bracket in an optional argument don't get
%  lost.  There will be no harm if such braces were not there at the
%  start.
%    \begin{macrocode}
\expandafter\def\csname pr@parse[\endcsname#1\pr@endparse#2{%
  \@ifnextchar[{\pr@bracket#1\pr@endparse{#2}}%
               {\pr@parseit#1\pr@endparse{#2}}}
\def\pr@bracket#1\pr@endparse#2[#3]{\pr@parseit#1\pr@endparse{#2[{#3}]}}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\begin{macro}{\pr@parse]}
%  This is basically a do-nothing, so that we may use the syntax
%  |{*[][]!}| in the optional argument instead of the more concise but
%  ugly |*[[!| which confuses the brace matchers of editors.
%    \begin{macrocode}
\expandafter\let\csname pr@parse]\endcsname=\pr@parseit
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\pr@parse}
%\begin{macro}{\pr@parse!}
%  Mandatory arguments are perhaps easiest to parse.
%    \begin{macrocode}
\def\pr@parse#1\pr@endparse#2#3{%
  \pr@parseit#1\pr@endparse{#2{#3}}}
\expandafter\let\csname pr@parse!\endcsname=\pr@parse
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\begin{macro}{\pr@parse\pr@endparse}
%  And finally the macro that gets called at the end and wraps all
%  this up by placing the completed macro call and then putting
%  \cmd{\pr@seq} behind it.
%    \begin{macrocode}
\expandafter\def\csname pr@parse\pr@endparse\endcsname#1{#1\pr@seq}
%</active>
%    \end{macrocode}
%\end{macro}
%\subsection{Selection options}
%  The |displaymath| option.  The |equation| environments in
%  AMS\LaTeX\ already do too much before our hook gets to interfere,
%  so we hook earlier.  Some juggling is involved to ensure we get the
%  original |\everydisplay| tokens only once and where appropriate.
%
%  The incredible hack with |\dt@ptrue| is necessary for working
%  around bug `amslatex/3425'.
%    \begin{macrocode}
%<*!active>
\begingroup
\catcode`\*=11
\@firstofone{\endgroup
\DeclareOption{displaymath}{%
  \preview@delay{\toks@{%
      \pr@startbox{\noindent$$%
        \aftergroup\pr@endbox\@gobbletwo}{$$}\@firstofone}%
    \everydisplay\expandafter{\the\expandafter\toks@
      \expandafter{\the\everydisplay}}}%
  \pr@advise@ship\equation{\begingroup\aftergroup\pr@endbox
    \def\dt@ptrue{\m@ne=\m@ne}\noindent}
    {\endgroup}%
  \pr@advise@ship\equation*{\begingroup\aftergroup\pr@endbox
    \def\dt@ptrue{\m@ne=\m@ne}\noindent}
    {\endgroup}%
}}
%    \end{macrocode}
%
%  The |textmath| option.  Some folderol in order to define the active
%  |$| math mode delimiter.  \cmd\pr@textmathcheck is used for
%  checking whether we have a single |$| or double |$$|.  In the
%  latter case, we enter display math (this sort of display math is
%  not allowed inside of \LaTeX\ because of inconsistent spacing, but
%  surprisingly many people use it nevertheless).  Strictly speaking,
%  this is incorrect, since not every |$$| actually means display
%  math.  For example, |\hbox{$$}| will because of restricted
%  horizontal mode rather yield an empty text math formula.  Since our
%  implementation moved the sequence inside of a |\vbox|, the
%  interpretation will change.  People should just not enter rubbish
%  like that.
%    \begin{macrocode}
\begingroup
\def\next#1#2{%
  \endgroup
  \DeclareOption{textmath}{%
    \preview@delay{\ifx#1\@undefined \let#1=$%$
      \fi\catcode`\$=\active}%
    \pr@advise@ship\(\pr@endaftergroup{}% \)
    \pr@advise@ship#1{\@firstoftwo{\let#1=#2%
        \futurelet\reserved@a\pr@textmathcheck}}{}}%
  \def\pr@textmathcheck{\expandafter\pr@endaftergroup
    \ifx\reserved@a#1{#2#2}\expandafter\@gobbletwo\fi#2}}
\lccode`\~=`\$
\lowercase{\expandafter\next\expandafter~}%
  \csname pr@\string$\endcsname
%</!active>
%    \end{macrocode}
%\begin{macro}{\pr@endaftergroup}
%  This justs ends the box after the group opened by |#1| is closed
%  again.
%    \begin{macrocode}
%<*active>
\def\pr@endaftergroup#1{#1\aftergroup\pr@endbox}
%</active>
%    \end{macrocode}
%\end{macro}
%
%  The |graphics| option.
%    \begin{macrocode}
%<*!active>
\DeclareOption{graphics}{%
  \PreviewMacro[*[[!]{\includegraphics}%]]
}
%    \end{macrocode}
%  The |floats| option.
%    \begin{macrocode}
\DeclareOption{floats}{%
  \PreviewSnarfEnvironment[![]{@float}%]
  \PreviewSnarfEnvironment[![]{@dblfloat}%]
}
%    \end{macrocode}
%  The |sections| option.
%    \begin{macrocode}
\DeclareOption{sections}{%
  \PreviewMacro[!!!!!!*[!]{\@startsection}%]
}
%    \end{macrocode}
%We now interpret any further options as driver files we load.  Note
%that these driver files are loaded even when |preview| is not
%active.  The reason is that they might define commands (like
%\cmd{\PreviewCommand}) that should be available even in case of
%an inactive package.  Large parts of the |preview| package will not
%have been loaded in this case: you have to cater for that.
%    \begin{macrocode}
\DeclareOption*
   {\InputIfFileExists{pr\CurrentOption.def}{}{\OptionNotUsed}}
%    \end{macrocode}
%
%\subsection{Preview attaching commands}
%  \begin{macro}{\PreviewMacro}
%  As explained above. Detect possible |*| and call appropriate macro.
%    \begin{macrocode}
\def\PreviewMacro{\@ifstar\pr@starmacro\pr@macro}
%    \end{macrocode}
%  The version without |*| is now rather straightforward.
%  \begin{macro}{\pr@macro}
%    \begin{macrocode}
\newcommand*\pr@macro[2][]{%
   \pr@advise@ship{#2}{\pr@callafter\pr@endbox#1\pr@endparse}{}}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%  \begin{macro}{PreviewMacro*}
%  \begin{macro}{\pr@protect@star}
%  \begin{macro}{\pr@stargobble}
%  \begin{macro}{\pr@starmacro}
%  The version with |*| has to parse the arguments, then throw them
%  away.  Some internal macros first, then the interface call.
%    \begin{macrocode}
\def\pr@protect@star#1{\pr@protect{%
    \pr@callafter\pr@seq!#1\pr@endparse\pr@stargobble}}
\def\pr@stargobble#1\pr@seq{}
\newcommand*\pr@starmacro[2][]{\pr@advise#2{\pr@protect@star{#1}}}
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\end{macro}
%\end{macro}
%\begin{macro}{\PreviewEnvironment}
%  Actually, this ignores any syntax argument.  But don't tell
%  anybody.  But for the |*|~form, it respects (actually ignores) any
%  argument!  Of course, we'll need to deactivate
%  |\end{|\meta{environment}|}| as well.
%    \begin{macrocode}
\def\PreviewEnvironment{\@ifstar\pr@starenv\pr@env}
\newcommand*\pr@starenv[2][]{\toks@{\pr@starmacro[{#1}]}%
  \the\expandafter\toks@\csname#2\endcsname
  \expandafter\pr@starmacro\csname end#2\endcsname}
\newcommand*{\pr@env}[2][]{\expandafter\pr@advise@ship
   \csname #2\endcsname{\begingroup\aftergroup\pr@endbox}{\endgroup}}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\PreviewSnarfEnvironment}
%  This is a nuisance since we have to advise \emph{both} the
%  environment and its end.
%    \begin{macrocode}
\newcommand*{\PreviewSnarfEnvironment}[2][]{%
  \expandafter\pr@advise
   \csname #2\endcsname{\pr@snarfafter#1\pr@endparse}%
 \expandafter\pr@advise
   \csname end#2\endcsname{\endgroup}}
%</!active>
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\pr@snarfafter}
%\begin{macro}{\pr@startsnarf}
%  Ok, this looks complicated, but we have to start a group in order
%  to be able to hook \cmd{\pr@endbox} into the game only when
%  \cmd{\ifpr@outer} has triggered the start.  And we need to get our
%  start messages out before parsing the arguments.
%    \begin{macrocode}
%<*active>
\def\pr@snarfafter{\ifpr@outer
     \pr@ship@start
     \let\pr@ship@start\@empty
   \fi
  \pr@callafter\pr@startsnarf}
\def\pr@startsnarf{\begingroup
   \pr@startbox{\begingroup\aftergroup\pr@endbox}{\endgroup}%
   \ignorespaces}
%</active>
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\begin{macro}{\pr@ship@start}
%\begin{macro}{\pr@ship@end}
%  The hooks \cmd{\pr@ship@start} and \cmd{\pr@ship@end} can be added
%  to by option files by the help of the \cmd{\g@addto@macro} command
%  from \LaTeX, and by the \cmd{\pr@addto@front} command from
%  |preview.sty| itself.  They are called just before starting to
%  process some preview, and just after it.  Here is the policy for
%  adding to them: \cmd{\pr@ship@start} is called inside of the vbox
%  |\pr@box| before typeset material gets produced.  It is, however,
%  preceded by a break command that is intended for usage in
%  \cmd{\vsplit}, so that any following glue might disappear.  In case
%  you want to add any material on the list, you have to precede it
%  with \cmd{\unpenalty} and have to follow it with \cmd{\break}.  You
%  have make sure that under no circumstances any other legal
%  breakpoints appear before that, and your material should contribute
%  no nonzero dimensions to the page.  For the policies of the
%  \cmd{\pr@ship@end} hook, see the description on
%  page~\pageref{sec:prshipend}.
%    \begin{macrocode}
%<*!active>
\let\pr@ship@start\@empty
\let\pr@ship@end\@empty
%    \end{macrocode}
%\end{macro}
%\end{macro}
%\begin{environment}{preview}
%\begin{environment}{nopreview}
%  First we write the definitions of these environments when |preview|
%  is inactive.  We will redefine them if |preview| gets activated.
%    \begin{macrocode}
\newenvironment{preview}{\ignorespaces}{\unskip}
\newenvironment{nopreview}{\ignorespaces}{\unskip}
%    \end{macrocode}
%\end{environment}
%\end{environment}
%
%We now process the options and finish in case we are not active.
%    \begin{macrocode}
\ProcessOptions\relax
\ifPreview\else\expandafter\endinput\fi
%</!active>
%    \end{macrocode}
%Now for the redefinition of the |preview| and |endpreview|
%environments:
%    \begin{macrocode}
%<*active>
\renewenvironment{preview}{\begingroup
   \pr@startbox{\begingroup\aftergroup\pr@endbox}%
               {\endgroup}%
   \ignorespaces}%
   {\unskip\endgroup}
\renewenvironment{nopreview}{\pr@outerfalse\ignorespaces}{\unskip}
%    \end{macrocode}
%Try to keep \LaTeX\ from overwriting its information files:
%    \begin{macrocode}
\nofiles
%    \end{macrocode}
%Let the output routine throw everything gathered regularly away.
%Start with all float boxes, continue with output box, pack everything
%afloat from \cmd{\@currlist} back into \cmd{\@freelist}.
%    \begin{macrocode}
\output{\def\@elt#1{\global\setbox#1=\box\voidb@x}%
  \@currlist
  \@elt{255}%
  \let\@elt\relax
  \xdef\@freelist{\@currlist\@freelist}%
  \global\let\@currlist\@empty
  \deadcycles\z@}
%    \end{macrocode}
%\begin{macro}{\pr@typeinfos}
%  Then we have some document info that style files might want to
%  output.
%    \begin{macrocode}
\def\pr@typeinfos{\typeout{Preview: Fontsize \f@size pt}%
  \ifnum\mag=\@m\else\typeout{Preview: Magnification \number\mag}\fi}
\AtBeginDocument{\pr@typeinfos}
%    \end{macrocode}
%\end{macro}
%And at the end we load the default configuration file, so that it may
%override settings from this package:
%    \begin{macrocode}
\pr@loadcfg{prdefault}
%</active>
%</style>
%    \end{macrocode}
%
%\section{The option files}
%\subsection{The \texttt{auctex} option}
%The AUC~\TeX\ option will cause error messages to spew.  We want them
%on the terminal, but we don't want \LaTeX\ to stop its automated run.
%We delay \cmd{\nonstopmode} in case the user has any
%pseudo-interactive folderol like reading in of file names in his
%preamble.  Because we are so good-hearted, we will not break this as
%long as the document has not started, but after that we need the
%error message mechanism operative.
%
%So here is the contents of the |prauctex.def| file:
%    \begin{macrocode}
%<auctex>\ifPreview\else\expandafter\endinput\fi
%<auctex>\preview@delay{\nonstopmode}
%    \end{macrocode}
%Ok, here comes creative error message formatting.  It turns out a
%sizable portion of the runtime is spent in I/O.  Making the error
%messages short is an advantage.  It is not possible to convince \TeX\
%to make shorter error messages than this: \TeX\ always wants to
%include context.  This is about the shortest \ae sthetic one we can
%muster.
%    \begin{macrocode}
%<auctex>\begingroup
%<auctex>\lccode`\~=`\-
%<auctex>\lccode`\{=`\<
%<auctex>\lccode`\}=`\>
%<auctex>\lowercase{\endgroup
%<auctex>  \def\pr@msgi{{~}}}
%<auctex>\def\pr@msgii{Preview:
%<auctex>   Snippet \number\pr@snippet\space}
%<auctex>\begingroup
%<auctex>\catcode`\-=13
%<auctex>\catcode`\<=13
%<auctex>\@firstofone{\endgroup
%<auctex>\def\pr@msg#1{{%
%<auctex>   \let<\pr@msgi
%<auctex>   \def-{\pr@msgii#1}%
%<auctex>   \errhelp{Not a real error.}%
%<auctex>   \errmessage<}}}
%<auctex>\g@addto@macro\pr@ship@start{\pr@msg{started}}
%<auctex>\g@addto@macro\pr@ship@end{\pr@msg{ended.%
%<auctex>  (\number\ht\pr@box+\number\dp\pr@box x\number\wd\pr@box)}}
%    \end{macrocode}
%This looks pretty baffling, but it produces something short and
%semi-graphical, namely |<-><->|.  That is a macro |<| that expands
%into |<->|, where |<| and |>| are the braces around an
%\cmd{\errmessage} argument and |-| is a macro expanding to the full
%text of the error message.  Cough cough.  You did not really want to
%know, did you?
%
%Since over/underfull boxes are about the messiest things to parse, we
%disable them by setting the appropriate badness limits and making the
%variables point to junk.  We also disable other stuff.  While we set
%\cmd{\showboxbreadth} and \cmd{\showboxdepth} to indicate as little
%diagnostic output as possible, we keep them operative, so that the
%user retains the option of debugging using this stuff.  The other
%variables concerning the generation of warnings and daignostics,
%however, are more often set by commonly employed packages and macros
%such as \cmd{\sloppy}.  So we kill them off for good.
%    \begin{macrocode}
%<auctex>\hbadness=\maxdimen
%<auctex>\newcount\hbadness
%<auctex>\vbadness=\maxdimen
%<auctex>\let\vbadness=\hbadness
%<auctex>\hfuzz=\maxdimen
%<auctex>\newdimen\hfuzz
%<auctex>\vfuzz=\maxdimen
%<auctex>\let\vfuzz=\hfuzz
%<auctex>\showboxdepth=-1
%<auctex>\showboxbreadth=-1
%    \end{macrocode}
%Ok, now we load a possible configuration file.
%    \begin{macrocode}
%<auctex>\pr@loadcfg{prauctex}
%    \end{macrocode}
%And here we cater for several frequently used commands in
%|prauctex.cfg|:
%    \begin{macrocode}
%<auccfg>\PreviewMacro*\footnote
%<auccfg>\PreviewMacro*\emph
%<auccfg>\PreviewMacro*\textrm
%<auccfg>\PreviewMacro*\textit
%<auccfg>\PreviewMacro*\textsc
%<auccfg>\PreviewMacro*\textsf
%<auccfg>\PreviewMacro*\textsl
%<auccfg>\PreviewMacro*\texttt
%<auccfg>\PreviewMacro*\textcolor
%<auccfg>\PreviewMacro*\mbox
%<auccfg>\PreviewMacro*\maketitle
%<auccfg>\PreviewMacro*\author
%<auccfg>\PreviewMacro*\title
%<auccfg>\PreviewMacro*\and
%<auccfg>\PreviewMacro*\thanks
%<auccfg>\preview@delay{\@ifundefined{pr@\string\@startsection}{%
%<auccfg>  \PreviewMacro*[!!!!!!*]\@startsection}{}}
%<auccfg>\PreviewMacro*\index
%    \end{macrocode}
%
%\subsection{The \texttt{lyx} option}
%The following is the option providing LyX with info for its
%preview implementation.
%    \begin{macrocode}
%<lyx>\ifPreview\else\expandafter\endinput\fi
%<lyx>\pr@loadcfg{prlyx}
%<lyx>\g@addto@macro\pr@ship@end{\typeout{Preview:
%<lyx>  Snippet \number\pr@snippet\space
%<lyx>  \number\ht\pr@box\space \number\dp\pr@box \space\number\wd\pr@box}}
%    \end{macrocode}
%
%\subsection{The \texttt{counters} option}
%This outputs a checkpoint.  We do this by saving all counter
%registers in backup macros starting with |\pr@c@| in their name.  A
%checkpoint first writes out all changed counters (previously
%unchecked counters are not written out unless different from zero),
%then saves all involved counter values.  \LaTeX\ tracks its counters
%in the global variable \cmd{\cl@ckpt}.
%    \begin{macrocode}
%<counters>\ifPreview\else\expandafter\endinput\fi
%<counters>\def\pr@eltprint#1{\expandafter\@gobble\ifnum\value{#1}=0%
%<counters>  \csname pr@c@#1\endcsname\else\relax
%<counters>  \space{#1}{\arabic{#1}}\fi}
%<counters>\def\pr@eltdef#1{\expandafter\xdef
%<counters>  \csname pr@c@#1\endcsname{\arabic{#1}}}
%<counters>\def\pr@ckpt#1{{\let\@elt\pr@eltprint\edef\next{\cl@@ckpt}%
%<counters>  \ifx\next\@empty\else\typeout{Preview: Counters\next#1}%
%<counters>  \let\@elt\pr@eltdef\cl@@ckpt\fi}}
%<counters>\g@addto@macro\pr@ship@start{\pr@ckpt:}
%<counters>\g@addto@macro\pr@ship@end{\pr@ckpt.}
%    \end{macrocode}
%
%\subsection{Debugging options}
%Those are for debugging the operation of |preview|, and thus are
%mostly of interest for people that want to use |preview| for their
%own purposes.  Since debugging output is potentially confusing to the
%error message parsing from AUC~\TeX, you should not turn on
%|\tracingonline| or switch from |\nonstopmode| unless you are certain
%your package will never be used with \previewlatex.
%
%\paragraph{The \texttt{showbox} option} will generate diagnostic
%output for every produced box.  It does not
%delay the resetting of the |\showboxbreadth| and |\showboxdepth|
%parameters so that you can still change them after the loading of the
%package.  It does, however, move them to the end of the package
%loading, so that they will not be affected by the |auctex| option.
%    \begin{macrocode}
%<showbox>\ifPreview\else\expandafter\endinput\fi
%<showbox>\AtEndOfPackage{%
%<showbox>  \showboxbreadth\maxdimen
%<showbox>  \showboxdepth\maxdimen}
%<showbox>\g@addto@macro\pr@ship@end{\showbox\pr@box}
%    \end{macrocode}
%
%\paragraph{The \texttt{tracingall} option} is for the really heavy
%diagnostic stuff.  For the reasons mentioned above, we do not want to
%change the setting of the interaction mode, nor of the
%|tracingonline| flag.  If the user wants them different, he should
%set them outside of the preview boxes.
%    \begin{macrocode}
%<tracingall>\ifPreview\else\expandafter\endinput\fi
%<tracingall>\pr@addto@front\pr@ship@start{\let\tracingonline\count@
%<tracingall>  \let\errorstopmode\@empty\tracingall}
%    \end{macrocode}
%
%\subsection{Supporting conversions}
%It is not uncommon to want to use the results of |preview| as
%images.  One possibility is to generate a flurry of EPS files with
%\begin{quote}
%  |dvips -E -i -Ppdf -o| \meta{outputfile}|.000| \meta{inputfile}
%\end{quote}
%However, in case those are to be processed further into graphic image
%files by GhostScript, this process is inefficient.  One cannot use
%GhostScript in a single run for generating the files, however, since
%one needs to set the page size (or full size pages will be
%produced).  The |tightpage| option will set the page dimensions at
%the start of each PostScript page so that the output will be sized
%appropriately.  That way, a single pass of Dvips followed by a single
%pass of GhostScript will be sufficient for generating all images.
%
%You will usually want to use the |dvips| option along with this
%option, so that the page size is not being tampered with.
%
%  \begin{macro}{\PreviewBorder}
%  \begin{macro}{\PreviewBbAdjust}
%  We start this off with the user tunable parameters which get
%  defined even in the case of an inactive package, so that
%  redefinitions and assignments to them will always work:
%    \begin{macrocode}
%<tightpage>\newdimen\PreviewBorder
%<tightpage>\PreviewBorder=0.50001bp
%<tightpage>\def\PreviewBbAdjust{-\PreviewBorder -\PreviewBorder
%<tightpage>  \PreviewBorder \PreviewBorder}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%Here is stuff used for parsing this:
%    \begin{macrocode}
%<tightpage>\ifPreview\else\expandafter\endinput\fi
%<tightpage>\def\pr@nextbb{\edef\next{\next\space\number\dimen@}%
%<tightpage>  \advance\count@\m@ne\ifnum\count@>\z@
%<tightpage>  \afterassignment\pr@nextbb\dimen@\fi}
%    \end{macrocode}
%And here is the stuff that we fudge into our hook.  Of course, we
%have to do it in a box, and we start this box off with our special.
%There is one small consideration here:  it might come before any
%|\AtBeginDvi| stuff containing header specials.  It turns out Dvips
%rearranges this amicably: header code specials get transferred to the
%appropriate header section, anyhow, so this ensures that we come
%right after the bop section.  We insert the 7~numbers here: the
%4~bounding box adjustments, and the 3~\TeX\ box dimensions.
%In case the box adjustments have changed since the last time, we
%write them out to the console.
%    \begin{macrocode}
%<tightpage>\global\let\pr@bbadjust\@empty
%<tightpage>\pr@addto@front\pr@ship@end{\begingroup
%<tightpage>  \let\next\@gobble
%<tightpage>  \count@4\afterassignment\pr@nextbb
%<tightpage>  \dimen@\PreviewBbAdjust
%<tightpage>  \ifx\pr@bbadjust\next
%<tightpage>  \else \global\let\pr@bbadjust\next
%<tightpage>  \typeout{Preview: Tightpage \pr@bbadjust}%
%<tightpage>  \fi\endgroup}
%<tightpage>\g@addto@macro\pr@ship@end{\setbox\pr@box\hbox{%
%<tightpage>  \special{ps::\pr@bbadjust\space\number\ht\pr@box\space
%<tightpage>  \number\dp\pr@box\space\number\wd\pr@box}\box\pr@box}}
%    \end{macrocode}
%Ok, here comes the beef.  First we fish the 7~numbers from the
%file with |token| and convert them from \TeX~|sp| to PostScript
%points.
%    \begin{macrocode}
%<tightpage>\preview@delay{\AtBeginDvi{%
%<tightpage>  \special{!userdict begin/bop-hook{%
%<tightpage>     7{currentfile token not{stop}if
%<tightpage>       65781.76 div DVImag mul}repeat
%    \end{macrocode}
%Next we produce the horizontal part of the bounding box as
%\[ (1\mathrm{in},1\mathrm{in}) +
%\bigl(\min(|\wd\pr@box|,0),\max(|\wd\pr@box|,0)\bigr) \]
%and roll it to the bottom of the stack:
%    \begin{macrocode}
%<tightpage>     72 add 72 2 copy gt{exch}if 4 2 roll
%    \end{macrocode}
%Next is the vertical part of the bounding box.  Depth counts in
%negatively, and we again take $\min$ and $\max$ of possible extents
%in the vertical direction, limited by 0.  720 corresponds to
%$10\,\mathrm{in}$ and is the famous $1\,\mathrm{in}$ distance away
%from the edge of letterpaper.
%    \begin{macrocode}
%<tightpage>     neg 2 copy lt{exch}if dup 0 gt{pop 0 exch}%
%<tightpage>     {exch dup 0 lt{pop 0}if}ifelse 720 add exch 720 add
%<tightpage>     3 1 roll
%    \end{macrocode}
%Ok, we now have the bounding box on the stack in the proper order
%llx, lly, urx, ury.  We add the adjustments:
%    \begin{macrocode}
%<tightpage>    4{5 -1 roll add 4 1 roll}repeat
%    \end{macrocode}
%The page size is calculated as the appropriate differences, the page
%offset consists of the coordinates of the lower left corner, with
%those coordinates negated that would be reckoned positive in the
%device coordinate system.
%    \begin{macrocode}
%<tightpage>     <</PageSize[5 -1 roll 6 index sub 5 -1 roll 5 index sub]%
%<tightpage>       /PageOffset[7 -2 roll [1 1 dtransform exch]%
%<tightpage>       {0 ge{neg}if exch}forall]>>setpagedevice%
%    \end{macrocode}
%So we now bind the old definition of |bop-hook| into our new
%definition and finish it.
%    \begin{macrocode}
%<tightpage>     //bop-hook exec}bind def end}}}%
%    \end{macrocode}
%
%\subsection{The \texttt{showlabels} option}
%During the editing process, some people like to see the label names
%in their equations, figures and the like.  Now if you are using Emacs
%for editing, and in particular \previewlatex, I'd strongly recommend
%that you check out the ref\TeX\ package which pretty much obliterates
%the need for this kind of functionality.  If you still want it,
%standard \LaTeX\ provides it with the |showkeys| package, and there is
%also the less encompassing |showlabels| package.  Unfortunately,
%since those go to some pain not to change the page layout and
%spacing, they also don't change |preview|'s idea of the \TeX\
%dimensions of the involved boxes.
%
%So those packages are mostly useless.  So we present here an
%alternative hack that will get the labels through.
%  \begin{macro}{\pr@labelbox}
%    This works by collecting them into a separate box which we then
%    tack to the right of the previews.
%    \begin{macrocode}
%<showlabels>\ifPreview\else\expandafter\endinput\fi
%<showlabels>\newbox\pr@labelbox
%    \end{macrocode}
%  \end{macro}
%  \begin{macro}{\pr@label}
%    We follow up with our own definition of the \cmd{\label} macro
%    which will be active only in previews.  The original definition
%    is stored in |\pr@@label|.  |\pr@lastlabel| contains the last
%    typeset label in order to avoid duplication in certain
%    environments, and we keep the stuff in |\pr@labelbox|.
%    \begin{macrocode}
%<showlabels>\def\pr@label#1{\pr@@label{#1}%
%    \end{macrocode}
%    Ok, now we generate the box, by placing the label below any
%    existing stuff.
%    \begin{macrocode}
%<showlabels>   \ifpr@setbox\z@{#1}%
%<showlabels>     \global\setbox\pr@labelbox\vbox{\unvbox\pr@labelbox
%<showlabels>      \box\z@}\egroup\fi}
%    \end{macrocode}
%  \end{macro}
%  \begin{macro}{\ifpr@setbox}
%    |\ifpr@setbox| receives two arguments, |#1| is the box into which
%    to set a label, |#2| is the label text itself.  If a label needs
%    to be set (if it is not a duplicate in the current box, and is
%    nonempty, and we are in the course of typesetting and so on), we
%    are left in a true conditional and an open group with the preset
%    box.  If nothing should be set, no group is opened, and we get
%    into skipping to the closing of the conditional.  Since
%    |\ifpr@setbox| is a macro, you should not place the call to it
%    into conditional text, since it will not pair up with |\fi| until
%    being expanded.
%
%    We have some trickery involved here.  |\romannumeral\z@| expands
%    to empty, and will also remove everything between the two of them
%    that also expands to empty, like a chain of |\fi|.
%    \begin{macrocode}
%<showlabels>\def\ifpr@setbox#1#2{%
%<showlabels>  \romannumeral%
%<showlabels>  \ifx\protect\@typeset@protect\ifpr@outer\else
%    \end{macrocode}
%    Ignore empty labels\dots
%    \begin{macrocode}
%<showlabels>   \z@\bgroup
%<showlabels>   \protected@edef\next{#2}\@onelevel@sanitize\next
%<showlabels>   \ifx\next\@empty\egroup\romannumeral\else
%    \end{macrocode}
%    and labels equal to the last one.
%    \begin{macrocode}
%<showlabels>   \ifx\next\pr@lastlabel\egroup\romannumeral\else
%<showlabels>   \global\let\pr@lastlabel\next
%<showlabels>   \setbox#1\pr@boxlabel\pr@lastlabel
%<showlabels>   \expandafter\expandafter\romannumeral\fi\fi\fi\fi
%<showlabels>   \z@\iffalse\iftrue\fi}
%    \end{macrocode}
%  \end{macro}
%  \begin{macro}{\pr@boxlabel}
%    Now the actual typesetting of a label box is done.  We use a
%    small typewriter font inside of a framed box (the default
%    frame/box separating distance is a bit large).
%    \begin{macrocode}
%<showlabels>\def\pr@boxlabel#1{\hbox{\normalfont
%<showlabels>   \footnotesize\ttfamily\fboxsep0.4ex\relax\fbox{#1}}}
%    \end{macrocode}
%  \end{macro}
%  \begin{macro}{\pr@maketag}
%    And here is a version for |amsmath| equations.  They look better
%    when the label is right beside the tag, so we place it there, but
%    augment |\box\pr@labelbox| with an appropriate placeholder.
%    \begin{macrocode}
%<showlabels>\def\pr@maketag#1{\pr@@maketag{#1}%
%<showlabels>  \ifpr@setbox\z@{\df@label}%
%<showlabels>      \global\setbox\pr@labelbox\vbox{%
%<showlabels>         \hrule\@width\wd\z@\@height\z@
%<showlabels>         \unvbox\pr@labelbox}%
%    \end{macrocode}
%    Set the width of the box to empty so that the label placement
%    gets not disturbed, then append it.
%    \begin{macrocode}
%<showlabels>        \wd\z@\z@\box\z@ \egroup\fi}
%    \end{macrocode}
%  \end{macro}
%  \begin{macro}{\pr@lastlabel}
%    Ok, here is how we activate this: we clear out box and label info
%    \begin{macrocode}
%<showlabels>\g@addto@macro\pr@ship@start{%
%<showlabels>  \global\setbox\pr@labelbox\box\voidb@x
%<showlabels>  \xdef\pr@lastlabel{}%
%    \end{macrocode}
%    The definitions above are global because we might be in any
%    amount of nesting.  We then reassign the appropriate labelling
%    macros:
%    \begin{macrocode}
%<showlabels>  \let\pr@@label\label \let\label\pr@label
%<showlabels>  \let\pr@@maketag\maketag@@@ \let\maketag@@@\pr@maketag
%<showlabels>}
%    \end{macrocode}
%  \end{macro}
%Now all we have to do is to add the stuff to the box in question.
%    \begin{macrocode}
%<showlabels>\pr@addto@front\pr@ship@end{%
%<showlabels>   \ifvoid\pr@labelbox
%<showlabels>   \else \setbox\pr@box\hbox{%
%<showlabels>         \box\pr@box\,\box\pr@labelbox}%
%<showlabels>   \fi}
%    \end{macrocode}
%
% \section{Various driver files}
% The installer, in case it is missing.  If it is to be used via
% |make|, we don't specify an installation path, since
%\begin{quote}
%|make install|
%\end{quote}
% is supposed to cater for the installation itself.
%    \begin{macrocode}
%<installer> \input docstrip
%<installer&make> \askforoverwritefalse
%<installer> \generate{
%<installer>    \file{preview.drv}{\from{preview.dtx}{driver}}
%<installer&!make>    \usedir{tex/latex/preview}
%<installer>    \file{preview.sty}{\from{preview.dtx}{style}
%<installer>                       \from{preview.dtx}{style,active}}
%<installer>    \file{prauctex.def}{\from{preview.dtx}{auctex}}
%<installer>    \file{prauctex.cfg}{\from{preview.dtx}{auccfg}}
%<installer>    \file{prshowbox.def}{\from{preview.dtx}{showbox}}
%<installer>    \file{prshowlabels.def}{\from{preview.dtx}{showlabels}}
%<installer>    \file{prtracingall.def}{\from{preview.dtx}{tracingall}}
%<installer>    \file{prtightpage.def}{\from{preview.dtx}{tightpage}}
%<installer>    \file{prlyx.def}{\from{preview.dtx}{lyx}}
%<installer>    \file{prcounters.def}{\from{preview.dtx}{counters}}
%<installer> }
%<installer> \endbatchfile
%    \end{macrocode}
% And here comes the documentation driver.
%    \begin{macrocode}
%<driver> \documentclass{ltxdoc}
%<driver> \newcommand\previewlatex{Preview-\LaTeX}
%<driver> \begin{document}
%<driver> \DocInput{preview.dtx}
%<driver> \end{document}
%    \end{macrocode}
% \Finale{}
