summaryrefslogtreecommitdiff
path: root/graphics/asypictureb/asypictureB.dtx
blob: b86e6a18514b251b1259558c37833ed77ca9c583 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
% \iffalse meta-comment
%<*internal>
\iffalse
%</internal>
%<*readme>
----------------------------------------------------------------
asypictureB -- user-friendly integration of Asymptote into LaTeX
E-mail: cstaats at math.uchicago.edu
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------

The `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 `asymptote` package 
that comes with Asymptote. The most important advantage of the 
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.
%</readme>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
----------------------------------------------------------------
asypictureB --- user-friendly integration of Asymptote into LaTeX
E-mail: cstaats at math.uchicago.edu
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------

\endpreamble
\postamble

Copyright (C) 2014 by Charles Staats III

This file may be distributed and/or modified under the
conditions of the LaTeX Project Public License (LPPL), either
version 1.3c of this license or (at your option) any later 
version. The latest version of this license is in the following file:

     http://www.latex-project.org/lppl.txt

This work is "maintained" (as per LPPL maintenance status) by 
Charles Staats III.

This work consists of the file  asypictureB.dtx
and the derived files           asypictureB.ins,
                                asypictureB.pdf, and
                                asypictureB.sty.

\endpostamble
\usedir{tex/latex/asypictureb}
\generate{
  \file{\jobname.sty}{\from{\jobname.dtx}{package}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/asypictureb}
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\usedir{doc/latex/asypictureb}
\generate{
  \file{README.txt}{\from{\jobname.dtx}{readme}}
}
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>
%<*package>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{asypictureB}
  [2014/04/15 v0.3 user-friendly integration of Asymptote into LaTeX]
%</package> 
%<*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 66\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 
\RecordChanges 
\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: cstaats at math.uchicago.edu}}
% \date{Released \filedate}
%
% \maketitle
%
% \changes{v0.3}{2014/04/15}{First public release}
%
% \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}
% 
% \noindent\textbf{Disclaimer:} The author of this package has no affiliation with 
% the creators of the Asymptote programming language.
% 
% \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.\footnote{A note 
% on why this is desirable: Asymptote's 3d capabilities are 
% currently much better for 
% rasterized images than for vector graphics. Thus, it is often desirable 
% to produce a rasterized image. Because of a quirk of Asymptote, it is 
% easy to produce high-resolution images in PNG format, but much harder 
% to raise the resolution when the output is in the default PDF format.}
% \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.\footnote{This is relevant because a 
% second trick for producing a high-resolution rasterized image with the 
% Asymptote language is to produce a scaled-up PDF image, and then 
% scale it back down when including it into the \TeX\ file.}
% \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, the user is shown the Asymptote error log and a line number 
% that will allow him 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 Mac OS X 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.24/}.
% \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, 
% the \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 three commands at the terminal. 
% 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
% foo-asy_compile.bat
% pdflatex foo
% \end{Verbatim}
% \end{minipage}
% \hfill
% \begin{minipage}{0.48\linewidth}
% Mac OS X and Unix-like systems:
% \begin{Verbatim}[commandchars=\\\{\},gobble=2]
% pdflatex foo
% sh foo-asy_compile.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}-|asy_compile| 
% 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 using the |\RequireAsyRecompile| command.
% \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{picturname} 
% 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);
% \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);
% \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 content. 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. One unfortunate 
% consequence of this procedure is that 
% a warning by Asymptote will be translated into a full-fledged error, 
% which is why the \LaTeX\ error message begins with the words 
% ``Possible Asymptote 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 If the first line of the error log is of the form
% \begin{quote}
% \texttt{\meta{filename}-\meta{picturename}.asy:\meta{number}.\meta{stuff}}
% \end{quote}
% then the \meta{number} is extracted. If the first line is not of this 
% form, then the parsing is considered to have failed. As a consequence, 
% if the first line of the error log is a warning but an error shows up 
% later, \packagename{asypictureB} will not notice the error message.
% In the author's experience, this problem does occur, but not often.
% \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]
% ./asyerrorexample.tex:13: Package asypicture Error: 
% Possible Asymptote error:
% 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 asypicture package documentation for explanation.
% Type  H <return>  for immediate help.
%  ...                                              
%                                                    
% l.13 \end{asypicture}
% \end{Verbatim}
% The first two lines of the \LaTeX\ error message identifies that the 
% |asypicture| environment ending on line 13 may have produced an error.
% The next two lines are the Asymptote error log: they explain that this 
% was, in fact, a syntax error. Looking at the five lines displayed, 
% 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. 
% 
% \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 limitations---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.
% 
% \StopEventually{^^A
%    \PrintChanges
%    \PrintIndex
% }
%
% \section{Implementation}
%\iffalse
%<*package>
%\fi
%    \begin{macrocode}
\RequirePackage{fancyvrb}
\RequirePackage{graphicx}
\RequirePackage{pgfkeys}


\makeatletter

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

\RequirePackage{ifplatform}

\ifshellescape
    \def\ASYPIC@shell{18}
    \newcommand{\shell@execute}{\immediate\write\ASYPIC@shell}
\else
	\newwrite\ASYPIC@shell
	\ifwindows
	    \openout\ASYPIC@shell=\jobname-compile_asy.bat\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

\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

\newcommand{\deletefile}[1]{%
    \ifwindows%
        \shell@execute{del #1}%
    \else%
        \shell@execute{rm #1}%
    \fi%
}


\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}}

\newcommand{\ASYPIC@recordname}[1]{%
    \edef\tempmacroname{ASYPIC@name@#1}%
    \ifcsname\tempmacroname\endcsname%% if \ASYPIC@name@... is defined
        \edef\oldnum{\csname\tempmacroname\endcsname}%
        \edef\@asypicturename%
            {\scantokens\expandafter{\jobname\noexpand}-#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%
            {\scantokens\expandafter{\jobname\noexpand}-#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
                      \read\@asyreadold to \oldfileline%
                      \read\@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%
    \else%
        \def\@asyerrormessage{^^JPossible Asymptote error:^^J}%
        \read\@asyreadold to \@asyerrorfirstline%
        \g@addto@macro\@asyerrormessage{\@asyerrorfirstline^^J}%
        \ifeof\@asyreadold%
            \closein\@asyreadold%
        \else%
            {\endlinechar=`^^J%
            \loop%
                \readline\@asyreadold to \@asytempmessage%
                \expandafter\g@addto@macro\expandafter%
                  \@asyerrormessage\expandafter{\@asytempmessage^^J}%
            \unless\ifeof\@asyreadold\repeat%
            }%
            \closein\@asyreadold%
            \g@addto@macro\@asyerrormessage{^^J}%
            %Next: Need to process \@asyerrorfirstline to figure 
            %out the Asymptote file line number
            \expandafter\def\expandafter\@asy@processerrorline%
              \expandafter##\expandafter1\@asypicturename.asy:%
               ##2.##3\relax{\xdef\@asy@errorline{\numexpr##2\relax}}%
            {\expandafter\expandafter\expandafter%
                \@asy@processerrorline\expandafter%
                \@asyerrorfirstline\@asypicturename.asy: -5.\relax%
            }%  The surrounding braces should prevent too much from 
             %  being gobbled in case the pattern doesn't match.
            \openin\@asyreadold\@asypicturename.asy\relax%
            \edef\numlinesout{5}
            \setcounter{@asy@linenumber}{\numlinesout}
            \loop\ifnum\value{@asy@linenumber}<\@asy@errorline%
                \readline\@asyreadold to \temp%
                \stepcounter{@asy@linenumber}%
            \repeat%
            %
            \addtocounter{@asy@linenumber}{-\numlinesout}
            %
            {%
                \endlinechar=`^^J%
                \loop\ifnum\value{@asy@linenumber}<\@asy@errorline%
                    \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