Debian Codegraph @

texmf-dist/source/latex/asypictureb/asypictureB.dtx

raw
1 % \iffalse meta-comment
2 % \fi
3 % \iffalse
4 %<*driver>
5 \documentclass{ltxdoc}
6 \usepackage{lmodern}
7 \usepackage{\jobname}
8 \usepackage{fancyvrb}
9 \usepackage{scalerel}
10 \usepackage{microtype}
11 \usepackage{amssymb}
12 \usepackage{framed}
13 \usepackage[numbered]{hypdoc}
14
15 \setlength{\emergencystretch}{0.5\linewidth}
16 \hypersetup{hidelinks}
17 \newcommand{\packagename}[1]{\textsf{#1}}
18 \newcommand{\danger}{\fontencoding{U}\fontfamily{futs}\selectfont\char 49\relax}
19 \newenvironment{warning}%
20 {%
21 \medskip\noindent\hangindent=3em\hangafter=-2%
22 \clubpenalty=10000%
23 \hbox to0pt%
24 {\hskip-\hangindent\smash%
25 {\raisebox{-0.6\baselineskip}{\scaleto{$\danger$}{1.8\baselineskip}}}%
26 \hfill%
27 }\ignorespaces\textbf{\hspace*{-.2em}Warning:} \ignorespaces%
28 }{%
29 \par\medskip%
30 }
31
32 \EnableCrossrefs
33 \CodelineIndex
34 \begin{document}
35 \DocInput{\jobname.dtx}
36 \end{document}
37 %</driver>
38 % \fi
39 %
40 % \GetFileInfo{\jobname.sty}
41 %
42 % \title{^^A
43 % \packagename{asypictureB} --- user-friendly integration
44 % of Asymptote into \LaTeX\thanks{^^A
45 % This document
46 % corresponds to \packagename{asypictureB}~\fileversion, dated~\filedate.^^A
47 % }^^A
48 % }
49 % \author{Charles Staats III\thanks{E-mail: charles dot staats dot iii at gmail.com}}
50 % \date{Released \filedate}
51 %
52 % \maketitle
53 %
54 % \begin{abstract}
55 % The \packagename{asypictureB} package allows users to integrate Asymptote code
56 % for producing pictures into \LaTeX\ source code using the shell-escape
57 % functionality. It is an alternative to the \texttt{asymptote} package
58 % that comes with Asymptote. The most important advantage of the
59 % \packagename{asypictureB} package is that it provides immediate access to
60 % Asymptote errors by repackaging them as \LaTeX\ errors. It also allows
61 % limited use of TeX macros such as |\the\linewidth| inside Asymptote code.
62 % \end{abstract}
63 %
64 % \tableofcontents
65 %
66 % \section{Introduction}
67 %
68 % The \href{http://asymptote.sourceforge.net}%
69 % {Asymptote programming language}\footnote{\url{http://asymptote.sourceforge.net}}
70 % is a powerful tool for the creation of diagrams, both two- and
71 % three-dimensional, that are compatible with
72 % \TeX\ documents. The programming language ships with a \LaTeX\ package
73 % called (appropriately but confusingly) \packagename{asymptote} that makes it
74 % easy to draw pictures by including Asymptote code in the \LaTeX\ source
75 % file. The \packagename{asymptote} package also allows the inclusion of
76 % interactive three-dimensional images in |pdf| files.\footnote{
77 % The \packagename{asypictureB} package does not
78 % currently support this feature.}
79 %
80 % Unfortunately, the author has encountered the several annoyances
81 % using the \packagename{asymptote} package.
82 % \begin{enumerate}
83 % \item
84 % When my Asymptote code contains errors that prevent it
85 % from compiling, I have found it extremely difficult to track down
86 % the offending line in the \TeX\ source file. This has been the
87 % single biggest annoyance and has led me to compose all but the simplest
88 % Asymptote images as separate |.asy| files, which are then
89 % included into the \TeX\ source once they do what I want.
90 % \item
91 % The \packagename{asymptote} package does not support PNG files,
92 % although the Asymptote language does.
93 % \item
94 % The \packagename{asymptote} package does have a mechanism in place so that
95 % when it is used with |latexmk|, Asymptote images that have not changed are
96 % not recompiled. Unfortunately, the mechanism for recognizing unchanged images
97 % is somewhat fragile: if a single image is inserted or deleted, then all
98 % subsequent images will have to be recompiled. In my experience, this can
99 % make a compilation last several minutes that would otherwise have lasted
100 % several seconds.
101 % \item
102 % The \packagename{asymptote} package can rescale an Asymptote-produced
103 % image to a given width and/or a given height, but it cannot rescale the
104 % image by a given scaling factor.
105 % \end{enumerate}
106 % The most important issue here is the first, which can to some extent be fixed
107 % using editor features; see, for instance,
108 % \href{http://www.artofproblemsolving.com/Wiki/index.php/Asymptote:_Advanced_Configuration\#Showing_Asymptote_error_messages_in_TeXnicCenter}%
109 % {this explanation for TeXnicCenter (Windows only)}\footnote{\raggedright
110 % \url{http://www.artofproblemsolving.com/Wiki/index.php/Asymptote:_Advanced_Configuration\#Showing_Asymptote_error_messages_in_TeXnicCenter}
111 % }.
112 %
113 % The \packagename{asypictureB} package is an alternative to the
114 % \packagename{asymptote}
115 % package that (optionally)
116 % uses |\write18| to call the Asymptote compiler directly from
117 % \LaTeX. The package provides some sort of fix for
118 % all the above issues:
119 % \begin{enumerate}
120 % \item
121 % Asymptote errors are repackaged as \LaTeX\ errors and reported immediately.
122 % At a minimum, users are shown the Asymptote error log and a line number
123 % that will allow them to locate the correct Asymptote picture within the
124 % \TeX\ source file.
125 %
126 % Additionally, \packagename{asypictureB} is usually able to display the
127 % five lines of Asymptote code up to and including the first error. This is
128 % useful to locate the line of code
129 % on which the error occurs, since the line numbers in the Asymptote error log
130 % do not correspond to the line numbers in the \LaTeX\ source file.
131 % %
132 % \item
133 % Any file type that is supported by both the Asymptote language and the
134 % |\includegraphics| command is supported by \packagename{asypictureB}.
135 % Assuming that the document is compiled using \texttt{pdflatex}, this includes
136 % the PNG, PDF, and (more or less) EPS file formats.
137 % \item
138 % Users are permitted and strongly encouraged to specify a distinct name for
139 % each Asymptote image in the file. Distinctly named Asymptote images
140 % are not recompiled if their
141 % code has not changed since the last |latex| run.
142 % \item Any option that works for the |\includegraphics| command can be given
143 % as an option to an |asypicture| environment.
144 % \end{enumerate}
145 %
146 % There is one other feature worth mentioning: \LaTeX\ macros will be expanded
147 % inside Asymptote code if they are prefixed by @ instead of |\|. This
148 % can be used as an incomplete substitute for the |inline| option of the
149 % \packagename{asymptote} package. It also provides a way to use macros
150 % in Asymptote code, which is not a feature that Asymptote supports otherwise.
151 %
152 % Each of these features could stand significant improvements. However,
153 % since implementing them, the author has found himself much more willing
154 % to compose Asymptote code directly in a \TeX\ source file. This indicates
155 % to him that the package might prove useful to others, even in its current
156 % form. His hope is that the best ideas of this package would
157 % be copied and improved in the official \packagename{asymptote} package,
158 % allowing him to deprecate \packagename{asypictureB}.
159 %
160 % \section{Installation and running}
161 %
162 % First of all, the \packagename{asypictureB} package is mostly useless unless you
163 % have Asymptote installed on your system.
164 % \begin{itemize}
165 % \item For a MacOS system, this installation
166 % is automatic with a standard installation
167 % of \href{http://tug.org/mactex/}{MacTeX}.
168 % \item For a Windows system, the
169 % \href{http://asymptote.sourceforge.net/doc/Microsoft-Windows.html}{official
170 % installation instructions} are fairly good. As of this writing, the most
171 % recent version of the |setup.exe| file can be downloaded from
172 % \url{http://sourceforge.net/projects/asymptote/files/2.95/}.
173 % \item
174 % For a Unix-like system, a version of
175 % Asymptote is included in TeX Live, but there may be additional dependencies;
176 % see, for instance, \url{http://tex.stackexchange.com/a/155284/484}. You
177 % should also consult
178 % \href{http://asymptote.sourceforge.net/doc/UNIX-binary-distributions.html}{these}
179 % \href{http://asymptote.sourceforge.net/doc/Compiling-from-UNIX-source.html}{two}
180 % pages from the official documentation.
181 % \end{itemize}
182 %
183 % To use the \texttt{asypictureB} package,
184 % your \texttt{.tex} file should be run with shell-escape
185 % enabled:
186 % \begin{Verbatim}[commandchars=\\\{\},gobble=2]
187 % pdflatex -shell-escape \meta{filename}
188 % \end{Verbatim}
189 % [Note that |latex|, |lualatex|, etc.\ can be substituted for |pdflatex|, although
190 % you should make sure that the engine you use is compatible with whatever
191 % graphics formats your Asymptote pictures are compiled to.]
192 %
193 % \subsection{Running without shell escape}
194 %
195 % If you are unwilling to use shell-escape, \packagename{asypictureB}
196 % creates a script that makes it easy to execute the necessary commands
197 % afterwards. To use it, run the following four commands at the terminal.
198 % (The second command is optional; it allows you to inspect the script
199 % before running it.)
200 % For convenience, it is assumed that the name of the \LaTeX\ source file
201 % is |foo.tex|.
202 %
203 % \bigskip\noindent
204 % \begin{minipage}{0.48\linewidth}
205 % Windows:
206 % \begin{Verbatim}[commandchars=\\\{\},gobble=2]
207 % pdflatex foo
208 % type foo-compile_asy.txt
209 % type foo-compile_asy.txt|cmd
210 % pdflatex foo
211 % \end{Verbatim}
212 % \end{minipage}
213 % \hfill
214 % \begin{minipage}{0.48\linewidth}
215 % MacOS and Unix-like systems:
216 % \begin{Verbatim}[commandchars=\\\{\},gobble=2]
217 % pdflatex foo
218 % cat foo-compile_asy.sh
219 % sh foo-compile_asy.sh
220 % pdflatex foo
221 % \end{Verbatim}
222 % \end{minipage}
223 %
224 % \medskip\noindent
225 % Instead of |pdflatex|, one may use |latex|, |lualatex|, \ldots. Asymptote
226 % errors will be visible as \TeX\ errors on the second run of \LaTeX.
227 %
228 % \begin{warning}
229 % This method is not entirely foolproof. In particular, if |pdflatex| is run
230 % twice in a row without running the \meta{filename}-|compile_asy|
231 % script in between, then
232 % Asymptote pictures which have been compiled at some point in the past, even
233 % if they have since been altered, will
234 % not be recompiled. Should this happen, you can force every |asypicture|
235 % to be recompiled by temporarily adding the |\RequireAsyRecompile| command
236 % before your first |asypicture|.
237 % \end{warning}
238 %
239 % \paragraph{The safer method}
240 % If you want to compile a \TeX\ source file from someone else (e.g.,
241 % the internet), you may want neither to enable shell-escape nor to
242 % run a script generated by this file.
243 % A safer way to compile
244 % all Asymptote pictures generated by \packagename{asypictureB}
245 % (and for that matter by the \packagename{asymptote} package) is to run
246 % |asy -noV |\meta{filename}|-*.asy| after compiling \meta{filename}|.tex|.
247 % Once this is done, all the Asymptote files should be compiled, will be
248 % correctly imported upon a second run of |pdflatex| (or |latex|, \ldots).
249 % Re-running |asy| is not necessary until and unless any of the Asymptote
250 % pictures change. This method is ``safe'' in that you have greater
251 % control over which programs are actually being run.
252 %
253 % This method will also work for your own files-in-progress, but is not
254 % recommended for two reasons:
255 % \begin{itemize}
256 % \item Asymptote errors will not be repackaged as \TeX\ errors, negating
257 % one of the main features of the \packagename{asypictureB} package.
258 % \item All Asymptote pictures will be recompiled, even if they have not
259 % changed since the last run. This can add considerably to the compile time.
260 % \end{itemize}
261 %
262 % \subsection{Dependencies}
263 % As currently implemented, the \packagename{asypictureB} package requires
264 % the packages \packagename{fancyvrb} (tested with version 2.8),
265 % \packagename{graphicx} (tested with version 2005/11/14),
266 % \packagename{pgfkeys}, and \packagename{ifplatform} (tested with
267 % version 0.4). In fact, it depends
268 % on undocumented internals of the \packagename{fancyvrb} package, so
269 % later versions of this package could conceivably break it as well as earlier versions.
270 %
271 % If shell-escape is
272 % not enabled, it also requires the \packagename{verbatimcopy} package,
273 % version 0.06 or later. Since \packagename{verbatimcopy} is not backwards
274 % compatible, earlier versions will, in fact, break \packagename{asypictureB}.
275 %
276 % \section{Usage}
277 %
278 % \DescribeEnv{asypicture} Code for Asymptote pictures should be placed
279 % within the |asypicture| environment, which takes one mandatory argument:
280 % a list of comma-separated expressions of the form
281 % \meta{key}\texttt{=}\meta{value}. It is strongly recommended that the
282 % key |name| always be used, since this will prevent the package from
283 % re-compiling pictures whose code has not changed. The time saving can
284 % be substantial if you have a document with a significant number of
285 % Asymptote pictures.
286 %
287 % Keys other than |name| are passed onto an |\includegraphics|
288 % command from the \packagename{graphicx} package. The keys should be given
289 % in the following order:
290 % \begin{enumerate}
291 % \item Keys other than |scale| or |angle|.
292 % \item The |scale| key (if it is used).
293 % \item The |angle| key (if it is used).
294 % \end{enumerate}
295 % The way the package is currently implemented, the |scale| and |angle| keys
296 % will effectively be evaluated last, regardless of the order in which they
297 % are given. However, this behavior is not ideal and may change in future versions
298 % of the package; the goal would be that keys should be evaluated in the order
299 % in which they are given. For the time being, giving keys in the order specified
300 % should ensure compatibility with future versions.
301 %
302 % It is possible to expand macros within an |asypicture| environment by using an
303 % at symbol
304 % \texttt{@} in place of a backslash |\|. For instance, the line
305 % \begin{Verbatim}[gobble=2]
306 % size(@the@linewidth, 0);
307 % \end{Verbatim}
308 % will be translated to something like
309 % \begin{Verbatim}[gobble=2]
310 % size(345.0pt, 0);
311 % \end{Verbatim}
312 % before being compiled by Asymptote.
313 %
314 % \bigskip\noindent
315 % \DescribeEnv{asyheader}Code within an |asyheader| environment is appended to
316 % the ``header,'' which is inserted at the beginning of every \texttt{.asy} file
317 % output by the \packagename{asypictureB} package. Initially, the header
318 % consists of the two lines
319 % \begin{Verbatim}[gobble=2]
320 % defaultpen(fontsize(@getfontsize pt));
321 % settings.prc = false;
322 % \end{Verbatim}
323 % Again, macros can be expanded inside an |asyheader| environment by prefixing them
324 % by @ rather than |\|. The macros are fully expanded when they are added to the
325 % header, not when they are written to a file. Thus, for instance, in a 10-point
326 % document, the first line of the header will always read
327 % \begin{Verbatim}[gobble=2]
328 % defaultpen(fontsize(10pt));
329 % \end{Verbatim}
330 % even if the font size is later changed to 12 points.
331 %
332 % Note that any change to the header will require all subsequent |asypicture|
333 % environments to be recompiled. In a document with many Asympotote images,
334 % this could take a while.
335 %
336 % \bigskip\noindent
337 % \DescribeMacro{\getfontsize}The |\getfontsize| macro is an alias for
338 % |\f@size| that does not require |\makeatletter|. It expands to the current
339 % font size (without the suffix |pt|).
340 %
341 % \bigskip\noindent\DescribeMacro{\asylistingfile}The |\asylistingfile| macro
342 % contains the filename of the Asymptote code for the most recent |asypicture|.
343 % It can be used with commands from e.g. \packagename{fancyvrb} or
344 % \packagename{listings} to display the Asymptote code for an image.
345 %
346 % \bigskip\noindent
347 % \DescribeMacro{\RequireAsyRecompile}\DescribeMacro{\AsyCompileIfNecessary}
348 % By default, Asymptote images are compiled only if the Asymptote code has changed.
349 % The command |\RequireAsyRecompile| changes this setting to make all subsequent
350 % images recompile even if the code has not changed.
351 % (This can be useful, for instance,
352 % if you have just updated Asymptote.) The command
353 % |\AsyCompileIfNecessary| restores the default behavior for all subsequent
354 % Asymptote pictures.
355 %
356 % \subsection{Keys for \texttt{asypicture}}
357 %
358 % \DescribeMacro{name} If the key-value combination |name=|\meta{picturename}
359 % appears in the mandatory argument to an |asypicture| environment, then the
360 % contents of that environment will be saved to the file
361 % \begin{Verbatim}[gobble=2,commandchars=\\\{\}]
362 % \meta{filename}-\meta{picturename}.asy
363 % \end{Verbatim}
364 % where \meta{filename} is the name of the current |.tex| file (not including
365 % the |.tex| extension).
366 % The compiled Asymptote picture is saved to an image file such as
367 % \texttt{\meta{filename}-\meta{picturename}.eps},
368 % \texttt{\meta{filename}-\meta{picturename}.pdf}, or
369 % \texttt{\meta{filename}-\meta{picturename}.png}.
370 %
371 % Alphanumeric characters are allowed in the |name| key, as are the
372 % characters |-| (hyphen) and |_| (underscore). Other characters (including
373 % spaces, asterisks, etc.) should
374 % be avoided. The key may begin with any allowed character; however, names
375 % consisting of a single number with three or fewer digits are discouraged,
376 % since these may conflict with the \packagename{asymptote} package.
377 %
378 % Although the |name| key is technically not required,
379 % it is strongly recommended that distinct names be given
380 % to all the different |asypicture|
381 % environments. This allows the \packagename{asypictureB} package to avoid
382 % recompiling pictures whose code has not changed---even if the pictures are
383 % reordered.
384 %
385 % If the name \meta{picturename} has been used before,
386 % a suffix will be appended to it: |__1| for the first repeat, |__2| for
387 % the second repeat, etc. Thus, if the same name (say |foo|)
388 % is used for several |asypicture|s,
389 % then deleting the first of these will force all the others to recompile;
390 % but changing a picture of a different name will not affect any of the
391 % pictures named |foo|.
392 %
393 % If no |name| key is given, the key-value combination |name=noname| is
394 % assumed. This default can be changed using the |\asyset| command: after
395 % the line
396 % \begin{Verbatim}[gobble=2]
397 % \asyset{name = foo}
398 % \end{Verbatim}
399 % nameless pictures will be called |foo| rather than |noname|.
400 %
401 % \begin{warning}
402 % If \packagename{asypictureB} determines that the contents of an |asypicture|
403 % environment named \meta{picturename}
404 % have changed since the last run, it will delete
405 % all of the following files that exist:
406 % \begin{Verbatim}[gobble=2,commandchars=\\\{\}]
407 % \texttt{\meta{filename}-\meta{picturename}.eps}
408 % \texttt{\meta{filename}-\meta{picturename}.pdf}
409 % \texttt{\meta{filename}-\meta{picturename}.png}
410 % \end{Verbatim}
411 % \end{warning}
412 %
413 % \subsection{Styles}
414 % \DescribeMacro{\asyset}
415 % Since \packagename{asypictureB} uses \packagename{pgfkeys} for keys,
416 % it is possible to create new keys, called ``styles,'' that set
417 % several other keys at once. For instance, the following line
418 % \begin{Verbatim}[gobble=2]
419 % \asyset{mysize/.style = {width=6cm, height=4cm}}
420 % \end{Verbatim}
421 % creates a new key called |mysize|. Any time in the document after this
422 % line, the command
423 % \begin{Verbatim}[gobble=2]
424 % \begin{asypicture}{name=picturename, mysize}
425 % \end{Verbatim}
426 % is exactly equivalent to the line
427 % \begin{Verbatim}[gobble=2]
428 % \begin{asypicture}{name=picturename, width=6cm, height=4cm}
429 % \end{Verbatim}
430 % This is precisely the same style mechanism that is used in TikZ; in
431 % fact, |\asyset|\marg{keys} is equivalent to |\pgfkeys{/asy/.cd,|\meta{keys}|}|,
432 % much as |\tikzset|\marg{keys} is equivalent to |\pgfkeys{/tikz/.cd,|\meta{keys}|}|.
433 %
434 % The |\asyset| command may be used in the preamble or anywhere in the body of
435 % the document where macros are processed normally. It may not be used in the
436 % body of an |asypicture| or any other verbatim-like environment.
437 %
438 % \section{Examples}
439 %
440 % Here is a simple example:
441 %
442 % \noindent\def\asywidth{2.5cm}
443 % \begin{minipage}[t]{\asywidth}
444 % \vspace*{0pt}
445 % \begin{asypicture}[gobble=2]{name = sphere_image, width=\asywidth}
446 % settings.outformat = "png";
447 % settings.render = 16;
448 % size(@asywidth,0);
449 % import three;
450 % draw(unitsphere, white);
451 % \end{asypicture}
452 % \end{minipage}
453 % \hfill
454 % \begin{minipage}[t]{\dimexpr\linewidth-\asywidth-10pt\relax}
455 % \vspace*{0pt}
456 % \begin{Verbatim}[gobble=2,codes={\catcode`@=0},frame=single]
457 % \begin{asypicture}{name=sphere_image}
458 % settings.outformat = "png";
459 % settings.render = 16;
460 % size(@asywidth,0);
461 % import three;
462 % draw(unitsphere, white);
463 % \end{asypicture}
464 % \end{Verbatim}
465 % \end{minipage}
466 %
467 % \medskip\noindent
468 % Here is an example that uses the user-defined macro |\asywidth|
469 % inside the Asymptote code to avoid duplicate code.
470 %
471 % \begin{Verbatim}[gobble=2,tabsize=4,frame=single]
472 % \noindent\def\asywidth{4cm}
473 % \begin{asypicture}{name=triangle,width=\asywidth}
474 % settings.outformat="pdf";
475 % size(@asywidth,0);
476 % draw((0,0) -- (1,0) -- (1,1) -- cycle);
477 % dot((1,0), L=Label(
478 % "\textbf{Bold} \textsc{Smallcaps}",
479 % align=NE));
480 % \end{asypicture}
481 % \hfill
482 % \begin{minipage}{\dimexpr\linewidth-\asywidth-15pt\relax}
483 % \VerbatimInput[frame=leftline]{\asylistingfile}
484 % \end{minipage}
485 % \end{Verbatim}
486 % The result:
487 %
488 % \noindent\def\asywidth{4cm}
489 % \begin{asypicture}[gobble=2]{name=triangle,width=\asywidth}
490 % settings.outformat="pdf";
491 % size(@asywidth,0);
492 % draw((0,0) -- (1,0) -- (1,1) -- cycle);
493 % dot((1,0), L=Label(
494 % "\textbf{Bold} \textsc{Smallcaps}",
495 % align=NE));
496 % \end{asypicture}
497 % \hfill
498 % \begin{minipage}{\dimexpr\linewidth-\asywidth-15pt\relax}
499 % \VerbatimInput[frame=leftline]{\asylistingfile}
500 % \end{minipage}
501 %
502 % \bigskip
503 % The |\VerbatimInput| command is from the \packagename{fancyvrb} package.
504 % The first two lines of the Asymptote code listing come from the default
505 % |asyheader|. These, along with some extra whitespace,
506 % can be eliminated by playing with the \packagename{fancyvrb} options.
507 % More importantly for our purposes, changing the single line
508 % \begin{Verbatim}[gobble=2]
509 % \noindent\def\asywidth{4cm}
510 % \end{Verbatim}
511 % to
512 % \begin{Verbatim}[gobble=2]
513 % \noindent\def\asywidth{4.5cm}
514 % \end{Verbatim}
515 % will automatically affect both the width of the |asypicture| and the
516 % width of the |minipage|:
517 %
518 % \begin{Verbatim}[gobble=2,tabsize=4,frame=single]
519 % \noindent\def\asywidth{4.5cm}
520 % \begin{asypicture}{name=triangle2,width=\asywidth}
521 % settings.outformat="pdf";
522 % size(@asywidth,0);
523 % draw((0,0) -- (1,0) -- (1,1) -- cycle);
524 % dot((1,0), L=Label(
525 % "\textbf{Bold} \textsc{Smallcaps}",
526 % align=NE));
527 % \end{asypicture}
528 % \hfill
529 % \begin{minipage}{\dimexpr\linewidth-\asywidth-15pt\relax}
530 % \VerbatimInput[frame=leftline,gobble=4,firstline=5]
531 % {\asylistingfile}
532 % \end{minipage}
533 % \end{Verbatim}
534 % produces
535 %
536 % \noindent\def\asywidth{4.5cm}
537 % \begin{asypicture}[gobble=2]{name=triangle2,width=\asywidth}
538 % settings.outformat="pdf";
539 % size(@asywidth,0);
540 % draw((0,0) -- (1,0) -- (1,1) -- cycle);
541 % dot((1,0), L=Label(
542 % "\textbf{Bold} \textsc{Smallcaps}",
543 % align=NE));
544 % \end{asypicture}
545 % \hfill
546 % \begin{minipage}{\dimexpr\linewidth-\asywidth-15pt\relax}
547 % \VerbatimInput[frame=leftline,gobble=4,firstline=5]
548 % {\asylistingfile}
549 % \end{minipage}
550 %
551 % \section{Error handling}
552 %
553 % It has been stated several places in this document that
554 % \packagename{asypictureB} repackages Asymptote errors as
555 % \TeX\ errors (and at least once that this feature, among others,
556 % could stand significant improvements). In this section is described
557 % exactly how these errors are repackaged, at least as of the current
558 % implementation. None of this is guaranteed to remain the same in
559 % future versions.
560 %
561 % When \packagename{asypictureB} tells Asymptote to compile the file
562 % \begin{quote}
563 % \texttt{\meta{filename}-\meta{picturename}.asy},
564 % \end{quote}
565 % it reroutes all
566 % warning and error messages to the file
567 % \begin{quote}
568 % \texttt{\meta{filename}-\meta{picturename}\_errors.txt}.
569 % \end{quote}
570 % Once the Asymptote run is complete, it checks whether the error file
571 % has any lines of the form
572 % \begin{quote}
573 % \texttt{\meta{filename}-\meta{picturename}.asy:\meta{number}.\meta{stuff}}
574 % \end{quote}
575 % If so, it throws a \LaTeX\ package error and displays
576 % the contents of the error file, which include all the errors and
577 % warnings issued by Asymptote. Note that if some kind of asy error occurs
578 % that does not match this output form, \packagename{asypictureB} will not
579 % notice the error.
580 %
581 % The \LaTeX\ error message will give the line number of the
582 % |\end{asypicture}| command for the |asypicture| that caused the error, which
583 % is of limited use in isolating the error.
584 % More precise line numbers are given by the Asymptote error log;
585 % however, these line numbers are
586 % for the |asy| file rather than the |tex| file, and consequently not
587 % terribly helpful.
588 %
589 % To allow the user to locate the line on which the actual error occurred,
590 % the \packagename{asypictureB} package attempts to parse the Asymptote
591 % error log and print out the five lines leading up to the error. More precisely,
592 % it does the following:
593 % \begin{enumerate}
594 % \item From the first line of the form
595 % \begin{quote}
596 % \texttt{\meta{filename}-\meta{picturename}.asy:\meta{number}.\meta{stuff}}
597 % \end{quote}
598 % the \meta{number} is extracted. Note that if no successful extraction occurs,
599 % there will be no \LaTeX\ error.
600 % \item Assuming \meta{number} was extracted successfully, the lines
601 % of the |asy| file from
602 % the inclusive range $\meta{number}-5$ to \meta{number} are displayed
603 % as part of the \LaTeX\ error message. These lines are usually identical
604 % and almost always similar to the lines in the actual |tex| file
605 % leading up to the error. In the author's experience, this is usually enough
606 % information to locate without difficulty the line on which the error
607 % occurred.\footnote{I.e., the line that caused Asymptote to choke.
608 % The usual rules of debugging apply: the line on which the compiler identified
609 % an error might have been correct if not for an earlier mistake that
610 % was syntactically correct.}
611 % \end{enumerate}
612 %
613 % \noindent Here's an example: Consider \LaTeX\ file
614 % \begin{Verbatim}[gobble=2,numbers=left,tabsize=4]
615 % \documentclass{article}
616 % \usepackage{asypictureB}
617 % \begin{document}
618 % \def\asywidth{5cm}
619 % \begin{asypicture}{name=error_example}
620 % // A comment
621 % size(@asywidth, 0);
622 % path l1 = (0,0) -- (1,1);
623 % // Another comment
624 % draw(box((0,0),(1,1)))
625 % draw(l1, dotted);
626 % draw(l2, dashed);
627 % \end{asypicture}
628 % \end{document}
629 % \end{Verbatim}
630 % If this file is saved as \texttt{asyerrorexample.tex} and then
631 % compiled with the shell-escape option, the following error results:
632 % \begin{Verbatim}[gobble=2]
633 % ! Package asypictureB Error:
634 % draw(l1, dotted);
635 % ^
636 % asyerrorexample-error_example.asy: 10.5: syntax error
637 % error: could not load module 'asyerrorexample-error_example.asy'
638 %
639 % 6 size(5cm, 0);
640 % 7 path l1 = (0,0) -- (1,1);
641 % 8 // Another comment
642 % 9 draw(box((0,0),(1,1)))
643 % 10 draw(l1, dotted);
644 % .
645 %
646 % See the asypictureB package documentation for explanation.
647 % Type H <return> for immediate help.
648 % ...
649 %
650 % l.13 \end{asypicture}
651 % \end{Verbatim}
652 % The \LaTeX\ error message first repeats the Asymptote error log,
653 % which explains that there was a syntax error. Next, the five lines
654 % leading up to the error are displayed.
655 % Looking at these five lines,
656 % one can determine that the difficulty was a comma omitted
657 % on line 9 of the Asympote file. (It is diagnosed on line 10 because
658 % of how the compiler works.) Looking at the context, this corresponds
659 % to line 10 of the \LaTeX\ file.
660 %
661 % Note that the macro |@asywidth| in the |asypicture| code
662 % has been expanded to \texttt{5cm} in the error message.
663 %
664 % \section{Macros in \texttt{asypicture}s}
665 %
666 % When the author first conceived of allowing macros in an
667 % |asypicture| environment, the goal was to allow expressions like
668 % |size(@asywidth,0);| where |\asywidth| was a user-defined macro
669 % also used in the \LaTeX\ code to avoid hard-coding numbers.
670 % However, it was almost immediately apparent that this feature
671 % has more sophistocated uses, such as allowing user-defined
672 % syntax or even creating something similar to templates\footnote{If you
673 % actually want to use templates in Asymptote code, the experimental
674 % \emph{templated imports} feature of the language is probably a
675 % better choice than the macro techniques described in this section}.
676 %
677 % \subsection{Limitations}
678 %
679 % Before discussing the nifty features, let's discuss the fairly
680 % severe limitations they will have to work around.
681 %
682 % \begin{description}
683 % \item[No grouping symbols.] If a macro takes a mandatory argument
684 % inside braces |{}|, then that macro cannot be used inside an
685 % |asypicture|.
686 % \item[Purely expandable macros only.] Macros will be expanded, but not
687 % executed. In particular, macros that allow optional arguments will usually
688 % go horribly wrong.
689 % \item[One line only.] As we will discuss, it is
690 % possible to use ``unconventionally delimited'' arguments. However, even
691 % in this case, a macro and all its arguments must fit on a single line.
692 % \end{description}
693 %
694 % Since the first two points all but forbid the use of macros with
695 % conventional arguments, it might
696 % be wondered whether macros in Asymptote code can be used for anything
697 % more interesting than storing user-defined lengths. They can.
698 %
699 % \subsection{Single-token arguments}
700 %
701 % [This is really more a fix than an example, but this seems as good a
702 % place as any to discuss it.]
703 %
704 % Some macros, such as |\the|,
705 % take arguments that consist only of a single character or
706 % control sequence. For instance, |\the\textwidth| expands to something like
707 % \texttt{345.0pt}, whereas |\textwidth| by itself expands only to
708 % |\textwidth|. This can be significant if you want to produce an
709 % Asymptote picture that takes up a specified fraction of the text width.
710 %
711 % \subsection{Unconventionally delimited arguments}
712 %
713 % The \TeX\ primitive |\def| can be used to produce macros that are
714 % quite flexible about how they are delimited. For instance, the \TeX\ code
715 % \begin{Verbatim}[gobble=2]
716 % \def\draw#1;{draw(#1);}
717 % \end{Verbatim}
718 % will take as its argument everything between the |\draw| command and the
719 % first subsequent semicolon. If this line showed up in \TeX\ code
720 % (preferably in the preamble), then subsequent |\asypicture| environments
721 % could include a line such as
722 % \begin{Verbatim}[gobble=2]
723 % @draw box((0,0), (1,1));
724 % \end{Verbatim}
725 % as an arguably more aesthetic alternative to the translation
726 % \begin{Verbatim}[gobble=2]
727 % draw( box((0,0), (1,1)));
728 % \end{Verbatim}
729 %
730 % A more advanced example is essentially a template for a sorting
731 % function\footnote{The algorithm here is a somewhat inefficient
732 % mergesort.}.
733 % Include the following code in the \TeX\ preamble:
734 % \begin{Verbatim}[gobble=2]
735 % \def\definesortfunction #1;{%
736 % #1[] sort(#1[] a) {
737 % if (a.length <= 1) return a;
738 % static #1[] merge(#1[] b, #1[] c) {
739 % #1[] toreturn;
740 % int i = 0, j = 0;
741 % while (i < b.length && j < c.length) {
742 % if (!(c[j] < b[i])) { toreturn.push(b[i]); ++i; }
743 % else { toreturn.push(c[j]); ++j; }
744 % }
745 % while (i < b.length) {
746 % toreturn.push(b[i]);
747 % ++i;
748 % }
749 % while (j < c.length) {
750 % toreturn.push(c[j]);
751 % ++j;
752 % }
753 % return toreturn;
754 % }
755 % int halfway = floor(a.length / 2);
756 % #1[] b = sort(a[0:halfway]);
757 % #1[] c = sort(a[halfway:a.length]);
758 %
759 %
760 % return merge(b, c);
761 % }}
762 % \end{Verbatim}
763 % Then within any |asypicture| environment, the line |@definesortfunction T;|
764 % can be used to define a function |T[] sort(T[])| that returns a sorted
765 % version of its argument, for any type |T| for which the less than operator
766 % |<| is defined. For instance, within an |asypicture|, the code
767 % \begin{Verbatim}[gobble=2]
768 % bool operator <(pair a, pair b) {
769 % return (a.x < b.x || (a.x == b.x && a.y < b.y));
770 % }
771 %
772 % @definesortfunction pair;
773 % \end{Verbatim}
774 % makes available a lexicographic sorting routine for ordered pairs of real
775 % numbers.
776 %
777 % Unfortunately, while this compiles correctly, the resulting Asymptote
778 % file has no line breaks in the entire definition of the sort function.
779 % If you want the Asymptote code (as opposed to just the |asypicture| code)
780 % to be readable, the following setup, proposed by Enrico Gregorio\footnote{
781 % \url{http://tex.stackexchange.com/a/160740/484}}, does the job:
782 %
783 % \begin{Verbatim}[gobble=2]
784 % \begingroup
785 % \endlinechar=`^^J \obeyspaces% end of lines are newlines
786 % \gdef\definesortfunction #1;{% eat up the space following the macro
787 % #1[] sort(#1[] a) {
788 % if (a.length <= 1) return a;
789 % static #1[] merge(#1[] b, #1[] c) {
790 % #1[] toreturn;
791 % int i = 0, j = 0;
792 % while (i < b.length && j < c.length) {
793 % if (!(c[j] < b[i])) { toreturn.push(b[i]); ++i; }
794 % else { toreturn.push(c[j]); ++j; }
795 % }
796 % while (i < b.length) {
797 % toreturn.push(b[i]);
798 % ++i;
799 % }
800 % while (j < c.length) {
801 % toreturn.push(c[j]);
802 % ++j;
803 % }
804 % return toreturn;
805 % }
806 % int halfway = floor(a.length / 2);
807 % #1[] b = sort(a[0:halfway]);
808 % #1[] c = sort(a[halfway:a.length]);
809 %
810 %
811 % return merge(b, c);
812 % }% this % is necessary
813 % }% this % is necessary
814 % \endgroup% this % is necessary
815 % \end{Verbatim}
816 %
817 % \begin{warning}
818 % If two commands use the same unconventional delimiter, then they cannot
819 % be nested. In particular, a command cannot appear inside its own argument.
820 % This is one of the reasons people usually delimit with grouping
821 % symbols, which is not an option here, short of using |@bgroup| and
822 % |@egroup| with copious occurrences of |@expandafter|.
823 % \end{warning}
824 % \begin{warning}
825 % The |\def| command is not expandable. Thus, the code defining the
826 % macros must be given \emph{outside} |asypicture| environments,
827 % even when the macros are intended for use exclusively inside |asypicture|s.
828 % \end{warning}
829 %
830 %
831 % \section{Missing features}
832 %
833 % The introduction of this documentation touted ways in which
834 % \packagename{asypictureB} improves on the behavior of the \packagename{asymptote}
835 % package. In this section, I clarify ways in which it falls short and
836 % describe how to compensate where possible. My hope
837 % is that these differences would narrow in both directions until only
838 % one package is necessary---preferably the official \packagename{asymptote}
839 % package.
840 %
841 % Note that some of these ``missing features'' cannot be fixed without
842 % breaking backwards compatibility. This is largely why the package is
843 % named \packagename{asypictureB}, allowing room for a future, more
844 % powerful version named \packagename{asypicture}, in case the
845 % \packagename{asymptote} package does not step into the gap.
846 %
847 % \begin{framed}
848 % \noindent\textbf{Tip:} To retrieve the documentation for the
849 % \packagename{asymptote} \LaTeX\ package,
850 % type \texttt{texdoc asy-latex} at the command line. To retrieve the
851 % documentation for the Asymptote programming language, type
852 % \texttt{texdoc asymptote}. To retrieve the documentation for
853 % \packagename{asypictureB}, type \texttt{texdoc asypictureB}.
854 % \end{framed}
855 %
856 % \paragraph{No 3d interaction}
857 % One of the most spectacular features of the \packagename{asymptote} package is
858 % the embedding of interactive three-dimensional images in PDF files.
859 % (Note that this requires the |inline| option when loading the package.)
860 % The \packagename{asypictureB} package currently has no such feature.
861 % In fact, the default header includes
862 % the line \texttt{settings.prc = false;} to prevent Asymptote from trying
863 % to produce an interactive (|prc|) image, since this might break things.
864 %
865 % \paragraph{No \texttt{inline} option}
866 % A second feature that is important, but less spectacular, is the |inline|
867 % option itself, which causes the Asymptote labels to be compiled (in \LaTeX)
868 % with all the
869 % same packages loaded and macros defined as in the original document.
870 % For instance, font consistency between the Asymptote images and the
871 % larger document is ensured by this option.
872 % It comes with an important limitation---Asymptote cannot use size information
873 % about the labels when this option is in use.
874 %
875 % The closest
876 % equivalent in the \packagename{asypictureB} package is the immediate
877 % expansion of macros introduced by the @ symbol. This does not have the
878 % drawback of the |inline| option, and has many additional uses. However,
879 % it is less flexible inside labels (since, e.g., macros with arguments
880 % cannot be used this way), and does not ensure font consistency.
881 %
882 % To compensate for this when using \packagename{asypictureB}, important
883 % packages and macro definitions should be echoed in the Asymptote header.
884 % Here is an example of code that could be placed in the preamble of a document
885 % to make the macro |\RR| (for $\mathbb R$) available in both the document
886 % text and the labels of its Asymptote pictures:
887 % \begin{Verbatim}[gobble=2]
888 % \usepackage{amssymb}
889 % \newcommand{\RR}{\mathbb{R}}
890 % \begin{asyheader}
891 % usepackage("amssymb");
892 % texpreamble("\newcommand{\RR}{\mathbb{R}}");
893 % \end{asyheader}
894 % \end{Verbatim}
895 %
896 % \paragraph{The output format is not automatically set.}
897 % When using \packagename{asymptote}, the output format is automatically
898 % set to either |eps| or |pdf| depending on what \TeX\ engine is being run
899 % (and which kind of graphics file it prefers). When using \packagename{asypictureB},
900 % no such provision is made; if any format other than |eps| is desired,
901 % it must be selected by including the Asymptote code
902 % \texttt{settings.outformat="pdf";} (or |"png"|). This is by design, to allow
903 % the use of |png| files; but it can be a bit annoying at times. To set all
904 % Asymptote files to have |pdf| format, include the code
905 % \begin{Verbatim}[gobble=2]
906 % \begin{asyheader}
907 % settings.outformat="pdf";
908 % \end{asyheader}
909 % \end{Verbatim}
910 % in the preamble (or anywhere prior to the first |asypicture| environment).
911 %
912 % \paragraph{Keys to \texttt{asypicture} are not conveyed to Asymptote}
913 % In the \packagename{asymptote} package, keys like |width| and
914 % |height| are conveyed both to the implicit |\includegraphics| command
915 % and to Asymptote. In \packagename{asypictureB}, they are conveyed only
916 % to the |\includegraphics| command; font size and even resolution (for
917 % rasterized images) will be
918 % changed in the process of scaling the picture.
919 % Thus, the preferred method is to set the width, height, etc.\
920 % through Asymptote's |size| command.
921 %
922 % \strut
923 %
924 % \noindent
925 % \begin{minipage}{\textwidth}
926 % \gdef\asywidth{3.5cm}
927 % Bad (font size distorted):
928 %
929 % \medskip
930 % \begin{minipage}{\asywidth}
931 % \begin{asypicture}[gobble=2]{name=bad_width,
932 % width=\asywidth}
933 % settings.outformat="pdf";
934 % draw(unitcircle);
935 % label("a label", position=(0,0), align=NE);
936 % \end{asypicture}
937 % \end{minipage}
938 % \hfill
939 % \begin{minipage}{\dimexpr\linewidth-\asywidth-10pt\relax}
940 % \begin{Verbatim}[gobble=2,codes={\catcode`@=0}]
941 % \begin{asypicture}{name=bad_width,
942 % width=@asywidth}
943 % settings.outformat="pdf";
944 % draw(unitcircle);
945 % label("a label", position=(0,0), align=NE);
946 % \end{asypicture}
947 % \end{Verbatim}
948 % \end{minipage}
949 % \end{minipage}
950 %
951 % \bigskip
952 % \noindent
953 % \begin{minipage}{\textwidth}
954 % Good:
955 %
956 % \medskip
957 % \begin{minipage}{\asywidth}
958 % \begin{asypicture}[gobble=2]{name=good_width, width=\asywidth}
959 % size(@asywidth,0);
960 % settings.outformat="pdf";
961 % draw(unitcircle);
962 % label("a label", position=(0,0), align=NE);
963 % \end{asypicture}
964 % \end{minipage}
965 % \hfill
966 % \begin{minipage}{\dimexpr\linewidth-\asywidth-10pt\relax}
967 % \begin{Verbatim}[gobble=2,codes={\catcode`@=0}]
968 % \begin{asypicture}{name=good_width, width=@asywidth}
969 % size(@asywidth,0);
970 % settings.outformat="pdf";
971 % draw(unitcircle);
972 % label("a label", position=(0,0), align=NE);
973 % \end{asypicture}
974 % \end{Verbatim}
975 % \end{minipage}
976 % \end{minipage}
977 %
978 % \bigskip\noindent
979 % Note that in the ``good'' example, the key |width=3.5cm| is necessary
980 % only if you want to make sure the included image has \emph{precisely}
981 % the specified
982 % width, and probably not even then.
983 %
984 % \paragraph{No spaces in file names} The \packagename{asymptote} package
985 % takes measures to accomodate peculiar file names---in particular, file
986 % names that include spaces. The \packagename{asypictureB} package does not.
987 %
988 % \paragraph{No directory specification} The \packagename{asymptote} package
989 % allows the user to specify a directory in which to store the Asymptote
990 % code and images. The \packagename{asypictureB} package does not currently
991 % have this feature, although there is a patch by user202729 that will hopefully
992 % be included in the next version. Note that neither package respects the
993 % |-output-directory| commandline option.
994 %
995 % \paragraph{Suboptimal interaction with \texttt{latexmk}} By design, the
996 % \packagename{asypictureB} package allows the image format to be specified by
997 % the Asymptote code; it does not care whether a |png| or |pdf| file is produced.
998 % Unfortunately, |latexmk| relies on being able to identify the generated image file
999 % from the \TeX\ log file. Thus, if you use \packagename{asypictureB} with |latexmk|,
1000 % it will rerun Asymptote every time for |asypicture|s that produce a file format other
1001 % than the one expected by |latexmk|. Since \packagename{asypictureB} and |latexmk|
1002 % are two different solutions to the same problem, I expect most users will choose
1003 % one or the other. But for those who want to use them together, hopefully there will
1004 % be a better solution in the future.
1005 %
1006 % \section{Acknowledgements}
1007 %
1008 % The current \packagename{asypictureB} package incorporates bug fixes and improvements
1009 % by \href{https://github.com/user202729}{user202729}
1010 % (\url{https://tex.stackexchange.com/users/250119})
1011 % and \href{https://github.com/cfr42}{cfr}
1012 % (\url{https://tex.stackexchange.com/users/39222}). For more details,
1013 % see the \href{https://github.com/charlesstaats/asypictureB/commits/main/}{commit history on github}.
1014 %
1015 % \StopEventually{^^A
1016 % \PrintChanges
1017 % \PrintIndex
1018 % }
1019 %
1020 % \section{Implementation}
1021 %\iffalse
1022 %<*package>
1023 %\fi
1024 % \begin{macrocode}
1025 \NeedsTeXFormat{LaTeX2e}
1026 \ProvidesPackage{asypictureB}
1027 [2025/01/26 v0.4 user-friendly integration of Asymptote into LaTeX]
1028
1029 \RequirePackage{fancyvrb}
1030 \RequirePackage{graphicx}
1031 \RequirePackage{pgfkeys}
1032
1033 \makeatletter
1034
1035 \def\asy@OutFile{\FV@OutFile}
1036
1037 \RequirePackage{ifplatform}
1038
1039 % \end{macrocode}
1040 % Define the |\shell@execute| command. If shell escape is enabled, the
1041 % command executes its contents in the shell. Otherwise, the command
1042 % is written to a script for the user to execute.
1043 % \begin{macrocode}
1044 \ifshellescape
1045 \def\ASYPIC@shell{18}
1046 \newcommand{\shell@execute}{\immediate\write\ASYPIC@shell}
1047 \else
1048 \newwrite\ASYPIC@shell
1049 \ifwindows
1050 \openout\ASYPIC@shell=\jobname-compile_asy.txt\relax
1051 \else
1052 \openout\ASYPIC@shell=\jobname-compile_asy.sh\relax
1053 \fi
1054 \newcommand{\shell@execute}[1]{{%
1055 \edef\temp{#1}%
1056 \write\expandafter\ASYPIC@shell\expandafter{\temp}%
1057 }}
1058 \fi
1059
1060 % \end{macrocode}
1061 % Define the |\copyfile| command, which should work regardless of whether
1062 % shell escape is enabled. (If shell escape is enabled, it is more efficient.)
1063 % \begin{macrocode}
1064 \ifshellescape
1065 \newcommand{\copyfile}[2]{%
1066 \ifwindows%
1067 \immediate\write18{copy #1 #2 /y}%
1068 \else%
1069 \immediate\write18{cp #1 #2}%
1070 \fi%
1071 }
1072 \else
1073 \RequirePackage{verbatimcopy}
1074 \newcommand{\copyfile}[2]{%
1075 \OldVerbatimCopy{#1}{#2}%
1076 }
1077 \fi
1078
1079 % \end{macrocode}
1080 % Define the |\deletefile| command. Unlike |\copyfile|, this command will
1081 % have no immediate effect if shell escape is not enabled, instead writing
1082 % a line to the script. (If shell escape \emph{is} enabled, of course the
1083 % command will immediately delete the file.)
1084 % \begin{macrocode}
1085 \newcommand{\deletefile}[1]{%
1086 \ifwindows%
1087 \shell@execute{del #1}%
1088 \else%
1089 \shell@execute{rm #1}%
1090 \fi%
1091 }
1092
1093 % \end{macrocode}
1094 %
1095 % Set up the key-value system for \packagename{asypictureB} using \packagename{pgfkeys}.
1096 % \begin{macrocode}
1097 \newcommand{\asyset}[1]{\pgfqkeys{/asy}{#1}}
1098 \newcommand{\@asyerrorfilename}{\@asypicturename_errors.txt}
1099 \newcounter{@asy@linenumber}
1100
1101 \asyset{name/.initial=noname, name/.value required}%
1102
1103 \asyset{graphic options/.code={}}
1104 \asyset{set graphic option/.style={graphic options/.append code=#1}}
1105 % \end{macrocode}
1106 % Unrecognized keys should be passed to the |\includegraphics| command
1107 % using |\setkeys|.
1108 % \begin{macrocode}
1109 \asyset{.unknown/.code = %
1110 {%
1111 \edef\unknownkey{\pgfkeyscurrentname}%
1112 \asyset{set graphic option/.expand once = {%
1113 \expandafter\setkeys\expandafter{%
1114 \expandafter G\expandafter i\expandafter n\expandafter%
1115 }\expandafter{\unknownkey=#1}%
1116 }}%
1117 }%
1118 }
1119 % \end{macrocode}
1120 % However, scale and angle must be dealt with separately.
1121 %
1122 % Important note: using this implementation, scale and angle will always be the next-to-last, respectively
1123 % the last, keys executed, no matter in what order the keys are given. Thus, it is impossible to rotate an
1124 % asypicture and then set the width or height using these keys. However, this functionality can be provided
1125 % within the Asymptote code.
1126 % \begin{macrocode}
1127 \def\asy@scale{1}
1128 \asyset{scale/.style={set graphic option = {\def\asy@scale{#1}}},
1129 scale/.value required}
1130 \newcommand{\asy@angle}{0}
1131 \asyset{angle/.style = {set graphic option = {\def\asy@angle{#1}}},
1132 angle/.value required}
1133
1134 \newcommand{\getfontsize}{\f@size}
1135
1136 \newif\ifasyfilechanged
1137 \newif\ifASYPIC@flush
1138 \newif\if@asyrepeat
1139 \newread\@asyreadold
1140 \newread\@asyreadnew
1141 \edef\@tempasyfile{\jobname-temp}
1142
1143 \newcommand{\RequireAsyRecompile}{\ASYPIC@flushtrue}
1144 \newcommand{\AsyCompileIfNecessary}{\ASYPIC@flushfalse}
1145 \AsyCompileIfNecessary
1146
1147 \newcommand\clearasyheader{\def\ASYPIC@header{}}
1148
1149 \def\ASYPIC@header{}
1150
1151 \def\asyheader{\FV@Environment{}{asyheader}}
1152
1153 \def\FVB@asyheader{%
1154 \@bsphack
1155 \begingroup
1156 \FV@UseKeyValues
1157 \FV@DefineWhiteSpace
1158 \def\FV@Space{\space}%
1159 \FV@DefineTabOut
1160 \def\FV@ProcessLine##1{\g@addto@macro\ASYPIC@header{##1^^J}}%
1161 \let\FV@FontScanPrep\relax
1162 %% DG/SR modification begin - May. 18, 1998
1163 %% (to avoid problems with ligatures)
1164 \let\@noligs\relax
1165 %% DG/SR modification end
1166 \FV@Scan}%
1167
1168 \def\FVE@asyheader{\endgroup\@esphack}
1169
1170 \DefineVerbatimEnvironment{asyheader}{asyheader}%
1171 {codes={\catcode`@=0},tabsize=4}
1172 % \end{macrocode}
1173 % Set up the default |asyheader|:
1174 % \begin{macrocode}
1175 \begin{asyheader}
1176 defaultpen(fontsize(@getfontsize pt));
1177 settings.prc = false;
1178 \end{asyheader}
1179 % \end{macrocode}
1180 % Now, define the |asypicture| environment using
1181 % \packagename{fancyvrb} internals:
1182 % \begin{macrocode}
1183 \def\asypicture{\FV@Environment{}{asypicture}}
1184
1185 % \end{macrocode}
1186 %
1187 % Define the |\ASYPIC%recordname| command. As an example, |\ASYPIC@recordname@foo|
1188 % in the file |bar.tex| would set |\asylistingfile| to |bar-foo.asy| the first
1189 % time it is called. A second call to |\ASYPIC@recordname{foo}| would set
1190 % |\asylistingfile| to |bar-foo__1.asy|, a third to |bar-foo__2.asy|, and so on.
1191 % The next number to use is stored in the macro |\ASYPIC@name@foo|. (Note that this
1192 % is a macro, not a counter.)
1193 % \begin{macrocode}
1194 \newcommand{\ASYPIC@recordname}[1]{%
1195 \edef\tempmacroname{ASYPIC@name@#1}%
1196 \ifcsname\tempmacroname\endcsname%% if \ASYPIC@name@... is defined
1197 \edef\oldnum{\csname\tempmacroname\endcsname}%
1198 \edef\@asypicturename%
1199 {\jobname-#1__\oldnum}%
1200 \expandafter\xdef\csname\tempmacroname\endcsname%
1201 {\the\numexpr\oldnum+1\relax}%
1202 \edef\ASYPIC@current@num{\csname\tempmacroname\endcsname}%
1203 \else%% This is the first time this name is being used.
1204 \edef\@asypicturename%
1205 {\jobname-#1}%
1206 \expandafter\gdef\csname\tempmacroname\endcsname{1}%
1207 \fi%
1208 \xdef\asylistingfile{\@asypicturename.asy}
1209 }
1210
1211 % \end{macrocode}
1212 % Most of
1213 % the following definition is copied verbatim from the
1214 % definition of the |VerbatimOut| environment in
1215 % |fancyvrb.dtx|. I don't actually understand what a lot of
1216 % it does.
1217 % \begin{macrocode}
1218
1219 \def\FVB@asypicture#1{%
1220 \@bsphack
1221 \asyset{graphic options/.code={}}%
1222 \asyset{#1, name/.get = \currentname}%
1223 \ASYPIC@recordname{\currentname}%
1224 \begingroup
1225 \FV@UseKeyValues
1226 \FV@DefineWhiteSpace
1227 \def\FV@Space{\space}%
1228 \FV@DefineTabOut
1229 \def\FV@ProcessLine{\immediate\write\asy@OutFile}%
1230 \immediate\openout\asy@OutFile\@tempasyfile.asy\relax
1231 \immediate\write\asy@OutFile{\ASYPIC@header^^J}
1232 \let\FV@FontScanPrep\relax
1233 %% DG/SR modification begin - May. 18, 1998
1234 %% (to avoid problems with ligatures)
1235 \let\@noligs\relax
1236 %% DG/SR modification end
1237 \FV@Scan}
1238
1239 \newcommand{\ASYPICcomparefiles}[2]{
1240 \IfFileExists{\@asypicturename.asy}%
1241 {\openin\@asyreadold=#1.asy\relax%
1242 \openin\@asyreadnew=#2.asy\relax%
1243 \asyfilechangedfalse%
1244 \@asyrepeattrue%
1245 \loop%
1246 \ifeof\@asyreadold%
1247 \@asyrepeatfalse%
1248 \ifeof\@asyreadnew%
1249 \else%
1250 \asyfilechangedtrue%
1251 \fi%
1252 \else%
1253 \ifeof\@asyreadnew%
1254 \@asyrepeatfalse%
1255 \asyfilechangedtrue%
1256 \else% Not at the end of either file in this case
1257 \readline\@asyreadold to \oldfileline%
1258 \readline\@asyreadnew to \newfileline%
1259 \ifx\oldfileline\newfileline%
1260 \@asyrepeattrue%
1261 \else
1262 \asyfilechangedtrue%
1263 \@asyrepeatfalse%
1264 \fi%
1265 \fi%
1266 \fi%
1267 \if@asyrepeat
1268 \repeat%
1269 \closein\@asyreadold%
1270 \closein\@asyreadnew%
1271 }%
1272 {\asyfilechangedtrue}%
1273 \IfFileExists{#1.pdf}{}{%
1274 \IfFileExists{#1.png}{}{%
1275 \IfFileExists{#1.eps}{}{%
1276 \asyfilechangedtrue%
1277 }%
1278 }%
1279 }%
1280 }
1281
1282 \def\ASYPIC@runasy{%
1283 \message{Attempting to run asy on \@asypicturename.asy^^J}%
1284 \shell@execute{asy -noV \@asypicturename.asy 2> \@asyerrorfilename}%
1285 \openin\@asyreadold=\@asyerrorfilename\relax%
1286 \ifeof\@asyreadold%
1287 \closein\@asyreadold% Error log does not exist.
1288 \else%
1289 \def\@asyerrormessage{^^J}%
1290 %Create command to process line of log file to figure
1291 %out the Asymptote file line number:
1292 \edef\@asy@temp{%
1293 \def\noexpand\@asy@processerrorline####1\detokenize\expandafter{%
1294 \@asypicturename.asy:%
1295 }%
1296 }%
1297 \@asy@temp##2.##3:\relax{%
1298 \xdef\@asy@errorlinenumber{\numexpr##2\relax}}%
1299 %
1300 \gdef\@asy@errorlinenumber{-5}%
1301 {%
1302 \endlinechar=-1%
1303 \loop\unless\ifeof\@asyreadold%
1304 \readline\@asyreadold to \@asyerrorcurrentline%
1305 \ifnum\@asy@errorlinenumber=-5%
1306 {\expandafter\expandafter\expandafter%
1307 \@asy@processerrorline\expandafter%
1308 % Suffix provides default of -5:
1309 \@asyerrorcurrentline\detokenize\expandafter%
1310 {\@asypicturename.asy:} -5.1:\relax%
1311 }%
1312 \fi%
1313 \expandafter\g@addto@macro\expandafter\@asyerrormessage%
1314 \expandafter{\@asyerrorcurrentline^^J}%
1315 \repeat%
1316 }%
1317 \closein\@asyreadold%
1318 \ifnum\@asy@errorlinenumber=-5% No error
1319 \else%
1320 \openin\@asyreadold\@asypicturename.asy\relax%
1321 \edef\numlinesout{5}
1322 \setcounter{@asy@linenumber}{\numlinesout}
1323 \loop\ifnum\value{@asy@linenumber}<\@asy@errorlinenumber%
1324 \readline\@asyreadold to \temp%
1325 \stepcounter{@asy@linenumber}%
1326 \repeat%
1327 %
1328 \addtocounter{@asy@linenumber}{-\numlinesout}%
1329 %
1330 {%
1331 \endlinechar=`^^J%
1332 \loop\ifnum\value{@asy@linenumber}<\@asy@errorlinenumber%
1333 \stepcounter{@asy@linenumber}%
1334 \edef\temp/{\arabic{@asy@linenumber}}%
1335 \expandafter\g@addto@macro\expandafter%
1336 \@asyerrormessage\expandafter{\temp/ }%
1337 \readline\@asyreadold to \@asytempmessage%
1338 \expandafter\g@addto@macro\expandafter%
1339 \@asyerrormessage\expandafter{\@asytempmessage}%
1340 \repeat%
1341 }%
1342 \closein\@asyreadold%
1343 \PackageError{asypictureB}{\@asyerrormessage}{%
1344 The Asymptote run described above
1345 gave a non-empty error log. I have^^J%
1346 reproduced the error log and attempted
1347 to print the five lines leading^^J%
1348 up to the error. Press enter or return
1349 to continue, and then fix your^^J%
1350 Asymptote code when this run is done.
1351 }%
1352 \fi%
1353 \fi%
1354 }
1355
1356 \def\FVE@asypicture{\immediate\closeout\asy@OutFile\endgroup%
1357 \@esphack%
1358 \ifASYPIC@flush%
1359 \asyfilechangedtrue%
1360 \else%
1361 \ASYPICcomparefiles{\@asypicturename}{\@tempasyfile}%
1362 \fi%
1363 \ifasyfilechanged%
1364 \IfFileExists{\@asypicturename.png}%
1365 {\deletefile{\@asypicturename.png}}{}%
1366 \IfFileExists{\@asypicturename.pdf}%
1367 {\deletefile{\@asypicturename.pdf}}{}%
1368 \IfFileExists{\@asypicturename.eps}%
1369 {\deletefile{\@asypicturename.eps}}{}%
1370 \copyfile{\@tempasyfile.asy}{\@asypicturename.asy}%
1371 \ASYPIC@runasy%
1372 \fi%
1373 \asyset{graphic options}%
1374 % Avoid giving "file does not exist" errors.
1375 \chardef\previousinteractionmode=\interactionmode%
1376 \batchmode%
1377 \includegraphics[scale=\asy@scale,angle=\asy@angle]%
1378 {\@asypicturename}%
1379 \interactionmode=\previousinteractionmode%
1380 }
1381
1382 \DefineVerbatimEnvironment{asypicture}{asypicture}%
1383 {codes={\catcode`@=0},tabsize=4}
1384
1385 %\AtEndDocument{\deletefile{\@tempasyfile.asy}}
1386 \ifshellescape\else
1387 \AtEndDocument{\closeout\ASYPIC@shell}
1388 \fi
1389
1390
1391 \makeatother
1392 % \end{macrocode}
1393 %\iffalse
1394 %</package>
1395 %\fi
1396 % \Finale