% \iffalse 
%<*internal>
\iffalse
%</internal>
%<*readme>
________________________
The auto-pst-pdf package
v0.5          2008/03/14
 
The package uses -shell-escape to execute pst-pdf. 
This makes it especially easy to integrate into the 
workflow of an editor with just "LaTeX" and "pdfLaTeX" 
buttons.

Wrappers are provided for various psfrag-related 
features so that Matlab figures via laprint, Mathematica 
figures via MathPSfrag, and regular psfrag figures can 
all be input consistently and easily.

Will Robertson  -  wspr 81 at gmail dot com
Johannes Große

Copyright 2007
Distributed under the LaTeX Project Public License
%</readme>
%<*internal>
\fi
\begingroup
%</internal>
%<*batchfile>
\input docstrip.tex
\keepsilent
\preamble
  __________________________________
  Copyright (C) 2007  Will Robertson
  License information appended.
\endpreamble
\postamble
Copyright (C) 2007 by Will Robertson & Johannes Große

Distributable under the LaTeX Project Public License,
version 1.3c or higher (your choice). The latest version of
this license is at: http://www.latex-project.org/lppl.txt

This work is "maintained" (as per LPPL maintenance status) 
by Will Robertson.

This work consists of the file  auto-pst-pdf.dtx
          and the derived files auto-pst-pdf.pdf,
                                auto-pst-pdf.sty, and 
                                auto-pst-pdf.ins.
\endpostamble
\askforoverwritefalse
\generate{\file{\jobname.sty}{\from{\jobname.dtx}{package}}}
%</batchfile>
%<batchfile>\endbatchfile
%<*internal>
\generate{\file{\jobname.ins}{\from{\jobname.dtx}{batchfile}}}
\nopreamble\nopostamble
\generate{\file{README.txt}{\from{\jobname.dtx}{readme}}}
\generate{\file{dtx-style.sty}{\from{\jobname.dtx}{dtx-style}}}
\endgroup
\immediate\write18{mv README.txt README}
\immediate\write18{makeindex -s gind.ist -o \jobname.ind  \jobname.idx}
\immediate\write18{makeindex -s gglo.ist -o \jobname.gls  \jobname.glo}
%</internal>
%
%<*driver>
\documentclass{ltxdoc}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
%\OnlyDescription
\usepackage{dtx-style}
\begin{document}
  \DocInput{auto-pst-pdf.dtx}
\end{document}
%</driver>
%
%
% \fi
%
% \GetFileInfo{\jobname.sty}
% \CheckSum{0}
% \makeatletter
%
% \title{The \pkg{auto-pst-pdf} package}
% \author{Will Robertson \& Johannes Gro\ss{}e\\
%         \texttt{\color{gray}wspr 81 at gmail dot com}}
% \date{\filedate \qquad \fileversion}
%
% \maketitle
%
% \section{Basic usage}
% This package provides a wrapper around \pkg{pst-pdf} to automatically
% accomodate for typesetting either with \DVI\ or \PDF\ output. With
% default package option |[on]|, typesetting under pdf\/\LaTeX\ will
% automatically initiate an auxiliary compilation of \LaTeX $\to$ |dvips| $\to$ |ps2pdf| $\to$ |pdfcrop| to generate the required \PDF\ figures for the document.
%
% After this has been done and the figures no longer need to be
% re-generated, the package can be given the |[off]| option to save
% compilation time:\par
% {\color{gray}|\usepackage[|\color{niceblue}|off|\color{gray}|]{auto-pst-pdf}|}
%
% \noindent
% If the extension of your \LaTeX\ document is not |.tex|, then it must be declared when the package is loaded (\eg, I like to use |.ltx| to distinguish between Plain \TeX\ and \LaTeX\ files):\par
% {\color{gray}|\usepackage[|\color{niceblue}|ext=ltx|\color{gray}|]{auto-pst-pdf}|}
%
% \section{Requirements}
% pdf\TeX\ must be called with the |-shell-escape| option.
% Requires the following packages: \pkg{ifplatform}, \pkg{pst-pdf}, \pkg{xkeyval}.
%
% Heiko Oberdiek's |pdfcrop| Perl script
%  \note{\url{http://www.ctan.org/tex-archive/support/pdfcrop/}} 
% must be installed for the default {\color{niceblue}|crop=on|} option (see section~\ref{sec:opt}).
% Under Windows, a Perl installation 
%   \note{Freely available: \url{http://www.activestate.com/Products/activeperl/index.plex}} 
% will also need to be installed even though |pdfcrop| itself is part of MiK\TeX.
%
% \section{Provided macros for including graphics}
% Macros are provided to easily facilitate figures created by the
% \textsc{Matlab} package \pkg{laprint}
%  \note{\url{http://www.uni-kassel.de/fb16/rat/matlab/laprint/}} 
% and the Mathematica package \pkg{MathPSfrag}
%  \note{\url{http://wwwth.mppmu.mpg.de/members/jgrosse/mathpsfrag/}}. Also,
% a generic \pkg{psfrag}
%  \note{\url{http://www.ctan.org/tex-archive/help/Catalogue/entries/psfrag.html}} 
% wrapper is provided.
%
% \begin{trivlist}\item[]
% \begin{tabular}{@{\hskip 2em}lp{7cm}}
%  \cmd\mathfig\marg{filename}   & \raggedright 
%    insert a Mathematica graphic from \pkg{MathPSfrag}
%    (without |-psfrag| suffix)\arraybackslash \\
%  \cmd\matlabfig\marg{filename} & 
%    insert a \textsc{Matlab} graphic from \pkg{laprint} \\
%  \cmd\psfragfig\marg{filename} & 
%    insert an \EPS\ with \pkg{psfrag}
% \end{tabular}
% \end{trivlist}
%
% \noindent The above commands all accept an optional argument 
% which is passed to the underlying \cmd\includegraphics\ macro.
%
% The \cmd\matlabfig\ command meddles slightly with the output of
% \pkg{laprint}; the font sizes in the figure will always be as 
% originally defined. (This is unavoidable I'm afraid.)
%
% For the \cmd\psfragfig\ command, \pkg{psfrag} statements are input 
% from either or both of the files \meta{document}|-psfrag.tex| and 
% \meta{filename}|-psfrag.tex| if they exist. Furthermore, supplementary
% \cmd\psfrag\ statements can be added in a trailing optional argument:
%
% |\psfragfig|\oarg{graphics options}\marg{filename}\oarg{psfrag statements}
%
% \noindent Manual \cmd\psfrag s override those in \meta{filename}|-psfrag.tex|
% which in turn override those in \meta{document}|-psfrag.tex|.
%
%
% \section{Advanced package options}\label{sec:opt}
%
% Better results are obtained by using |pdfcrop| in the auxiliary compilation process, and this is used by default. It is not installed by default, however, and will not always be required. Cropping with this tool can be controlled with the |crop| option:
%
% {\color{gray}|\usepackage[|\color{niceblue}|crop=off|\color{gray}|]{auto-pst-pdf}|}
%
% \bigskip\noindent
% The package automatically deletes the files generated during the auxiliary \LaTeX\ compilation. Which files are deleted are chosen by passing a list of file extensions to the |cleanup| option (no error message or warning is produced if a file is specified that does not exist). The default list is:\par
% {\color{gray}|\usepackage[|\color{niceblue}|cleanup={log,aux,dvi,ps,pdf}|\color{gray}|]{auto-pst-pdf}|}
%
% \bigskip\noindent
% The options passed individually to |latex|, |dvips|, |ps2pdf|, and |pdfcrop| in the auxiliary compilation process may all be customised, if you know what you're doing. The defaults for the latter three are\par
% {\color{gray}|\usepackage[|{\color{niceblue}|dvips={-o -Ppdf}|}|,|}\par
% {\color{white}|\usepackage[|{\color{niceblue}|pspdf={-dAutoRotatePages=/None}}|}\color{gray}|,|}\par
% {\color{white}|\usepackage[|\color{niceblue}|pdfcrop={}|\color{gray}|]{auto-pst-pdf}|}
%
% \bigskip\noindent
% The \LaTeX\ auxiliary compilation has some hard-coded options (see the source if you're interested), and further options can be appended if you wish. For example, to run the auxiliary compilation with more information written to the console, use the following package option:
%
% {\color{gray}|\usepackage[|\color{niceblue}|latex={-interaction=nonstopmode}|\color{gray}|]{auto-pst-pdf}|}
%
%
% \section{Acknowledgements}
%
% Many thanks to the authors of \pkg{pst-pdf}, \pkg{psfrag}, \pkg{laprint}, \pkg{MathPSfrag}, and \pkg{pdfcrop}. This package could not exist without their combined efforts over many years. Finally, Gernot \textsc{Hassenfplug} deserves special mention for extensive testing, feature suggestions, and moral support |:)| Thanks, mate.
%
% \StopEventually{}
%
% \clearpage
%
%
% \part{\pkg{\jobname} implementation}
%\iffalse
%<*package>
%\fi
%
% \section{Setup code}
% This is the package.
%
%    \begin{macrocode}
\ProvidesPackage{auto-pst-pdf}[2008/03/14 v0.5 Wrapper for pst-pdf]
%    \end{macrocode}
%
% \PrintChanges
% \changes{v0.5}{2007/11/22}{\texttt{delay} option removed. Keep it simple, stupid!}
% \changes{v0.4}{2007/11/11}{Will sorted it all out.}
% \changes{v0.4}{2007/11/09}{Johannes tinkered with the code. Will will improve. :-)}
% \changes{v0.3}{2007/11/09}{Too many changes to list. Command execution totally re-written.}
%
% \paragraph{Required packages}
% \pkg{pst-pdf} is loaded later on.
%    \begin{macrocode}
\RequirePackage{ifpdf,xkeyval,ifplatform}
%    \end{macrocode}
%
% \paragraph{Things we need}
%    \begin{macrocode}
\newif\if@app@off@
\newif\if@app@crop@
\def\app@suffix{autopp}
\edef\app@jobname{\jobname-\app@suffix}
\edef\app@pics{\jobname-pics.pdf}
%    \end{macrocode}
%
% \paragraph{Option processing}
%    \begin{macrocode}
\DeclareOptionX{off}[]{\@app@off@true}
\define@choicekey{auto-pst-pdf.sty}{crop}[\@tempa\@tempb]{on,off}{%
  \ifcase\@tempb\relax 
    \@app@crop@true
  \or 
    \@app@crop@false 
  \fi}
\DeclareOptionX{on}[]{\@app@off@false}
\DeclareOptionX{ext}{\def\app@ext{#1}}
\DeclareOptionX{latex}{%
  \def\app@latex@opts{%
    \ifwindows
      -disable-write18 
    \else
      -no-shell-escape 
    \fi
    -jobname="\app@jobname"
    -interaction=batchmode
    #1}}
\DeclareOptionX{dvips}{\def\app@dvips@opts{#1}}
\DeclareOptionX{pspdf}{\def\app@pspdf@opts{#1}}
\DeclareOptionX{pdfcrop}{\def\app@pdfcrop@opts{#1}}
\DeclareOptionX{cleanup}{%
  \let\app@rm@files\@empty
  \@for\@ii:=#1\do{%
    \edef\app@rm@files{\app@rm@files,\app@jobname.\@ii}}}
\ExecuteOptionsX{%
    ext=tex,
    crop=on,
    latex={},
    dvips={-Ppdf},
    pspdf={},
    pdfcrop={},
    cleanup={log,aux,dvi,ps,pdf}}
\ifwindows
  \ExecuteOptionsX{pspdf={}}
\else
  \ExecuteOptionsX{pspdf={-dAutoRotatePages=/None}}
\fi
\ProcessOptionsX
%    \end{macrocode}
%
% \paragraph{Shorthands}
%    \begin{macrocode}
\def\app@exe{\immediate\write18}
\def\app@nl{^^J\space\space\space\space}
\newcommand\app@PackageError[2]{%
  \PackageError{auto-pst-pdf}{\app@nl #1^^J}{#2}}
\newcommand\app@PackageWarning[1]{%
  \PackageWarning{auto-pst-pdf}{\app@nl #1^^JThis warning occured}}
\newcommand\app@PackageInfo[1]{\PackageInfo{auto-pst-pdf}{#1}}
%    \end{macrocode}
%
% These are cute:
%    \begin{macrocode}
\newcommand\OnlyIfFileExists[2]{\IfFileExists{#1}{#2}{}}
\newcommand\NotIfFileExists[2]{\IfFileExists{#1}{}{#2}}
%    \end{macrocode}
%
% \begin{macro}{\app@convert}
% \darg{command name}
% \darg{source file}
% \darg{destination file}
% Check if the source file exists and calls the command to generate the destination file. If the final file is not created, generate an error.
%    \begin{macrocode}
\def\app@convert#1#2#3{%
  \OnlyIfFileExists{#2}{%
    \app@exe{\csname app@cmd@#1\endcsname{#2}{#3}}%
    \NotIfFileExists{#3}{\app@PackageWarning{Creation of #3 failed.}}}}
%    \end{macrocode}
% \changes{v0.5}{2007/11/21}{Fix PackageError (should have been a warning).}
% \end{macro}
%
% \begin{macro}{\app@compile}
% First we define the entire |latex| $\to$ |dvips| $\to$ |ps2pdf| ($\to$ |pdfcrop|) command sequence. The actual call to the compilation macro follows thereafter. This macro contains the actual creation of the pdf container. Each processing step is in a separate macro to allow simple modification.
%    \begin{macrocode}
\def\app@compile{%
  \app@cleanup
  \app@remove@container
  \app@convert{latex}{\jobname.\app@ext}{\app@jobname.dvi}%
  \app@convert{dvips}{\app@jobname.dvi}{\app@jobname.ps}%
  \if@app@crop@  
    \app@convert{pstopdf}{\app@jobname.ps}{\app@jobname.pdf}%
    \app@convert{pdfcrop}{\app@jobname.pdf}{\app@pics}%
  \else
    \app@convert{pstopdf}{\app@jobname.ps}{\app@pics}%
  \fi
  \IfFileExists{\app@pics}
    {\app@cleanup}
    {\app@PackageWarning{Could not create \app@pics. 
      Auxiliary files not deleted.}}}
%    \end{macrocode}
% \end{macro}
% 
% Command-line program to delete files: 
%    \begin{macrocode}
\edef\app@rm{\ifwindows del \else rm -- \fi}
%    \end{macrocode}
%
% \begin{macro}{\app@try@rm}
% Macro to delete files (comma-separated) if they exist:
%    \begin{macrocode}
\newcommand\app@try@rm[1]{%
  \@for\@tempa:=#1\do{%
    \OnlyIfFileExists{\@tempa}{\app@exe{\app@rm "\@tempa"}}}}
%    \end{macrocode}
% \end{macro}
%
% Remove pdf picture container:
%    \begin{macrocode}
\def\app@remove@container{\app@try@rm{\app@pics}}
%    \end{macrocode}
% Clean up auxiliary files: (\cmd\app@rm@files\ defined by the |cleanup| package option)
%    \begin{macrocode}
\def\app@cleanup{\app@try@rm{\app@rm@files}}
%    \end{macrocode}
% \LaTeX:
%    \begin{macrocode}
\def\app@cmd@latex#1#2{latex \app@latex@opts\space 
  "\let\noexpand\APPmakepictures\noexpand\empty\noexpand\input #1"}
%    \end{macrocode}
% |dvips|:
%    \begin{macrocode}
\def\app@cmd@dvips#1#2{dvips \app@dvips@opts\space -o "#2" "#1"}
%    \end{macrocode}
% |ps2pdf|:
%    \begin{macrocode}
\def\app@cmd@pstopdf#1#2{ps2pdf \app@pspdf@opts\space "#1" "#2"}
%    \end{macrocode}
% |pdfcrop|:
%    \begin{macrocode}
\def\app@cmd@pdfcrop#1#2{pdfcrop \app@pdfcrop@opts\space "#1" "#2"}
%    \end{macrocode}
%
% \begin{subsection}{Base functionality}
%
% For compilation, we use the |[notightpage]| option of |pst-pdf| and the
% |pdfcrop| Perl script because \EPS\ figures can have elements
% that extend ouside their bounding boxes, and end up with clipped
% content after |ps2pdf|. Otherwise the script |ps4pdf| would be sufficient.
%
% \paragraph{pdf\LaTeX\ compilation}
% Requires supplementary processing with \pkg{pst-pdf}:
%    \begin{macrocode}
\ifpdf
  \if@app@off@\else
    \ifshellescape
      \app@exe{echo "  "}
      \app@exe{echo "-------------------------------------------------"}
      \app@exe{echo "auto-pst-pdf: Auxiliary LaTeX compilation"}
      \app@exe{echo "-------------------------------------------------"}
      \app@compile
      \app@exe{echo "-------------------------------------------------"}
      \app@exe{echo "auto-pst-pdf: End auxiliary LaTeX compilation"}
      \app@exe{echo "-------------------------------------------------"}
    \else
      \app@PackageError{%
        "shell escape" (or "write18") is not enabled:\app@nl 
        auto-pst-pdf will not work!}
       {You need to run LaTeX with the equivalent of 
        "pdflatex -shell-escape"\app@nl 
        Or turn off auto-pst-pdf.}%
    \fi
  \fi
  \if@app@crop@
    \PassOptionsToPackage{notightpage}{pst-pdf}
  \fi
%    \end{macrocode}
% \changes{v0.5}{2008/03/14}{Removed mucking about with image extensions.}
%
% \paragraph{\LaTeX\ compilation}
% Either we're calling |latex| from within a pdf\LaTeX\ run (see above) or the document is being compiled as usual.
%    \begin{macrocode}
\else
%    \end{macrocode}
% \LaTeX\ compilation from scratch (as in `|latex| \meta{document}|.tex|`) --- here the |postscript| environment does nothing and document is processed `normally':
% \changes{v0.5}{2007/11/22}{Removed \cs{ifdefined} to avoid e-\TeX.}
%    \begin{macrocode}
  \ifx\APPmakepictures\@undefined    
    \PassOptionsToPackage{inactive}{pst-pdf}
%    \end{macrocode}
% \LaTeX\ compilation induced by this package:
%    \begin{macrocode}
  \else
    \if@app@crop@
      \PassOptionsToPackage{notightpage}{pst-pdf}
    \fi
  \fi
\fi
%    \end{macrocode}
%
% After the requisite package options have been declared depending on the execution mode, it's now time to load the package:
%    \begin{macrocode}
\RequirePackage{pst-pdf}
%    \end{macrocode}
%
% \end{subsection}
%
% \begin{subsection}{Extras for external packages}
%
% Commands are provided that mirror \cmd\includegraphics\ (and similarly
% accept an optional argument) for the output of different
% psfrag-related packages. This provides a consistent and easy way to
% include such figures in the document.
%
% Please suggest wrappers for other packages that output psfrag figures
% (for example: SciLab, R, Maple, LabView, Sage, ... ?)
% 
% \begin{macro}{\matlabfig}
% We need to disable the scaling that laprint applies
% to \cmd\includegraphics\ in here, because otherwise labels that extend
% outside the bounding box of the generated PostScript file will
% change the intended width of the graphic.
%    \begin{macrocode}
\let\app@ig\includegraphics
\newcommand\matlabfig[2][]{%
  \begin{postscript}
    \renewcommand\resizebox[3]{##3}%
    \renewcommand\includegraphics[2][]{\app@ig[#1]{##2}}%
    \input{#2}%
  \end{postscript}}
%    \end{macrocode}
% \changes{v0.5}{2007/11/21}{Redefine \cmd\resizebox\ for \texttt{laprint}.}
% \end{macro}
%
% \begin{macro}{\mathfig}
% For Mathematica's \pkg{MathPSfrag} output
%    \begin{macrocode}
\newcommand\mathfig[2][]{%
  \begin{postscript}
    \input{#2-psfrag}%
    \includegraphics[#1]{#2-psfrag}%
  \end{postscript}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\psfragfig}
% \EPS\ graphics via \pkg{psfrag}.
% Include your psfrag commands in the files \meta{document}-psfrag.tex
% and/or \meta{figname}-psfrag.tex, where \meta{document} is the filename of the
% main document and \meta{figname} is the filename of the graphics
% inserted.
%    \begin{macrocode}
\newcommand\psfragfig[2][]{%
  \@ifnextchar[
    {\app@psfragfig[#1]{#2}}
    {\app@psfragfig[#1]{#2}[]}}
\def\app@psfragfig[#1]#2[#3]{%
  \begin{postscript}
    \InputIfFileExists{#2-psfrag}{}{}%
    #3
    \includegraphics[#1]{#2}%
  \end{postscript}}
%    \end{macrocode}
% Finally, input any psfrag commands associated with the document:
%    \begin{macrocode}
\InputIfFileExists{\jobname-psfrag}{}{}
%    \end{macrocode}
% \changes{v0.5}{2007/11/21}{Extend \cmd\psfragfig\ to accept arbitrary input for extra \cmd\psfrag\ commands.}
% \end{macro}
%
% \end{subsection}
%
%\iffalse
%</package>
%<*dtx-style>
%    \begin{macrocode}
\ProvidesPackage{dtx-style}
\errorcontextlines=999

\setcounter{IndexColumns}{2}
\renewenvironment{theglossary}
  {\small\list{}{}
     \item\relax
     \glossary@prologue\GlossaryParms 
     \let\item\@idxitem \ignorespaces 
     \def\pfill{\hspace*{\fill}}}
  {\endlist}

\usepackage[svgnames]{xcolor}
\usepackage{array,booktabs,calc,enumitem,fancyvrb,hyperref}
\usepackage[off]{auto-pst-pdf}
\usepackage[sc,osf]{mathpazo}

\hypersetup{colorlinks,linkcolor=black,urlcolor=black}

\linespread{1.1}      % A bit more space between lines
\frenchspacing         % Remove ugly extra space after punctuation
  
\definecolor{niceblue}{rgb}{0.2,0.4,0.8}
\def\theCodelineNo{\textcolor{niceblue}{\sffamily\tiny\arabic{CodelineNo}}}

\newcommand*\name[1]{{#1}}
\newcommand*\pkg[1]{\textsf{#1}}
\newcommand*\feat[1]{\texttt{#1}}
\newcommand*\opt[1]{\texttt{#1}}

\newcommand\note{\unskip\footnote}

\let\latin\textit
\def\eg{\latin{e.g.}}
\def\Eg{\latin{E.g.}}
\def\ie{\latin{i.e.}}
\def\etc{\@ifnextchar.{\latin{etc}}{\latin{etc.}\@}}

\def\DVI{\textsc{dvi}}
\def\PDF{\textsc{pdf}}
\def\EPS{\textsc{eps}}

\def\MacOSX{Mac~OS~X}
\def\ascii{\textsc{ascii}}
\def\CTAN{\textsc{ctan}}

\newcounter{argument}
\g@addto@macro\endmacro{\setcounter{argument}{0}}
\newcommand*\darg[1]{%
  \stepcounter{argument}%
  {\ttfamily\char`\#\theargument~:~}#1\par\noindent\ignorespaces}
\newcommand*\doarg[1]{%
  \stepcounter{argument}%
  {\ttfamily\makebox[0pt][r]{[}\char`\#\theargument]:~}#1\par\noindent\ignorespaces}

\def\codeline{\par\hspace{\parindent}}% two \indents in total

\setlength\parindent{2em}
\hfuzz2pt
%    \end{macrocode}
%</dtx-style>
%
%\fi
%
% \typeout{*************************************************************}
% \typeout{*}
% \typeout{* To finish the installation you have to move the following}
% \typeout{* file into a directory searched by XeTeX:}
% \typeout{*}
% \typeout{* \space\space\space auto-pst-pdf.sty}
% \typeout{*}
% \typeout{*************************************************************}
%
\endinput
