% \iffalse meta-comment
% \fi
% \iffalse
%<*driver> 
\documentclass{ltxdoc}  
\usepackage{lmodern}
\usepackage{\jobname}
\usepackage{fancyvrb}
\usepackage{scalerel}
\usepackage{microtype}
\usepackage{amssymb}
\usepackage{framed}
\usepackage[numbered]{hypdoc}

\setlength{\emergencystretch}{0.5\linewidth}
\hypersetup{hidelinks}
\newcommand{\packagename}[1]{\textsf{#1}}
\newcommand{\danger}{\fontencoding{U}\fontfamily{futs}\selectfont\char 49\relax}
\newenvironment{warning}%
{%
    \medskip\noindent\hangindent=3em\hangafter=-2%
    \clubpenalty=10000%
    \hbox to0pt%
    {\hskip-\hangindent\smash%
        {\raisebox{-0.6\baselineskip}{\scaleto{$\danger$}{1.8\baselineskip}}}%
        \hfill%
    }\ignorespaces\textbf{\hspace*{-.2em}Warning:} \ignorespaces%
}{%
    \par\medskip%
}
    
\EnableCrossrefs 
\CodelineIndex 
\begin{document}
    \DocInput{\jobname.dtx} 
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{\jobname.sty}
%
% \title{^^A
%   \packagename{asypictureB} --- user-friendly integration
%    of Asymptote into \LaTeX\thanks{^^A
%    This document
%    corresponds to \packagename{asypictureB}~\fileversion, dated~\filedate.^^A
%    }^^A
% }
% \author{Charles Staats III\thanks{E-mail: charles dot staats dot iii at gmail.com}}
% \date{Released \filedate}
%
% \maketitle
%
% \begin{abstract}
%  The \packagename{asypictureB} package allows users to integrate Asymptote code 
%  for producing pictures into \LaTeX\ source code using the shell-escape 
%  functionality. It is an alternative to the \texttt{asymptote} package 
%  that comes with Asymptote. The most important advantage of the 
%  \packagename{asypictureB} package is that it provides immediate access to 
%  Asymptote errors by repackaging them as \LaTeX\ errors. It also allows 
%  limited use of TeX macros such as |\the\linewidth| inside Asymptote code.
% \end{abstract}
% 
% \tableofcontents
% 
% \section{Introduction}
% 
% The \href{http://asymptote.sourceforge.net}%
% {Asymptote programming language}\footnote{\url{http://asymptote.sourceforge.net}} 
% is a powerful tool for the creation of diagrams, both two- and
% three-dimensional, that are compatible with 
% \TeX\ documents. The programming language ships with a \LaTeX\ package 
% called (appropriately but confusingly) \packagename{asymptote} that makes it 
% easy to draw pictures by including Asymptote code in the \LaTeX\ source 
% file. The \packagename{asymptote} package also allows the inclusion of 
% interactive three-dimensional images in |pdf| files.\footnote{ 
% The \packagename{asypictureB} package does not 
% currently support this feature.}
% 
% Unfortunately, the author has encountered the several annoyances 
% using the \packagename{asymptote} package.
% \begin{enumerate}
% \item
% When my Asymptote code contains errors that prevent it 
% from compiling, I have found it extremely difficult to track down 
% the offending line in the \TeX\ source file. This has been the 
% single biggest annoyance and has led me to compose all but the simplest 
% Asymptote images as separate |.asy| files, which are then 
% included into the \TeX\ source once they do what I want.
% \item 
% The \packagename{asymptote} package does not support PNG files, 
% although the Asymptote language does.
% \item
% The \packagename{asymptote} package does have a mechanism in place so that 
% when it is used with |latexmk|, Asymptote images that have not changed are 
% not recompiled. Unfortunately, the mechanism for recognizing unchanged images 
% is somewhat fragile: if a single image is inserted or deleted, then all 
% subsequent images will have to be recompiled. In my experience, this can 
% make a compilation last several minutes that would otherwise have lasted 
% several seconds.
% \item
% The \packagename{asymptote} package can rescale an Asymptote-produced 
% image to a given width and/or a given height, but it cannot rescale the 
% image by a given scaling factor.
% \end{enumerate}
% The most important issue here is the first, which can to some extent be fixed 
% using editor features; see, for instance, 
% \href{http://www.artofproblemsolving.com/Wiki/index.php/Asymptote:_Advanced_Configuration\#Showing_Asymptote_error_messages_in_TeXnicCenter}%
% {this explanation for TeXnicCenter (Windows only)}\footnote{\raggedright
% \url{http://www.artofproblemsolving.com/Wiki/index.php/Asymptote:_Advanced_Configuration\#Showing_Asymptote_error_messages_in_TeXnicCenter}
% }.
% 
% The \packagename{asypictureB} package is an alternative to the 
% \packagename{asymptote} 
% package that (optionally) 
% uses |\write18| to call the Asymptote compiler directly from 
% \LaTeX. The package provides some sort of fix for 
% all the above issues:
% \begin{enumerate}
% \item
% Asymptote errors are repackaged as \LaTeX\ errors and reported immediately.
% At a minimum, users are shown the Asymptote error log and a line number 
% that will allow them to locate the correct Asymptote picture within the 
% \TeX\ source file.
% 
% Additionally, \packagename{asypictureB} is usually able to display the 
% five lines of Asymptote code up to and including the first error. This is 
% useful to locate the line of code 
% on which the error occurs, since the line numbers in the Asymptote error log 
% do not correspond to the line numbers in the \LaTeX\ source file.
% %
% \item
% Any file type that is supported by both the Asymptote language and the 
% |\includegraphics| command is supported by \packagename{asypictureB}. 
% Assuming that the document is compiled using \texttt{pdflatex}, this includes 
% the PNG, PDF, and (more or less) EPS file formats.
% \item
% Users are permitted and strongly encouraged to specify a distinct name for 
% each Asymptote image in the file. Distinctly named Asymptote images 
% are not recompiled if their 
% code has not changed since the last |latex| run.
% \item Any option that works for the |\includegraphics| command can be given 
% as an option to an |asypicture| environment.
% \end{enumerate}
% 
% There is one other feature worth mentioning: \LaTeX\ macros will be expanded 
% inside Asymptote code if they are prefixed by @ instead of |\|. This 
% can be used as an incomplete substitute for the |inline| option of the 
% \packagename{asymptote} package.  It also provides a way to use macros 
% in Asymptote code, which is not a feature that Asymptote supports otherwise.
% 
% Each of these features could stand significant improvements. However, 
% since implementing them, the author has found himself much more willing 
% to compose Asymptote code directly in a \TeX\ source file.  This indicates 
% to him that the package might prove useful to others, even in its current 
% form.  His hope is that the best ideas of this package would 
% be copied and improved in the official \packagename{asymptote} package,
% allowing him to deprecate \packagename{asypictureB}.
% 
% \section{Installation and running}
% 
% First of all, the \packagename{asypictureB} package is mostly useless unless you 
% have Asymptote installed on your system. 
% \begin{itemize}
% \item For a MacOS system, this installation
% is automatic with a standard installation 
% of \href{http://tug.org/mactex/}{MacTeX}. 
% \item For a Windows system, the 
% \href{http://asymptote.sourceforge.net/doc/Microsoft-Windows.html}{official 
% installation instructions} are fairly good. As of this writing, the most 
% recent version of the |setup.exe| file can be downloaded from 
% \url{http://sourceforge.net/projects/asymptote/files/2.95/}.
% \item
% For a Unix-like system, a version of 
% Asymptote is included in TeX Live, but there may be additional dependencies;
% see, for instance, \url{http://tex.stackexchange.com/a/155284/484}. You 
% should also consult 
% \href{http://asymptote.sourceforge.net/doc/UNIX-binary-distributions.html}{these}
% \href{http://asymptote.sourceforge.net/doc/Compiling-from-UNIX-source.html}{two}
% pages from the official documentation. 
% \end{itemize}
% 
% To use the \texttt{asypictureB} package, 
% your \texttt{.tex} file should be run with shell-escape 
% enabled:
% \begin{Verbatim}[commandchars=\\\{\},gobble=2]
% pdflatex -shell-escape \meta{filename}
% \end{Verbatim}  
% [Note that |latex|, |lualatex|, etc.\ can be substituted for |pdflatex|, although
% you should make sure that the engine you use is compatible with whatever 
% graphics formats your Asymptote pictures are compiled to.]
% 
% \subsection{Running without shell escape}
% 
% If you are unwilling to use shell-escape, \packagename{asypictureB} 
% creates a script that makes it easy to execute the necessary commands 
% afterwards. To use it, run the following four commands at the terminal. 
% (The second command is optional; it allows you to inspect the script
% before running it.)
% For convenience, it is assumed that the name of the \LaTeX\ source file 
% is |foo.tex|.
% 
% \bigskip\noindent
% \begin{minipage}{0.48\linewidth}
% Windows:
% \begin{Verbatim}[commandchars=\\\{\},gobble=2]
% pdflatex foo
% type foo-compile_asy.txt
% type foo-compile_asy.txt|cmd
% pdflatex foo
% \end{Verbatim}
% \end{minipage}
% \hfill
% \begin{minipage}{0.48\linewidth}
% MacOS and Unix-like systems:
% \begin{Verbatim}[commandchars=\\\{\},gobble=2]
% pdflatex foo
% cat foo-compile_asy.sh
% sh foo-compile_asy.sh
% pdflatex foo
% \end{Verbatim}
% \end{minipage}
% 
% \medskip\noindent
% Instead of |pdflatex|, one may use |latex|, |lualatex|, \ldots. Asymptote 
% errors will be visible as \TeX\ errors on the second run of \LaTeX.
% 
% \begin{warning}
% This method is not entirely foolproof. In particular, if |pdflatex| is run 
% twice in a row without running the \meta{filename}-|compile_asy| 
% script in between, then 
% Asymptote pictures which have been compiled at some point in the past, even 
% if they have since been altered, will 
% not be recompiled. Should this happen, you can force every |asypicture| 
% to be recompiled by temporarily adding the |\RequireAsyRecompile| command
% before your first |asypicture|.
% \end{warning}
% 
% \paragraph{The safer method}
% If you want to compile a \TeX\ source file from someone else (e.g., 
% the internet), you may want neither to enable shell-escape nor to 
% run a script generated by this file.
% A safer way to compile 
% all Asymptote pictures generated by \packagename{asypictureB} 
% (and for that matter by the \packagename{asymptote} package) is to run 
% |asy -noV |\meta{filename}|-*.asy| after compiling \meta{filename}|.tex|.
% Once this is done, all the Asymptote files should be compiled, will be 
% correctly imported upon a second run of |pdflatex| (or |latex|, \ldots).
% Re-running |asy| is not necessary until and unless any of the Asymptote 
% pictures change. This method is ``safe'' in that you have greater 
% control over which programs are actually being run.
% 
% This method will also work for your own files-in-progress, but is not 
% recommended for two reasons:
% \begin{itemize}
% \item Asymptote errors will not be repackaged as \TeX\ errors, negating 
% one of the main features of the \packagename{asypictureB} package.
% \item All Asymptote pictures will be recompiled, even if they have not 
% changed since the last run. This can add considerably to the compile time.
% \end{itemize}
% 
% \subsection{Dependencies}
% As currently implemented, the \packagename{asypictureB} package requires
% the packages \packagename{fancyvrb} (tested with version 2.8), 
% \packagename{graphicx} (tested with version 2005/11/14), 
% \packagename{pgfkeys}, and \packagename{ifplatform} (tested with 
% version 0.4). In fact, it depends 
% on undocumented internals of the \packagename{fancyvrb} package, so 
% later versions of this package could conceivably break it as well as earlier versions.
% 
% If shell-escape is 
% not enabled, it also requires the \packagename{verbatimcopy} package, 
% version 0.06 or later.  Since \packagename{verbatimcopy} is not backwards 
% compatible, earlier versions will, in fact, break \packagename{asypictureB}.
% 
% \section{Usage}
%
% \DescribeEnv{asypicture} Code for Asymptote pictures should be placed 
% within the |asypicture| environment, which takes one mandatory argument:
% a list of comma-separated expressions of the form 
% \meta{key}\texttt{=}\meta{value}. It is strongly recommended that the 
% key |name| always be used, since this will prevent the package from 
% re-compiling pictures whose code has not changed. The time saving can 
% be substantial if you have a document with a significant number of 
% Asymptote pictures. 
% 
% Keys other than |name| are passed onto an |\includegraphics| 
% command from the \packagename{graphicx} package. The keys should be given 
% in the following order:
% \begin{enumerate}
% \item Keys other than |scale| or |angle|.
% \item The |scale| key (if it is used).
% \item The |angle| key (if it is used).
% \end{enumerate}
% The way the package is currently implemented, the |scale| and |angle| keys 
% will effectively be evaluated last, regardless of the order in which they 
% are given. However, this behavior is not ideal and may change in future versions 
% of the package; the goal would be that keys should be evaluated in the order 
% in which they are given. For the time being, giving keys in the order specified 
% should ensure compatibility with future versions.
% 
% It is possible to expand macros within an |asypicture| environment by using an 
% at symbol
% \texttt{@} in place of a backslash |\|. For instance, the line
% \begin{Verbatim}[gobble=2]
% size(@the@linewidth, 0);
% \end{Verbatim}
% will be translated to something like
% \begin{Verbatim}[gobble=2]
% size(345.0pt, 0);
% \end{Verbatim}
% before being compiled by Asymptote.
% 
% \bigskip\noindent
% \DescribeEnv{asyheader}Code within an |asyheader| environment is appended to 
% the ``header,'' which is inserted at the beginning of every \texttt{.asy} file 
% output by the \packagename{asypictureB} package. Initially, the header 
% consists of the two lines
% \begin{Verbatim}[gobble=2]
% defaultpen(fontsize(@getfontsize pt));
% settings.prc = false;
% \end{Verbatim}
% Again, macros can be expanded inside an |asyheader| environment by prefixing them 
% by @ rather than |\|. The macros are fully expanded when they are added to the 
% header, not when they are written to a file. Thus, for instance, in a 10-point 
% document, the first line of the header will always read 
% \begin{Verbatim}[gobble=2]
% defaultpen(fontsize(10pt));
% \end{Verbatim}
% even if the font size is later changed to 12 points.
% 
% Note that any change to the header will require all subsequent |asypicture| 
% environments to be recompiled.  In a document with many Asympotote images, 
% this could take a while.
% 
% \bigskip\noindent
% \DescribeMacro{\getfontsize}The |\getfontsize| macro is an alias for 
% |\f@size| that does not require |\makeatletter|. It expands to the current 
% font size (without the suffix |pt|).
% 
% \bigskip\noindent\DescribeMacro{\asylistingfile}The |\asylistingfile| macro
% contains the filename of the Asymptote code for the most recent |asypicture|.
% It can be used with commands from e.g. \packagename{fancyvrb} or 
% \packagename{listings} to display the Asymptote code for an image.
% 
% \bigskip\noindent
% \DescribeMacro{\RequireAsyRecompile}\DescribeMacro{\AsyCompileIfNecessary}
% By default, Asymptote images are compiled only if the Asymptote code has changed. 
% The command |\RequireAsyRecompile| changes this setting to make all subsequent 
% images recompile even if the code has not changed. 
% (This can be useful, for instance, 
% if you have just updated Asymptote.) The command 
% |\AsyCompileIfNecessary| restores the default behavior for all subsequent 
% Asymptote pictures.
% 
% \subsection{Keys for \texttt{asypicture}}
% 
% \DescribeMacro{name} If the key-value combination |name=|\meta{picturename} 
% appears in the mandatory argument to an |asypicture| environment, then the 
% contents of that environment will be saved to the file 
% \begin{Verbatim}[gobble=2,commandchars=\\\{\}]
% \meta{filename}-\meta{picturename}.asy
% \end{Verbatim}
% where \meta{filename} is the name of the current |.tex| file (not including 
% the |.tex| extension). 
% The compiled Asymptote picture is saved to an image file such as 
% \texttt{\meta{filename}-\meta{picturename}.eps},
% \texttt{\meta{filename}-\meta{picturename}.pdf}, or
% \texttt{\meta{filename}-\meta{picturename}.png}.
% 
% Alphanumeric characters are allowed in the |name| key, as are the 
% characters |-| (hyphen) and |_| (underscore). Other characters (including 
% spaces, asterisks, etc.) should 
% be avoided. The key may begin with any allowed character; however, names 
% consisting of a single number with three or fewer digits are discouraged, 
% since these may conflict with the \packagename{asymptote} package.
% 
% Although the |name| key is technically not required, 
% it is strongly recommended that distinct names be given 
% to all the different |asypicture| 
% environments. This allows the \packagename{asypictureB} package to avoid 
% recompiling pictures whose code has not changed---even if the pictures are
% reordered.  
% 
% If the name \meta{picturename} has been used before, 
% a suffix will be appended to it: |__1| for the first repeat, |__2| for 
% the second repeat, etc. Thus, if the same name (say |foo|) 
% is used for several |asypicture|s, 
% then deleting the first of these will force all the others to recompile; 
% but changing a picture of a different name will not affect any of the 
% pictures named |foo|.
% 
% If no |name| key is given, the key-value combination |name=noname| is 
% assumed. This default can be changed using the |\asyset| command: after 
% the line 
% \begin{Verbatim}[gobble=2]
% \asyset{name = foo}
% \end{Verbatim}
% nameless pictures will be called |foo| rather than |noname|.
% 
% \begin{warning}
% If \packagename{asypictureB} determines that the contents of an |asypicture|
% environment named \meta{picturename} 
% have changed since the last run, it will delete 
% all of the following files that exist:
% \begin{Verbatim}[gobble=2,commandchars=\\\{\}]
% \texttt{\meta{filename}-\meta{picturename}.eps}
% \texttt{\meta{filename}-\meta{picturename}.pdf}
% \texttt{\meta{filename}-\meta{picturename}.png}
% \end{Verbatim}
% \end{warning}
% 
% \subsection{Styles}
% \DescribeMacro{\asyset} 
% Since \packagename{asypictureB} uses \packagename{pgfkeys} for keys, 
% it is possible to create new keys, called ``styles,'' that set 
% several other keys at once. For instance, the following line 
% \begin{Verbatim}[gobble=2]
% \asyset{mysize/.style = {width=6cm, height=4cm}}
% \end{Verbatim}
% creates a new key called |mysize|. Any time in the document after this 
% line, the command
% \begin{Verbatim}[gobble=2]
% \begin{asypicture}{name=picturename, mysize}
% \end{Verbatim}
% is exactly equivalent to the line
% \begin{Verbatim}[gobble=2]
% \begin{asypicture}{name=picturename, width=6cm, height=4cm}
% \end{Verbatim}
% This is precisely the same style mechanism that is used in TikZ; in 
% fact, |\asyset|\marg{keys} is equivalent to |\pgfkeys{/asy/.cd,|\meta{keys}|}|, 
% much as |\tikzset|\marg{keys} is equivalent to |\pgfkeys{/tikz/.cd,|\meta{keys}|}|.
% 
% The |\asyset| command may be used in the preamble or anywhere in the body of 
% the document where macros are processed normally. It may not be used in the 
% body of an |asypicture| or any other verbatim-like environment.
% 
% \section{Examples}
% 
% Here is a simple example:
% 
% \noindent\def\asywidth{2.5cm}
% \begin{minipage}[t]{\asywidth}
% \vspace*{0pt}
% \begin{asypicture}[gobble=2]{name = sphere_image, width=\asywidth}
% settings.outformat = "png";
% settings.render = 16;
% size(@asywidth,0);
% import three;
% draw(unitsphere, white);
% \end{asypicture}
% \end{minipage}
% \hfill
% \begin{minipage}[t]{\dimexpr\linewidth-\asywidth-10pt\relax}
% \vspace*{0pt}
% \begin{Verbatim}[gobble=2,codes={\catcode`@=0},frame=single]
% \begin{asypicture}{name=sphere_image}
% settings.outformat = "png";
% settings.render = 16;
% size(@asywidth,0);
% import three;
% draw(unitsphere, white);
% \end{asypicture}
% \end{Verbatim}
% \end{minipage}
% 
% \medskip\noindent
% Here is an example that uses the user-defined macro |\asywidth| 
% inside the Asymptote code to avoid duplicate code.
% 
% \begin{Verbatim}[gobble=2,tabsize=4,frame=single]
% \noindent\def\asywidth{4cm}
% \begin{asypicture}{name=triangle,width=\asywidth}
% 	settings.outformat="pdf";
% 	size(@asywidth,0);
% 	draw((0,0) -- (1,0) -- (1,1) -- cycle);
% 	dot((1,0), L=Label(
% 		"\textbf{Bold} \textsc{Smallcaps}",
% 		align=NE));
% \end{asypicture}
% \hfill
% \begin{minipage}{\dimexpr\linewidth-\asywidth-15pt\relax}
% \VerbatimInput[frame=leftline]{\asylistingfile}
% \end{minipage}
% \end{Verbatim}
% The result:
% 
% \noindent\def\asywidth{4cm}
% \begin{asypicture}[gobble=2]{name=triangle,width=\asywidth}
% 	settings.outformat="pdf";
% 	size(@asywidth,0);
% 	draw((0,0) -- (1,0) -- (1,1) -- cycle);
% 	dot((1,0), L=Label(
% 		"\textbf{Bold} \textsc{Smallcaps}",
% 		align=NE));
% \end{asypicture}
% \hfill
% \begin{minipage}{\dimexpr\linewidth-\asywidth-15pt\relax}
% \VerbatimInput[frame=leftline]{\asylistingfile}
% \end{minipage}
% 
% \bigskip
% The |\VerbatimInput| command is from the \packagename{fancyvrb} package. 
% The first two lines of the Asymptote code listing come from the default 
% |asyheader|. These, along with some extra whitespace, 
% can be eliminated by playing with the \packagename{fancyvrb} options. 
% More importantly for our purposes, changing the single line 
% \begin{Verbatim}[gobble=2]
% \noindent\def\asywidth{4cm}
% \end{Verbatim}
% to
% \begin{Verbatim}[gobble=2]
% \noindent\def\asywidth{4.5cm}
% \end{Verbatim}
% will automatically affect both the width of the |asypicture| and the 
% width of the |minipage|:
% 
% \begin{Verbatim}[gobble=2,tabsize=4,frame=single]
% \noindent\def\asywidth{4.5cm}
% \begin{asypicture}{name=triangle2,width=\asywidth}
% 	settings.outformat="pdf";
% 	size(@asywidth,0);
% 	draw((0,0) -- (1,0) -- (1,1) -- cycle);
% 	dot((1,0), L=Label(
% 		"\textbf{Bold} \textsc{Smallcaps}",
% 		align=NE));
% \end{asypicture}
% \hfill
% \begin{minipage}{\dimexpr\linewidth-\asywidth-15pt\relax}
% \VerbatimInput[frame=leftline,gobble=4,firstline=5]
% 	{\asylistingfile}
% \end{minipage}
% \end{Verbatim}
% produces
% 
% \noindent\def\asywidth{4.5cm}
% \begin{asypicture}[gobble=2]{name=triangle2,width=\asywidth}
% 	settings.outformat="pdf";
% 	size(@asywidth,0);
% 	draw((0,0) -- (1,0) -- (1,1) -- cycle);
% 	dot((1,0), L=Label(
% 		"\textbf{Bold} \textsc{Smallcaps}",
% 		align=NE));
% \end{asypicture}
% \hfill
% \begin{minipage}{\dimexpr\linewidth-\asywidth-15pt\relax}
% \VerbatimInput[frame=leftline,gobble=4,firstline=5]
% 	{\asylistingfile}
% \end{minipage}
% 
% \section{Error handling}
% 
% It has been stated several places in this document that 
% \packagename{asypictureB} repackages Asymptote errors as 
% \TeX\ errors (and at least once that this feature, among others, 
% could stand significant improvements). In this section is described 
% exactly how these errors are repackaged, at least as of the current 
% implementation. None of this is guaranteed to remain the same in 
% future versions.
% 
% When \packagename{asypictureB} tells Asymptote to compile the file 
% \begin{quote}
% \texttt{\meta{filename}-\meta{picturename}.asy},
% \end{quote}
%  it reroutes all 
% warning and error messages to the file 
% \begin{quote}
% \texttt{\meta{filename}-\meta{picturename}\_errors.txt}.
% \end{quote}
% Once the Asymptote run is complete, it checks whether the error file 
% has any lines of the form
% \begin{quote}
% \texttt{\meta{filename}-\meta{picturename}.asy:\meta{number}.\meta{stuff}}
% \end{quote}
% If so, it throws a \LaTeX\ package error and displays 
% the contents of the error file, which include all the errors and 
% warnings issued by Asymptote. Note that if some kind of asy error occurs
% that does not match this output form, \packagename{asypictureB} will not
% notice the error.
%
% The \LaTeX\ error message will give the line number of the 
% |\end{asypicture}| command for the |asypicture| that caused the error, which 
% is of limited use in isolating the error.
% More precise line numbers are given by the Asymptote error log; 
% however, these line numbers are 
% for the |asy| file rather than the |tex| file, and consequently not 
% terribly helpful.
% 
% To allow the user to locate the line on which the actual error occurred, 
% the \packagename{asypictureB} package attempts to parse the Asymptote 
% error log and print out the five lines leading up to the error.  More precisely, 
% it does the following:
% \begin{enumerate}
% \item From the first line of the form
% \begin{quote}
% \texttt{\meta{filename}-\meta{picturename}.asy:\meta{number}.\meta{stuff}}
% \end{quote}
% the \meta{number} is extracted. Note that if no successful extraction occurs,
% there will be no \LaTeX\ error.
% \item Assuming \meta{number} was extracted successfully, the lines 
% of the |asy| file from 
% the inclusive range $\meta{number}-5$ to \meta{number} are displayed
% as part of the \LaTeX\ error message. These lines are usually identical 
% and almost always similar to the lines in the actual |tex| file 
% leading up to the error. In the author's experience, this is usually enough 
% information to locate without difficulty the line on which the error 
% occurred.\footnote{I.e., the line that caused Asymptote to choke. 
% The usual rules of debugging apply: the line on which the compiler identified 
% an error might have been correct if not for an earlier mistake that 
% was syntactically correct.}
% \end{enumerate}
% 
% \noindent Here's an example: Consider \LaTeX\ file
% \begin{Verbatim}[gobble=2,numbers=left,tabsize=4]
% \documentclass{article}
% \usepackage{asypictureB}
% \begin{document}
% \def\asywidth{5cm}
% \begin{asypicture}{name=error_example}
% 	// A comment
% 	size(@asywidth, 0);
% 	path l1 = (0,0) -- (1,1);
% 	// Another comment
% 	draw(box((0,0),(1,1)))
% 	draw(l1, dotted);
% 	draw(l2, dashed);
% \end{asypicture}
% \end{document}
% \end{Verbatim}
% If this file is saved as \texttt{asyerrorexample.tex} and then 
% compiled with the shell-escape option, the following error results:
% \begin{Verbatim}[gobble=2]
% ! Package asypictureB Error: 
% draw(l1, dotted);
% ^
% asyerrorexample-error_example.asy: 10.5: syntax error
% error: could not load module 'asyerrorexample-error_example.asy'
% 
% 6     size(5cm, 0);
% 7     path l1 = (0,0) -- (1,1);
% 8     // Another comment
% 9     draw(box((0,0),(1,1)))
% 10     draw(l1, dotted);
% .
% 
% See the asypictureB package documentation for explanation.
% Type  H <return>  for immediate help.
%     ...                                              
%                                                     
% l.13 \end{asypicture}
% \end{Verbatim}
% The \LaTeX\ error message first repeats the Asymptote error log,
% which explains that there was a syntax error. Next, the five lines
% leading up to the error are displayed.
% Looking at these five lines, 
% one can determine that the difficulty was a comma omitted 
% on line 9 of the Asympote file. (It is diagnosed on line 10 because 
% of how the compiler works.) Looking at the context, this corresponds 
% to line 10 of the \LaTeX\ file.
% 
% Note that the macro |@asywidth| in the |asypicture| code 
% has been expanded to \texttt{5cm} in the error message.
% 
% \section{Macros in \texttt{asypicture}s}
% 
% When the author first conceived of allowing macros in an 
% |asypicture| environment, the goal was to allow expressions like 
% |size(@asywidth,0);| where |\asywidth| was a user-defined macro 
% also used in the \LaTeX\ code to avoid hard-coding numbers. 
% However, it was almost immediately apparent that this feature 
% has more sophistocated uses, such as allowing user-defined 
% syntax or even creating something similar to templates\footnote{If you
% actually want to use templates in Asymptote code, the experimental
% \emph{templated imports} feature of the language is probably a
% better choice than the macro techniques described in this section}. 
% 
% \subsection{Limitations}
% 
% Before discussing the nifty features, let's discuss the fairly 
% severe limitations they will have to work around.
% 
% \begin{description}
% \item[No grouping symbols.] If a macro takes a mandatory argument 
% inside braces |{}|, then that macro cannot be used inside an 
% |asypicture|.
% \item[Purely expandable macros only.] Macros will be expanded, but not 
% executed. In particular, macros that allow optional arguments will usually 
% go horribly wrong.
% \item[One line only.] As we will discuss, it is 
% possible to use ``unconventionally delimited'' arguments. However, even 
% in this case, a macro and all its arguments must fit on a single line.
% \end{description}
% 
% Since the first two points all but forbid the use of macros with 
% conventional arguments, it might 
% be wondered whether macros in Asymptote code can be used for anything 
% more interesting than storing user-defined lengths. They can.
% 
% \subsection{Single-token arguments}
% 
% [This is really more a fix than an example, but this seems as good a 
% place as any to discuss it.]
% 
% Some macros, such as |\the|,
% take arguments that consist only of a single character or 
% control sequence. For instance, |\the\textwidth| expands to something like 
% \texttt{345.0pt}, whereas |\textwidth| by itself expands only to 
% |\textwidth|. This can be significant if you want to produce an 
% Asymptote picture that takes up a specified fraction of the text width.
% 
% \subsection{Unconventionally delimited arguments}
% 
% The \TeX\ primitive |\def| can be used to produce macros that are 
% quite flexible about how they are delimited. For instance, the \TeX\ code
% \begin{Verbatim}[gobble=2]
% \def\draw#1;{draw(#1);}
% \end{Verbatim}
% will take as its argument everything between the |\draw| command and the 
% first subsequent semicolon. If this line showed up in \TeX\ code 
% (preferably in the preamble), then subsequent |\asypicture| environments 
% could include a line such as 
% \begin{Verbatim}[gobble=2]
% @draw box((0,0), (1,1));
% \end{Verbatim}
% as an arguably more aesthetic alternative to the translation
% \begin{Verbatim}[gobble=2]
% draw( box((0,0), (1,1)));
% \end{Verbatim}
% 
% A more advanced example is essentially a template for a sorting 
% function\footnote{The algorithm here is a somewhat inefficient 
% mergesort.}. 
% Include the following code in the \TeX\ preamble:
% \begin{Verbatim}[gobble=2]
% \def\definesortfunction #1;{%
% #1[] sort(#1[]  a) {
%     if (a.length <= 1) return a; 
%     static #1[] merge(#1[] b, #1[] c) { 
%         #1[] toreturn;
%         int i = 0, j = 0;
%         while (i < b.length && j < c.length) {
%             if (!(c[j] < b[i])) { toreturn.push(b[i]); ++i; }
%             else { toreturn.push(c[j]); ++j; }
%         }
%         while (i < b.length) {
%             toreturn.push(b[i]);
%             ++i;
%         }
%         while (j < c.length) {
%             toreturn.push(c[j]);
%             ++j;
%         }
%         return toreturn;
%     }
%     int halfway = floor(a.length / 2);
%     #1[] b = sort(a[0:halfway]);
%     #1[] c = sort(a[halfway:a.length]);
% 
% 
%     return merge(b, c);
% }}
% \end{Verbatim}
% Then within any |asypicture| environment, the line |@definesortfunction T;| 
% can be used to define a function |T[] sort(T[])| that returns a sorted 
% version of its argument, for any type |T| for which the less than operator 
% |<| is defined. For instance, within an |asypicture|, the code 
% \begin{Verbatim}[gobble=2]
% bool operator <(pair a, pair b) {
%    return (a.x < b.x  || (a.x == b.x && a.y < b.y));
% }
%
% @definesortfunction pair;
% \end{Verbatim}
% makes available a lexicographic sorting routine for ordered pairs of real 
% numbers.
% 
% Unfortunately, while this compiles correctly, the resulting Asymptote 
% file has no line breaks in the entire definition of the sort function.
% If you want the Asymptote code (as opposed to just the |asypicture| code) 
% to be readable, the following setup, proposed by Enrico Gregorio\footnote{
% \url{http://tex.stackexchange.com/a/160740/484}}, does the job:
% 
% \begin{Verbatim}[gobble=2]
% \begingroup
% \endlinechar=`^^J \obeyspaces% end of lines are newlines
% \gdef\definesortfunction #1;{% eat up the space following the macro
% #1[] sort(#1[]  a) {
%     if (a.length <= 1) return a; 
%     static #1[] merge(#1[] b, #1[] c) { 
%         #1[] toreturn;
%         int i = 0, j = 0;
%         while (i < b.length && j < c.length) {
%             if (!(c[j] < b[i])) { toreturn.push(b[i]); ++i; }
%             else { toreturn.push(c[j]); ++j; }
%         }
%         while (i < b.length) {
%             toreturn.push(b[i]);
%             ++i;
%         }
%         while (j < c.length) {
%             toreturn.push(c[j]);
%             ++j;
%         }
%         return toreturn;
%     }
%     int halfway = floor(a.length / 2);
%     #1[] b = sort(a[0:halfway]);
%     #1[] c = sort(a[halfway:a.length]);
% 
% 
%     return merge(b, c);
% }% this % is necessary
% }% this % is necessary
% \endgroup% this % is necessary
% \end{Verbatim}
% 
% \begin{warning}
% If two commands use the same unconventional delimiter, then they cannot 
% be nested. In particular, a command cannot appear inside its own argument. 
% This is one of the reasons people usually delimit with grouping 
% symbols, which is not an option here, short of using |@bgroup| and 
% |@egroup| with copious occurrences of |@expandafter|.
% \end{warning}
% \begin{warning}
% The |\def| command is not expandable. Thus, the code defining the 
% macros must be given \emph{outside} |asypicture| environments, 
% even when the macros are intended for use exclusively inside |asypicture|s.
% \end{warning}
% 
% 
% \section{Missing features} 
% 
% The introduction of this documentation touted ways in which 
% \packagename{asypictureB} improves on the behavior of the \packagename{asymptote}
% package. In this section, I clarify ways in which it falls short and 
% describe how to compensate where possible. My hope 
% is that these differences would narrow in both directions until only 
% one package is necessary---preferably the official \packagename{asymptote} 
% package.
% 
% Note that some of these ``missing features'' cannot be fixed without 
% breaking backwards compatibility. This is largely why the package is 
% named \packagename{asypictureB}, allowing room for a future, more 
% powerful version named \packagename{asypicture}, in case the 
% \packagename{asymptote} package does not step into the gap.
% 
% \begin{framed}
% \noindent\textbf{Tip:} To retrieve the documentation for the 
% \packagename{asymptote} \LaTeX\ package, 
% type \texttt{texdoc asy-latex} at the command line. To retrieve the 
% documentation for the Asymptote programming language, type 
% \texttt{texdoc asymptote}. To retrieve the documentation for 
% \packagename{asypictureB}, type \texttt{texdoc asypictureB}.
% \end{framed}
% 
% \paragraph{No 3d interaction}
% One of the most spectacular features of the \packagename{asymptote} package is 
% the embedding of interactive three-dimensional images in PDF files. 
% (Note that this requires the |inline| option when loading the package.)
% The \packagename{asypictureB} package currently has no such feature. 
% In fact, the default header includes 
% the line \texttt{settings.prc = false;} to prevent Asymptote from trying 
% to produce an interactive (|prc|) image, since this might break things.
% 
% \paragraph{No \texttt{inline} option}
% A second feature that is important, but less spectacular, is the |inline| 
% option itself, which causes the Asymptote labels to be compiled (in \LaTeX) 
% with all the 
% same packages loaded and macros defined as in the original document. 
% For instance, font consistency between the Asymptote images and the 
% larger document is ensured by this option.
% It comes with an important limitation---Asymptote cannot use size information 
% about the labels when this option is in use.
% 
% The closest 
% equivalent in the \packagename{asypictureB} package is the immediate 
% expansion of macros introduced by the @ symbol. This does not have the 
% drawback of the |inline| option, and has many additional uses. However, 
% it is less flexible inside labels (since, e.g., macros with arguments 
% cannot be used this way), and does not ensure font consistency. 
% 
% To compensate for this when using \packagename{asypictureB}, important 
% packages and macro definitions should be echoed in the Asymptote header. 
% Here is an example of code that could be placed in the preamble of a document 
% to make the macro |\RR| (for $\mathbb R$) available in both the document 
% text and the labels of its Asymptote pictures:
% \begin{Verbatim}[gobble=2]
% \usepackage{amssymb}
% \newcommand{\RR}{\mathbb{R}}
% \begin{asyheader}
% usepackage("amssymb");
% texpreamble("\newcommand{\RR}{\mathbb{R}}");
% \end{asyheader}
% \end{Verbatim}
% 
% \paragraph{The output format is not automatically set.} 
% When using \packagename{asymptote}, the output format is automatically 
% set to either |eps| or |pdf| depending on what \TeX\ engine is being run 
% (and which kind of graphics file it prefers). When using \packagename{asypictureB}, 
% no such provision is made; if any format other than |eps| is desired, 
% it must be selected by including the Asymptote code 
% \texttt{settings.outformat="pdf";} (or |"png"|). This is by design, to allow 
% the use of |png| files; but it can be a bit annoying at times. To set all 
% Asymptote files to have |pdf| format, include the code 
% \begin{Verbatim}[gobble=2]
% \begin{asyheader}
% settings.outformat="pdf";
% \end{asyheader}
% \end{Verbatim}
% in the preamble (or anywhere prior to the first |asypicture| environment).
% 
% \paragraph{Keys to \texttt{asypicture} are not conveyed to Asymptote}
% In the \packagename{asymptote} package, keys like |width| and 
% |height| are conveyed both to the implicit |\includegraphics| command 
% and to Asymptote. In \packagename{asypictureB}, they are conveyed only 
% to the |\includegraphics| command; font size and even resolution (for 
% rasterized images) will be 
% changed in the process of scaling the picture. 
% Thus, the preferred method is to set the width, height, etc.\ 
% through Asymptote's |size| command.
% 
% \strut
% 
% \noindent
% \begin{minipage}{\textwidth}
% \gdef\asywidth{3.5cm}
% Bad (font size distorted):
% 
% \medskip
% \begin{minipage}{\asywidth}
% \begin{asypicture}[gobble=2]{name=bad_width,
%      width=\asywidth}
% settings.outformat="pdf";
% draw(unitcircle);
% label("a label", position=(0,0), align=NE);
% \end{asypicture}
% \end{minipage}
% \hfill
% \begin{minipage}{\dimexpr\linewidth-\asywidth-10pt\relax}
% \begin{Verbatim}[gobble=2,codes={\catcode`@=0}]
% \begin{asypicture}{name=bad_width, 
%     width=@asywidth}
% settings.outformat="pdf";
% draw(unitcircle);
% label("a label", position=(0,0), align=NE);
% \end{asypicture}
% \end{Verbatim}
% \end{minipage}
% \end{minipage}
% 
% \bigskip
% \noindent
% \begin{minipage}{\textwidth}
% Good:
% 
% \medskip
% \begin{minipage}{\asywidth}
% \begin{asypicture}[gobble=2]{name=good_width, width=\asywidth}
% size(@asywidth,0);
% settings.outformat="pdf";
% draw(unitcircle);
% label("a label", position=(0,0), align=NE);
% \end{asypicture}
% \end{minipage}
% \hfill
% \begin{minipage}{\dimexpr\linewidth-\asywidth-10pt\relax}
% \begin{Verbatim}[gobble=2,codes={\catcode`@=0}]
% \begin{asypicture}{name=good_width, width=@asywidth}
% size(@asywidth,0);
% settings.outformat="pdf";
% draw(unitcircle);
% label("a label", position=(0,0), align=NE);
% \end{asypicture}
% \end{Verbatim}
% \end{minipage}
% \end{minipage}
% 
% \bigskip\noindent
% Note that in the ``good'' example, the key |width=3.5cm| is necessary 
% only if you want to make sure the included image has \emph{precisely} 
% the specified 
% width, and probably not even then.
% 
% \paragraph{No spaces in file names} The \packagename{asymptote} package
% takes measures to accomodate peculiar file names---in particular, file 
% names that include spaces. The \packagename{asypictureB} package does not.
%
% \paragraph{No directory specification} The \packagename{asymptote} package
% allows the user to specify a directory in which to store the Asymptote
% code and images. The \packagename{asypictureB} package does not currently
% have this feature, although there is a patch by user202729 that will hopefully
% be included in the next version. Note that neither package respects the
% |-output-directory| commandline option.
%
% \paragraph{Suboptimal interaction with \texttt{latexmk}} By design, the
% \packagename{asypictureB} package allows the image format to be specified by
% the Asymptote code; it does not care whether a |png| or |pdf| file is produced.
% Unfortunately, |latexmk| relies on being able to identify the generated image file
% from the \TeX\ log file. Thus, if you use \packagename{asypictureB} with |latexmk|,
% it will rerun Asymptote every time for |asypicture|s that produce a file format other
% than the one expected by |latexmk|. Since \packagename{asypictureB} and |latexmk|
% are two different solutions to the same problem, I expect most users will choose
% one or the other. But for those who want to use them together, hopefully there will
% be a better solution in the future.
%
% \section{Acknowledgements}
%
% The current \packagename{asypictureB} package incorporates bug fixes and improvements
% by \href{https://github.com/user202729}{user202729}
% (\url{https://tex.stackexchange.com/users/250119})
% and \href{https://github.com/cfr42}{cfr}
% (\url{https://tex.stackexchange.com/users/39222}). For more details,
% see the \href{https://github.com/charlesstaats/asypictureB/commits/main/}{commit history on github}.
% 
% \StopEventually{^^A
%    \PrintChanges
%    \PrintIndex
% }
%
% \section{Implementation}
%\iffalse
%<*package>
%\fi
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{asypictureB}
  [2025/01/26 v0.4 user-friendly integration of Asymptote into LaTeX]

\RequirePackage{fancyvrb}
\RequirePackage{graphicx}
\RequirePackage{pgfkeys}

\makeatletter

\def\asy@OutFile{\FV@OutFile}

\RequirePackage{ifplatform}

%    \end{macrocode}
% Define the |\shell@execute| command. If shell escape is enabled, the
% command executes its contents in the shell. Otherwise, the command
% is written to a script for the user to execute.
%    \begin{macrocode}
\ifshellescape
    \def\ASYPIC@shell{18}
    \newcommand{\shell@execute}{\immediate\write\ASYPIC@shell}
\else
    \newwrite\ASYPIC@shell
    \ifwindows
        \openout\ASYPIC@shell=\jobname-compile_asy.txt\relax
    \else
        \openout\ASYPIC@shell=\jobname-compile_asy.sh\relax
    \fi
    \newcommand{\shell@execute}[1]{{%
        \edef\temp{#1}%
        \write\expandafter\ASYPIC@shell\expandafter{\temp}%
    }}
\fi

%    \end{macrocode}
% Define the |\copyfile| command, which should work regardless of whether
% shell escape is enabled. (If shell escape is enabled, it is more efficient.)
%    \begin{macrocode}
\ifshellescape
    \newcommand{\copyfile}[2]{%
    \ifwindows%
        \immediate\write18{copy #1 #2 /y}%
    \else%
        \immediate\write18{cp #1 #2}%
    \fi%
    }
\else
    \RequirePackage{verbatimcopy}
    \newcommand{\copyfile}[2]{%
        \OldVerbatimCopy{#1}{#2}%
    }
\fi

%    \end{macrocode}
% Define the |\deletefile| command. Unlike |\copyfile|, this command will
% have no immediate effect if shell escape is not enabled, instead writing
% a line to the script. (If shell escape \emph{is} enabled, of course the
% command will immediately delete the file.)
%    \begin{macrocode}
\newcommand{\deletefile}[1]{%
    \ifwindows%
        \shell@execute{del #1}%
    \else%
        \shell@execute{rm #1}%
    \fi%
}

%    \end{macrocode}
%
% Set up the key-value system for \packagename{asypictureB} using \packagename{pgfkeys}.
%    \begin{macrocode}
\newcommand{\asyset}[1]{\pgfqkeys{/asy}{#1}}
\newcommand{\@asyerrorfilename}{\@asypicturename_errors.txt}
\newcounter{@asy@linenumber}

\asyset{name/.initial=noname, name/.value required}%

\asyset{graphic options/.code={}}
\asyset{set graphic option/.style={graphic options/.append code=#1}}
%    \end{macrocode}
% Unrecognized keys should be passed to the |\includegraphics| command 
% using |\setkeys|.
%    \begin{macrocode}
\asyset{.unknown/.code = %
    {%
        \edef\unknownkey{\pgfkeyscurrentname}%
        \asyset{set graphic option/.expand once =  {%
            \expandafter\setkeys\expandafter{%
                \expandafter G\expandafter i\expandafter n\expandafter%
                }\expandafter{\unknownkey=#1}%
        }}%
    }%
}
%    \end{macrocode}
% However, scale and angle must be dealt with separately.
% 
% Important note: using this implementation, scale and angle will always be the next-to-last, respectively 
% the last, keys executed, no matter in what order the keys are given.  Thus, it is impossible to rotate an 
% asypicture and then set the width or height using these keys. However, this functionality can be provided 
% within the Asymptote code.
%     \begin{macrocode}
\def\asy@scale{1}
\asyset{scale/.style={set graphic option = {\def\asy@scale{#1}}},
    scale/.value required}
\newcommand{\asy@angle}{0}
\asyset{angle/.style = {set graphic option = {\def\asy@angle{#1}}},
    angle/.value required}

\newcommand{\getfontsize}{\f@size}

\newif\ifasyfilechanged
\newif\ifASYPIC@flush
\newif\if@asyrepeat
\newread\@asyreadold
\newread\@asyreadnew
\edef\@tempasyfile{\jobname-temp}

\newcommand{\RequireAsyRecompile}{\ASYPIC@flushtrue}
\newcommand{\AsyCompileIfNecessary}{\ASYPIC@flushfalse}
\AsyCompileIfNecessary

\newcommand\clearasyheader{\def\ASYPIC@header{}}

\def\ASYPIC@header{}

\def\asyheader{\FV@Environment{}{asyheader}}

\def\FVB@asyheader{%
     \@bsphack
         \begingroup
         \FV@UseKeyValues
         \FV@DefineWhiteSpace
         \def\FV@Space{\space}%
         \FV@DefineTabOut
         \def\FV@ProcessLine##1{\g@addto@macro\ASYPIC@header{##1^^J}}%
         \let\FV@FontScanPrep\relax
         %% DG/SR modification begin - May. 18, 1998
         %% (to avoid problems with ligatures)
         \let\@noligs\relax
         %% DG/SR modification end
         \FV@Scan}%

\def\FVE@asyheader{\endgroup\@esphack}

\DefineVerbatimEnvironment{asyheader}{asyheader}%
    {codes={\catcode`@=0},tabsize=4}
%    \end{macrocode}
%    Set up the default |asyheader|:
%    \begin{macrocode}
\begin{asyheader}
defaultpen(fontsize(@getfontsize pt));
settings.prc = false;
\end{asyheader}
%    \end{macrocode}
%    Now, define the |asypicture| environment using 
%    \packagename{fancyvrb} internals:
%    \begin{macrocode}
\def\asypicture{\FV@Environment{}{asypicture}}

%    \end{macrocode}
%
% Define the |\ASYPIC%recordname| command. As an example, |\ASYPIC@recordname@foo|
% in the file |bar.tex| would set |\asylistingfile| to |bar-foo.asy| the first
% time it is called. A second call to |\ASYPIC@recordname{foo}| would set
% |\asylistingfile| to |bar-foo__1.asy|, a third to |bar-foo__2.asy|, and so on.
% The next number to use is stored in the macro |\ASYPIC@name@foo|. (Note that this
% is a macro, not a counter.)
%    \begin{macrocode}
\newcommand{\ASYPIC@recordname}[1]{%
    \edef\tempmacroname{ASYPIC@name@#1}%
    \ifcsname\tempmacroname\endcsname%% if \ASYPIC@name@... is defined
        \edef\oldnum{\csname\tempmacroname\endcsname}%
        \edef\@asypicturename%
            {\jobname-#1__\oldnum}%
        \expandafter\xdef\csname\tempmacroname\endcsname%
            {\the\numexpr\oldnum+1\relax}%
        \edef\ASYPIC@current@num{\csname\tempmacroname\endcsname}%
    \else%% This is the first time this name is being used.
        \edef\@asypicturename%
            {\jobname-#1}%
        \expandafter\gdef\csname\tempmacroname\endcsname{1}%
    \fi%
    \xdef\asylistingfile{\@asypicturename.asy}
}

%    \end{macrocode}
%    Most of 
%    the following definition is copied verbatim from the 
%    definition of the |VerbatimOut| environment in 
%    |fancyvrb.dtx|. I don't actually understand what a lot of 
%    it does.
%    \begin{macrocode}

\def\FVB@asypicture#1{%
     \@bsphack
     \asyset{graphic options/.code={}}%
     \asyset{#1, name/.get = \currentname}%
     \ASYPIC@recordname{\currentname}%
     \begingroup
         \FV@UseKeyValues
         \FV@DefineWhiteSpace
         \def\FV@Space{\space}%
         \FV@DefineTabOut
         \def\FV@ProcessLine{\immediate\write\asy@OutFile}%
         \immediate\openout\asy@OutFile\@tempasyfile.asy\relax
         \immediate\write\asy@OutFile{\ASYPIC@header^^J}
         \let\FV@FontScanPrep\relax
         %% DG/SR modification begin - May. 18, 1998
         %% (to avoid problems with ligatures)
         \let\@noligs\relax
         %% DG/SR modification end
         \FV@Scan}

\newcommand{\ASYPICcomparefiles}[2]{
    \IfFileExists{\@asypicturename.asy}%
        {\openin\@asyreadold=#1.asy\relax%
          \openin\@asyreadnew=#2.asy\relax%
          \asyfilechangedfalse%
          \@asyrepeattrue%
          \loop%
              \ifeof\@asyreadold%
                  \@asyrepeatfalse%
                  \ifeof\@asyreadnew%
                  \else%
                      \asyfilechangedtrue%
                  \fi%
              \else%
                  \ifeof\@asyreadnew%
                      \@asyrepeatfalse%
                      \asyfilechangedtrue%
                  \else% Not at the end of either file in this case
                      \readline\@asyreadold to \oldfileline%
                      \readline\@asyreadnew to \newfileline%
                      \ifx\oldfileline\newfileline%
                          \@asyrepeattrue%
                      \else
                          \asyfilechangedtrue%
                          \@asyrepeatfalse%
                      \fi%
                  \fi%
              \fi%
              \if@asyrepeat
          \repeat%
          \closein\@asyreadold%
          \closein\@asyreadnew%
        }%
        {\asyfilechangedtrue}%
    \IfFileExists{#1.pdf}{}{%
        \IfFileExists{#1.png}{}{%
            \IfFileExists{#1.eps}{}{%
                \asyfilechangedtrue%
            }%
        }%
    }%
}

\def\ASYPIC@runasy{%
    \message{Attempting to run asy on \@asypicturename.asy^^J}%
    \shell@execute{asy -noV \@asypicturename.asy 2> \@asyerrorfilename}%
    \openin\@asyreadold=\@asyerrorfilename\relax%
    \ifeof\@asyreadold%
        \closein\@asyreadold% Error log does not exist.
    \else%
        \def\@asyerrormessage{^^J}%
        %Create command to process line of log file to figure
        %out the Asymptote file line number:
        \edef\@asy@temp{%
            \def\noexpand\@asy@processerrorline####1\detokenize\expandafter{%
                \@asypicturename.asy:%
            }%
        }%
        \@asy@temp##2.##3:\relax{%
            \xdef\@asy@errorlinenumber{\numexpr##2\relax}}%
        %
        \gdef\@asy@errorlinenumber{-5}%
        {%
            \endlinechar=-1%
            \loop\unless\ifeof\@asyreadold%
                \readline\@asyreadold to \@asyerrorcurrentline%
                \ifnum\@asy@errorlinenumber=-5%
                {\expandafter\expandafter\expandafter%
                 \@asy@processerrorline\expandafter%
                 % Suffix provides default of -5:
                 \@asyerrorcurrentline\detokenize\expandafter%
                     {\@asypicturename.asy:} -5.1:\relax%
                }%
                \fi%
                \expandafter\g@addto@macro\expandafter\@asyerrormessage%
                \expandafter{\@asyerrorcurrentline^^J}%
            \repeat%
        }%
        \closein\@asyreadold%
        \ifnum\@asy@errorlinenumber=-5%  No error
        \else%
            \openin\@asyreadold\@asypicturename.asy\relax%
            \edef\numlinesout{5}
            \setcounter{@asy@linenumber}{\numlinesout}
            \loop\ifnum\value{@asy@linenumber}<\@asy@errorlinenumber%
                \readline\@asyreadold to \temp%
                \stepcounter{@asy@linenumber}%
            \repeat%
            %
            \addtocounter{@asy@linenumber}{-\numlinesout}%
            %
            {%
                \endlinechar=`^^J%
                \loop\ifnum\value{@asy@linenumber}<\@asy@errorlinenumber%
                    \stepcounter{@asy@linenumber}%
                    \edef\temp/{\arabic{@asy@linenumber}}%
                    \expandafter\g@addto@macro\expandafter%
                      \@asyerrormessage\expandafter{\temp/ }%
                    \readline\@asyreadold to \@asytempmessage%
                    \expandafter\g@addto@macro\expandafter%
                      \@asyerrormessage\expandafter{\@asytempmessage}%
                \repeat%
            }%
            \closein\@asyreadold%
            \PackageError{asypictureB}{\@asyerrormessage}{%
                The Asymptote run described above
                gave a non-empty error log. I have^^J%
                reproduced the error log and attempted
                to print the five lines leading^^J%
                up to the error. Press enter or return
                to continue, and then fix your^^J%
                Asymptote code when this run is done.
            }%
        \fi%
    \fi%
}

\def\FVE@asypicture{\immediate\closeout\asy@OutFile\endgroup%
    \@esphack%
    \ifASYPIC@flush%
        \asyfilechangedtrue%
    \else%
        \ASYPICcomparefiles{\@asypicturename}{\@tempasyfile}%
    \fi%
    \ifasyfilechanged%
        \IfFileExists{\@asypicturename.png}%
            {\deletefile{\@asypicturename.png}}{}%
        \IfFileExists{\@asypicturename.pdf}%
            {\deletefile{\@asypicturename.pdf}}{}%
        \IfFileExists{\@asypicturename.eps}%
            {\deletefile{\@asypicturename.eps}}{}%
        \copyfile{\@tempasyfile.asy}{\@asypicturename.asy}%
        \ASYPIC@runasy%
    \fi%
    \asyset{graphic options}%
    % Avoid giving "file does not exist" errors.
    \chardef\previousinteractionmode=\interactionmode%
    \batchmode%
    \includegraphics[scale=\asy@scale,angle=\asy@angle]%
        {\@asypicturename}%
    \interactionmode=\previousinteractionmode%
}

\DefineVerbatimEnvironment{asypicture}{asypicture}%
    {codes={\catcode`@=0},tabsize=4}

%\AtEndDocument{\deletefile{\@tempasyfile.asy}}
\ifshellescape\else
    \AtEndDocument{\closeout\ASYPIC@shell}
\fi


\makeatother
%    \end{macrocode}
%\iffalse
%</package>
%\fi
% \Finale
