%%% Asymptote style file for LaTeX
%%% Contributed by Tom Prince 14 May 2003
%%% Modified by John Bowman
%%% Adapted from comment.sty (Under GPL v2+)

\ProvidesPackage{asymptote}[2010/07/05 v1.13 Asymptote style file for LaTeX]
\RequirePackage{keyval}
\RequirePackage{ifthen}
\newif\ifASYinline
\DeclareOption{inline}{\ASYinlinetrue}
\ProcessOptions*

\def\unquoteJobname#1"#2"#3\relax{\def\rawJobname{#1}%
  \ifx\rawJobname\empty\def\rawJobname{#2}\fi}
\expandafter\unquoteJobname\jobname""\relax
% Work around jobname bug in MiKTeX 2.5 and 2.6:
% Turn stars in file names (resulting from spaces, etc.) into minus signs
\def\fixstar#1*#2\relax{\def\argtwo{#2}\ifx\argtwo\empty\gdef\Jobname{#1}\else
\fixstar#1-#2\relax\fi}
\expandafter\fixstar\rawJobname*\relax

\RequirePackage{color,graphicx}

\def\makeinnocent#1{\catcode`#1=12 }
\def\csarg#1#2{\expandafter#1\csname#2\endcsname}
\newwrite\AsyStream
\newwrite\AsyAllStream
\newread\AsyTestTeXStream

\openin\AsyStream=\jobname_.pre
\ifeof\AsyStream
\else
\input \jobname_.pre
\fi
\closein\AsyStream%

\immediate\openout\AsyAllStream=\jobname.asy

\newif\ifASYattach
\newif\ifASYtex
\newif\ifASYPDF

\ifx\pdfoutput\undefined
\else
\ifcase\pdfoutput
\else
\ASYPDFtrue
\fi
\fi

\newbox\ASYbox
\newcounter{asy}

\def\ProcessAsymptote#1% start it all off
{\begingroup
  \def\CurrentAsymptote{#1}%
  \let\do\makeinnocent \dospecials 
  \makeinnocent\^^L% and whatever other special cases
  \endlinechar`\^^M \catcode`\^^M=12 \xAsymptote}
{\catcode`\^^M=12 \endlinechar=-1 %
  \gdef\xAsymptote{%
    \expandafter\ProcessAsymptoteLine}
  \gdef\ProcessAsymptoteLine#1^^M{\def\test{#1}
    \csarg\ifx{End\CurrentAsymptote Test}\test
    \edef\next{\noexpand\EndOfAsymptote{\CurrentAsymptote}}%
    \else \ThisAsymptote{#1}\let\next\ProcessAsymptoteLine
    \fi \next}
}

\def\ASYstringmeaning#1{\expandafter\ASYgobblearrow\meaning#1}
\def\ASYstringcsnoescape#1{\expandafter\ASYgobbleescape\string#1}
{\escapechar-1
  \expandafter\expandafter\expandafter\gdef
  \expandafter\expandafter\expandafter\ASYgobblearrow
  \expandafter\string\csname macro:->\endcsname{}
}
\def\ASYgobbleescape#1{\ifnum`\\=`#1 \else #1\fi}

\def\WriteAsyLine#1{\def\ASYtmp{#1}%
  \immediate\write\AsyStream{\ASYstringmeaning\ASYtmp}}

\def\gedefappend#1#2{%
  \toks@ = \expandafter{#1}%
  \global\edef#1{\the\toks@ #2}%
}%
\def\globalASYdefs{}
\def\WriteGlobalAsyLine#1{\def\ASYtmp{#1^^J}%
\gedefappend\globalASYdefs{\ASYstringmeaning\ASYtmp}}

\def\EndOfAsymptote#1{\endgroup\end{#1}%
    \csname After#1Asymptote\endcsname}
\def\AsymptoteEndDef#1{{\escapechar=-1\relax
    \csarg\xdef{End#1Test}{\string\\end\string\{#1\string\}}%
  }}

\def\AfterasyAsymptote{%
  \ifx\ASYwidth\empty
    \ifx\ASYheight\empty
    \else
      \immediate\write\AsyStream{size(0,\ASYheight);}%
    \fi
  \else
    \ifx\ASYheight\empty
      \immediate\write\AsyStream{size(\ASYwidth,0);}%
    \else
      \immediate\write\AsyStream{size(\ASYwidth,\ASYheight);}%
    \fi
  \fi
  \ASYtexfalse%
  \ifASYattach
    \def\ASYdefaultviewportwidth{0}%
  \else
    \def\ASYdefaultviewportwidth{\the\linewidth}%
    \ifASYinline
      \ASYtextrue%
    \fi
  \fi
  \ifx\ASYviewportwidth\empty
    \ifx\ASYviewportheight\empty
    \else
      \immediate\write\AsyStream{viewportsize=(0,\ASYviewportheight);}%
    \fi
  \else
    \ifx\ASYviewportheight\empty
      \immediate\write\AsyStream{viewportsize=(\ASYviewportwidth,0);}%
    \else
      \immediate\write%
        \AsyStream{viewportsize=(\ASYviewportwidth,\ASYviewportheight);}%
    \fi
  \fi
  \gdef\ASYwidth{}%
  \gdef\ASYheight{}%
  \gdef\ASYviewportwidth{\ASYdefaultviewportwidth}%
  \gdef\ASYviewportheight{}%
  \immediate\closeout\AsyStream

  \ifASYtex
    \openin\AsyStream=\AsyFile_.tex
  \else
    \ifASYPDF
      \openin\AsyStream=\AsyFile.pdf
    \else
      \openin\AsyStream=\AsyFile.eps
    \fi
  \fi
  \ifeof\AsyStream
    \ifASYtex
      \PackageWarning{asymptote}{file \AsyFile_.tex does not exist}%
    \else
      \openin\AsyTestTeXStream=\AsyFile_.tex
      \ifeof\AsyTestTeXStream
        \ifASYPDF
        \PackageWarning{asymptote}{file \AsyFile.pdf does not exist}%
        \else
        \PackageWarning{asymptote}{file \AsyFile.eps does not exist}%
        \fi
      \else
        \catcode`:=12
        \input \AsyFile_.tex%
      \fi
      \closein\AsyTestTeXStream%
    \fi
    \closein\AsyStream%
  \else
    \closein\AsyStream%
    \ifASYtex
      \catcode`:=12
      \input \AsyFile_.tex%
    \else
      \ifASYattach
        \ifASYPDF
          \openin\AsyStream=\AsyFile+0.pdf
        \fi
        \ifeof\AsyStream
          \setbox\ASYbox=\hbox{\includegraphics[hiresbb]{\AsyFile}}%
        \else
         \setbox\ASYbox=\hbox{\includegraphics[hiresbb]{\AsyFile+0}}%
        \fi
        \textattachfile{\AsyFile.pdf}{\phantom{\copy\ASYbox}}%
        \vskip-\ht\ASYbox%
        \indent%
        \box\ASYbox%
        \closein\AsyStream%
      \else
        \includegraphics[hiresbb]{\AsyFile}%
      \fi
    \fi
  \fi
  \global\ASYattachfalse
  \relax%
  \endgroup}
\gdef\ASYwidth{}%
\define@key{ASYkeys}{width}{%
  \gdef\ASYwidth{#1}%
}
\gdef\ASYheight{}%
\define@key{ASYkeys}{height}{%
  \gdef\ASYheight{#1}%
}
\gdef\ASYviewportwidth{\ASYdefaultviewportwidth}%
\define@key{ASYkeys}{viewportwidth}{%
  \gdef\ASYviewportwidth{#1}%
}
\gdef\ASYviewportheight{}%
\define@key{ASYkeys}{viewportheight}{%
  \gdef\ASYviewportheight{#1}%
}
\define@key{ASYkeys}{attach}[true]{%
  \ifthenelse{\equal{#1}{true}}{\global\ASYattachtrue}{\global\ASYattachfalse}%
}
\newcommand\asy[1][]{%
  \begingroup
  \let\par\empty
  \stepcounter{asy}%
  \setkeys{ASYkeys}{#1}%
   \immediate\write\AsyAllStream%
       {eval(quote\@charlb include "\jobname-\the\c@asy";\@charrb);}%
   \immediate\openout\AsyStream=\jobname-\the\c@asy.asy
   \gdef\AsyFile{\Jobname-\the\c@asy}
   \immediate\write\AsyStream{if(!settings.multipleView)}%
   \immediate\write\AsyStream{  settings.batchView=false;}%
   \ifx\XeTeXversion\undefined
   \ifASYPDF
   \immediate\write\AsyStream{settings.tex="pdflatex";}%
   \fi
   \else
   \immediate\write\AsyStream{settings.tex="xelatex";}%
   \ASYPDFtrue
   \fi

   \ifASYinline
   \immediate\write\AsyStream{settings.inlinetex=true;}%
   \immediate\write\AsyStream{deletepreamble();}%
   \fi
   \immediate\write\AsyStream{defaultfilename="\AsyFile";}%
   \immediate\write\AsyStream{if(settings.render < 0) settings.render=4;}%
   \ifASYattach
     \immediate\write\AsyStream{settings.inlineimage=false;}%
     \immediate\write\AsyStream{settings.embed=false;}%
     \immediate\write\AsyStream{settings.outformat="pdf";}%
     \immediate\write\AsyStream{settings.toolbar=true;}%
   \else
     \immediate\write\AsyStream{settings.inlineimage=true;}%
     \immediate\write\AsyStream{settings.embed=true;}%
     \immediate\write\AsyStream{settings.outformat="";}%
     \immediate\write\AsyStream{settings.toolbar=false;}%
     \immediate\write\AsyStream{viewportmargin=(2,2);}%
   \fi
  \immediate\write\AsyStream{\globalASYdefs}%
  \let\ThisAsymptote\WriteAsyLine%
  \ProcessAsymptote{asy}%
}
\AsymptoteEndDef{asy}
\def\asydef{%
  \let\ThisAsymptote\WriteGlobalAsyLine%
  \ProcessAsymptote{asydef}}
\AsymptoteEndDef{asydef}
\def\AfterasydefAsymptote{}

\AtEndDocument{\immediate\closeout\AsyAllStream}

\newcommand{\ASYanimategraphics}[5][]{%
\openin\AsyStream=_#3.pdf
\ifeof\AsyStream%
\else%
\animategraphics[{#1}]{#2}{_#3}{#4}{#5}%
\fi%
}

% Work around bug in dvips.def: allow spaces in file names.
\def\Ginclude@eps#1{%                                                           
 \message{<#1>}%                                                                
  \bgroup
  \def\@tempa{!}%                                                               
  \dimen@\Gin@req@width
  \dimen@ii.1bp%                                                                
  \divide\dimen@\dimen@ii
  \@tempdima\Gin@req@height
  \divide\@tempdima\dimen@ii
    \special{PSfile=#1\space
      llx=\Gin@llx\space
      lly=\Gin@lly\space
      urx=\Gin@urx\space
      ury=\Gin@ury\space
      \ifx\Gin@scalex\@tempa\else rwi=\number\dimen@\space\fi
      \ifx\Gin@scaley\@tempa\else rhi=\number\@tempdima\space\fi
      \ifGin@clip clip\fi}%                                                     
  \egroup}

\def\Asymptote{{\tt Asymptote}}

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: "shared"
%%% End: 
