Debian Codegraph @

texmf-dist/source/latex/asyfig/asyfig.tex

raw
1 \RequirePackage{filecontents}
2 \begin{filecontents*}{README.txt}
3 __________________
4 The ASYFIG package
5 v0.1c
6
7 This package provides an alternative to the asymptote package for including Asymptote graphics in a LaTeX document. In this package, all Asymptote figures are defined separately from the source in their own individual ".asy" files. This package uses Asymptote's inline mode so that labels in the graphics are produced by the main typesetting run; this ensures consistent font and size selection of text within the graphics. In addition, each individual ".asy" graphic can be very quickly processed individually to facilitate easy maintenance and editing of the graphics.
8
9 ______________
10 CHANGE HISTORY
11
12 v0.1c
13 - Add compilation flags to generate 2D vector output
14 in the case of 3D graphics
15
16 v0.1b
17 - Now supports Asymptote v1.78 (an internal macro was
18 changed that needed to be mirrored in this package)
19
20 v0.1a
21 - Initial public release after Asymptote was added to
22 CTAN and TeX Live
23
24 ____________
25 INSTALLATION
26
27 Run `latex` on asyfig.tex to produce the files
28 asyfig.ins, asyfig.sty, asyalign.sty, asyprocess.sty, and README.txt,
29 as well as to compile the PDF documentation.
30
31 Execute `tex pstool.ins` to produce the files above
32 except pstool.ins (and the PDF file, obviously).
33
34 ___________
35 MAINTENANCE
36
37 Please report bugs or request features:
38 <http://github.com/wspr/asyfig/issues>
39
40 Developmental and historical versions:
41 <http://github.com/wspr/asyfig>
42
43 Current release version:
44 <http://ctan.tug.org/pkg/asyfig>
45
46 ___________
47 LICENCE
48
49 The asyfig collection of packages are released under the LaTeX Project Public License. More information can be found within the PDF documentation.
50
51 ______________
52 Will Robertson
53 Copyright 2008-2009
54 \end{filecontents*}
55 %%%%%%%%%1%%%%%%%%%2%%%%%%%%%3%%%%%%%%%4%%%%%%%%%5
56
57 \begin{filecontents}{asyfig.sty}
58 \ProvidesPackage{asyfig}[2010/03/20 v0.1c
59 Commands for using asymptote figures]
60
61 % This package is the main user interface for inserting external |asy| figures
62 % into the document.
63
64 \RequirePackage{%
65 asyalign,color,ifmtarg,ifpdf,ifplatform,import,
66 graphicx,pdftexcmds,suffix,xkeyval}
67
68 % Better conditionals than |\newif| provides:
69 \def\@True{11}
70 \def\@False{01}
71 \def\asy@If#1{\if#1\relax\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}
72
73 \let\asy@always\@False
74 \let\asy@never\@False
75 \let\asy@process\@False
76
77 % Package options:
78 \define@choicekey*{asyfig.sty}{process}[\@tempa\@tempb]{all,none,auto}{%
79 \ifcase\@tempb\relax
80 \let\asy@always\@True
81 \or
82 \let\asy@never\@True
83 \or
84 \fi
85 }
86 \ExecuteOptions{process=auto}
87
88 \ProcessOptionsX
89
90 % \subsection{Auxiliary macros}
91
92 \def\asy@splitpath#1/#2/{%
93 % Recursive macro that is used like\\
94 % \qquad |\asy@splitpath abc/def/ghi.asy/\@nil/|\\
95 % It defines |\asy@filename| $\to$ |ghi.asy| and |\asy@path| $\to$ |abc/def/|
96 \ifx\@nil#2\relax
97 % If input is \<anything>|/\@nil/| then we've reached the end:
98 \def\asy@filename{#1}%
99 \else
100 % Otherwise we're in the middle of the slash-separated list;
101 % build up |\asy@path|, and iterate:
102 \edef\asy@path{\asy@path#1/}%
103 \def\@tempa{\asy@splitpath#2/}%
104 \expandafter\@tempa
105 \fi
106 }
107
108 \newcommand\asypath[1]{\def\asy@pathprefix{#1}}
109 \asypath{}
110
111 \def\asy@asyfile{\asy@pathprefix\asy@path\[email protected]}
112 \def\asy@texfile{\asy@pathprefix\asy@path\asy@filename\string_.tex}
113
114 \def\asy@cmdsep{\ifwindows \string& \else; \fi}
115
116 % \subsection{The main macro}
117
118 \newcommand\asyfig[1]{%
119 \let\asy@path\@empty
120 \asy@splitpath #1/\@nil/%
121 \IfFileExists{\asy@asyfile}{%
122 \asy@If\asy@process{}{%
123 \asy@If\asy@always{%
124 \let\asy@process\@True
125 }{%
126 \IfFileExists{\asy@texfile}{%
127 \asy@If\asy@never{}{%
128 % compare file dates to see if we want to reprocess:
129 \ifnum\pdf@strcmp{\pdf@filemoddate{\asy@texfile}}
130 {\pdf@filemoddate{\asy@asyfile}} < \z@
131 \let\asy@process\@True
132 \fi
133 }%
134 }{% if the .tex file doesn't exist, either
135 % give an error or process the .asy file:
136 \asy@If\asy@never{%
137 \PackageError{asyfig}{%
138 ^^J\space\space\space\space
139 "\asy@pathprefix\asy@path\[email protected]" requires processing%
140 }{%
141 The generated file that is required to insert the asy graphic,
142 ^^J\space\space\space\space
143 "\asy@pathprefix\asy@path\asy@filename\string_.tex"^^J%
144 does not exist.
145 Please process the asy figure manually or de-activate the^^J%
146 [process=none] package option.
147 }%
148 }{%
149 \let\asy@process\@True
150 }
151 }%
152 }}%
153 \asy@If\asy@process{%
154 \edef\@tempa{\asy@pathprefix\asy@path}%
155 \pdf@system{%
156 echo "^^J====== ASY PROCESS =====^^J"
157 \asy@cmdsep
158 \ifx\@tempa\@empty\else
159 cd \@tempa
160 \asy@cmdsep
161 \fi
162 \ifpdf pdf\fi latex
163 -shell-escape
164 -interaction=batchmode
165 -jobname=\asy@filename-comp
166 \unexpanded{%
167 "\RequirePackage{asyprocess}\ProcessAsy
168 \documentclass{article}
169 \begin{document}\ShowAsy
170 \end{document}"
171 }%
172 \asy@cmdsep
173 echo "^^J==== ASY END PROCESS ===^^J"
174 }%
175 }{}%
176 \import{\asy@pathprefix\asy@path}{\asy@filename\string_.tex}%
177 }{%
178 \PackageWarning{asyfig}{%
179 ^^J\space\space
180 "\asy@pathprefix\asy@path\[email protected]" not found.^^J%
181 This warning occurred%
182 }%
183 }%
184 \let\asy@process\@False
185 }
186
187 % The starred version of \cmd\asyfig\ processes the graphic always:
188 \WithSuffix\newcommand\asyfig*[1]{%
189 \begingroup
190 \let\asy@process\@True
191 \csname\NoSuffixName\asyfig\endcsname{#1}%
192 \endgroup
193 }
194
195 \end{filecontents}
196 %%%%%%%%%1%%%%%%%%%2%%%%%%%%%3%%%%%%%%%4%%%%%%%%%5
197
198
199
200
201 \begin{filecontents}{asyalign.sty}
202 \ProvidesPackage{asyalign}
203
204 % This package provides code for placing Asymptote labels inline in \LaTeX\ documents. It is adapted from code that is usually included within Aymptote's \<filename>|_.pre| file, which provides a \LaTeX\ preamble for |asy| processing; this preamble is skipped with the \pkg{asyfig} package since all figures inherit the preamble from that of the main document.
205
206 \RequirePackage{ifpdf}
207
208 \newbox\ASYbox
209 \newdimen\ASYdimen
210
211 \long\def\ASYbase#1#2{%
212 \leavevmode
213 \setbox\ASYbox\hbox{#1}%
214 \ASYdimen=\ht\ASYbox
215 \setbox\ASYbox\hbox{#2}%
216 \lower\ASYdimen\box\ASYbox
217 }
218
219 \ifpdf
220
221 \long\def\ASYaligned(#1,#2)(#3,#4)#5#6#7{%
222 \leavevmode
223 \setbox\ASYbox\hbox{#7}%
224 \setbox\ASYbox\hbox{%
225 \ASYdimen\ht\ASYbox
226 \advance\ASYdimen\dp\ASYbox
227 \kern#3\wd\ASYbox
228 \raise#4\ASYdimen
229 \box\ASYbox
230 }%
231 \put(#1,#2){%
232 #5\wd\ASYbox 0pt\dp\ASYbox 0pt\ht\ASYbox 0pt\box\ASYbox#6%
233 }%
234 }
235
236 \long\def\ASYalignT(#1,#2)(#3,#4)#5#6{%
237 \ASYaligned(#1,#2)(#3,#4){%
238 \special{pdf:q #5 0 0 cm}%
239 }{%
240 \special{pdf:Q}%
241 }{#6}%
242 }
243
244 \long\def\ASYalign(#1,#2)(#3,#4)#5{\ASYaligned(#1,#2)(#3,#4){}{}{#5}}
245
246 \let\ASYraw\@firstofone
247
248 \else
249
250 \long\def\ASYaligned(#1,#2)(#3,#4)#5#6#7{%
251 \leavevmode
252 \setbox\ASYbox\hbox{#7}%
253 \setbox\ASYbox\hbox{%
254 \ASYdimen\ht\ASYbox%
255 \advance\ASYdimen\dp\ASYbox
256 \kern#3\wd\ASYbox
257 \raise#4\ASYdimen
258 \box\ASYbox
259 }%
260 \put(#1,#2){#5\wd\ASYbox 0pt\dp\ASYbox 0pt\ht\ASYbox 0pt\box\ASYbox#6}%
261 }
262
263 \long\def\ASYalignT(#1,#2)(#3,#4)#5#6{%
264 \ASYaligned(#1,#2)(#3,#4){%
265 \special{%
266 ps:gsave currentpoint currentpoint translate
267 [#5 0 0] concat neg exch neg exch translate%
268 }%
269 }{%
270 \special{ps:currentpoint grestore moveto}%
271 }{#6}%
272 }
273
274 \long\def\ASYalign(#1,#2)(#3,#4)#5{\ASYaligned(#1,#2)(#3,#4){}{}{#5}}
275
276 \def\ASYraw#1{%
277 currentpoint currentpoint translate matrix currentmatrix
278 100 12 div -100 12 div scale
279 #1
280 setmatrix neg exch neg exch translate%
281 }
282
283 \fi
284
285 \end{filecontents}
286 %%%%%%%%%1%%%%%%%%%2%%%%%%%%%3%%%%%%%%%4%%%%%%%%%5
287
288 \begin{filecontents}{asyprocess.sty}
289 \ProvidesPackage{asyprocess}
290 \nofiles
291
292 \RequirePackage{ifmtarg,ifpdf,catchfile,ifplatform,color,graphicx}
293 \RequirePackage[active,tightpage]{preview}
294
295 \def\@par@macro{\par}
296
297 \def\asy@status{asyprocess-statusfile.txt}
298
299 \edef\@tempa{\detokenize{-comp}}
300 \@temptokena{\def\asy@strip@comp#1}
301 \expandafter\the\expandafter\@temptokena\@tempa#2\@nil{%
302 \@ifmtarg{#2}{%
303 \errorstopmode
304 \PackageError{asyprocess}{%
305 The \string\jobname\space of this compilation must end with `-comp'%
306 }{%
307 You must set the \cmd\jobname\ with the equivalent of^^J\space\space
308 pdflatex -jobname=XYZ-comp ...%
309 }
310 }{}%
311 \edef\asy@compname{#1}}
312 \expandafter\expandafter\expandafter
313 \asy@strip@comp\expandafter\jobname\@tempa\@nil
314
315 \newcommand\ProcessAsy{%
316 \immediate\write18{%
317 asy -wait -inlinetex -noprc -render 0 -tex \ifpdf pdf\fi latex
318 \asy@compname\space 2> \asy@status}%
319 \CatchFileDef{\@tempb}{\asy@status}{}%
320 \immediate\write18{\ifwindows del \else rm \fi \asy@status}
321 \ifx\@tempb\@par@macro
322 \expandafter\@gobble
323 \else
324 \g@addto@macro\@tempb{^^J^^J%
325 ------------ ASY ERROR ------------^^J%
326 -----------------------------------}%
327 \expandafter\@firstofone
328 \fi{%
329 \errorstopmode
330 \typeout{%
331 -----------------------------------^^J%
332 ------------ ASY ERROR ------------^^J}
333 \typeout{\expandafter\strip@prefix\meaning\@tempb}
334 \batchmode
335 \end{document}}}
336
337 \newcommand\ShowAsy{%
338 \begin{preview}
339 \input{\asy@compname_}
340 \end{preview}}
341
342 \AtBeginDocument{\InputIfFileExists{\asy@compname_.pre}{}{}}
343
344 \end{filecontents}
345
346
347 \begin{filecontents*}{test-asyfig.tex}
348
349 %% uncomment this to enable non-PDF mode:
350 %\pdfoutput=0
351 %% (rememer to delete asyfig_.tex if it's previously been generated for PDF mode)
352
353 \documentclass[12pt,twocolumn]{article}
354 \usepackage[sc]{mathpazo}
355 \usepackage{asyfig}
356 \begin{document}
357 A boxed graphic:\\
358 \fbox{\sffamily\asyfig{frf}}\par
359 Note the font in the graphic follows
360 the current font of the document.
361 \end{document}
362
363 \end{filecontents*}
364
365 \begin{filecontents*}{frf.asy}
366 unitsize(10mm);
367 draw( (0,0){right}..{up}(3,2){down}..
368 {down}(4,-2){up}..{right}(7,0) );
369 draw( "Resonance" , align=E, (3,2) );
370 draw( "Anti-resonance" , align=W, (4,-2) );
371 \end{filecontents*}
372
373
374
375
376 % Conditionally compile the documentation & generate the .ins file:
377 \providecommand\asyfigCompile{Y}
378 \makeatletter
379 \if\asyfigCompile N
380 \expandafter\@@end
381 \fi
382
383
384
385
386 \begin{filecontents*}{asyfig.ins}
387 %&latex
388 \def\asyfigCompile{N}
389 \input asyfig.tex
390 \csname@@end\endcsname
391 \end{filecontents*}
392
393
394
395
396 \makeatletter
397 \documentclass{article}
398
399 \usepackage[it,medium]{titlesec}
400
401 \usepackage{bigfoot,xcolor}
402 \usepackage[colorlinks,linktocpage]{hyperref}
403
404 \usepackage{gmdoc}
405 \usepackage{gmverb}
406 \dekclubs
407 \stanzaskip=\bigskipamount
408 \CodeSpacesGrey
409
410 \usepackage{tocloft,varwidth}
411 \setcounter{tocdepth}{1}
412 \def\tocwidthA{0.45}
413 \def\tocwidthB{0.45}
414 \def\cftpartfont{\scshape}
415 \def\cftsecfont{\small}
416 \cftbeforesecskip=0pt
417 \def\cftpartleader{}
418 \def\cftpartafterpnum{\cftparfillskip}
419 \def\cftsecleader{}
420 \def\cftsecafterpnum{\cftparfillskip}
421
422 \let\pkg\textsf
423 \def\pkgopt#1{\texttt{[#1]}}
424
425 \def\PDF{\textsc{pdf}}
426 \def\PS{\textsc{ps}}
427 \def\DVI{\textsc{dvi}}
428 \def\EPS{\textsc{eps}}
429
430 \usepackage{amsmath,listings}
431 \lstset{basicstyle=\ttfamily,columns=fullflexible}
432
433 \usepackage{asyfig}
434 \usepackage[T1]{fontenc}
435 \usepackage{microtype}
436 \usepackage{lmodern}
437 \usepackage[sc,osf]{mathpazo}
438 \linespread{1.1}
439 \frenchspacing
440
441 \GetFileInfo{asyfig.sty}
442 \begin{document}
443 {\addtocontents{toc}{\protect\begin{varwidth}[t]{\tocwidthA\linewidth}}}
444
445 \title{The \pkg{asyfig} packages}
446 \author{%
447 \normalsize Will Robertson\footnote{\texttt{[email protected]}}}
448 \date{\fileversion\qquad\filedate}
449
450 \maketitle
451
452 \begin{abstract}
453 This suite of packages provides an alternate method of including stand-alone Aymptote figures within \LaTeX\ documents via the \cmd\asyfig\ command.
454 \end{abstract}
455
456 \tableofcontents
457
458 \part{User documentation}
459
460 \section{Introduction}
461 Asymptote (or |asy|) is a vector graphics programming language inspired by MetaPost but based around an extended C-like language and full support for 3\textsc{d} bezier curves. Asymptote uses an auxiliary \LaTeX\ process to typeset its labels, and figures can be either generated as stand-alone graphics or in an `inline' form in which labels get placed by the main typesetting process as the figure is inserted into a document.
462
463 Support for |asy| in a \LaTeX\ document is provided by the \pkg{asymptote} package, which defines the |\begin{asy}| environment in which |asy| figures may be directly typed. In this case, the source file contains the complete specification for the text and graphics in the document. However, for large documents it can be quite inconvenient to maintain |asy| graphics that are inline with the document source, because the whole document requires two compilations before any changes in the graphic can be visualised.
464
465 This package, \pkg{asyfig}, provides an alternative, whereby all |asy| figures are defined \emph{separately} from the source in their own individual |.asy| files. \pkg{asyfig} uses Asymptote's inline mode so that labels in the graphics are produced by the main typesetting run; this ensures consistent font and size selection of text within the graphics. In addition, each individual |.asy| graphic can be very quickly processed individually to facilitate easy maintenance and editing of the graphics.
466
467 This package sometimes lags behind the current release of Asymptote simply because I don't use Asymptote very often. The current release of this package is designed to work with Asymptote v1.91 and later.
468
469 \section{Do you need this package?}
470
471 After I wrote and used this package for quite some time, I realised that what it is intended to do can be done with the standard \pkg{asymptote} package. If you have an Aymptote graphic called |myfig.asy|, you can include it in your document as follows:
472 \begin{verbatim}
473 \begin{asy}
474 include "myfig";
475 \end{asy}
476 \end{verbatim}
477 There's actually not much point using this package if this works for you. But I'll keep supporting this package for now while I continue to use it.
478
479 \section{Getting started}
480
481 Load the \pkg{asyfig} package like any other. I'll discuss the workflow of the package with an illustrative example.
482
483 \paragraph{An \texttt{asy} graphic}
484 First we need an example Asymptote graphic. This package is distributed with one such, |frf.asy|:
485 \lstinputlisting{frf.asy}
486 Material within |texpreamble| is \emph{not} used in the final typesetting of the labels; it is purely for the `proof' graphic that is produced before the graphic is integrated within the main document.
487
488 \paragraph{Inserting the graphic}
489 After processing (see the next step), this graphic can be included in the document with the \cmd\asyfig\arg{graphic name} command. In the case of the example, this would be |\asyfig{frf}|. It does \emph{not} take any option arguments like a regular graphic to affect the scaling or rotation of the graphic (cf.~\cmd\includegraphics); you are expected to produce the figure in the correct size and orientation within Asymptote.
490
491 If all of your |.asy| files take a common path prefix (such as |./figures/asy/|), this can be defined with the \cmd\asypath\marg{path} command. For example, instead of writing |\asyfig{asy/frf}| one can write |\asypath{asy/}| somewhere in the document (usually the preamble) and then later |\asyfig{frf}|.
492
493 \paragraph{Processing the graphic}
494 But before the graphic can be placed into the document it must be processed. If the \texttt{asy} graphic has not been processed, or if the \texttt{asy} file is \emph{newer} than its processed graphic, then this package will attempt to do the processing automatically. To turn off this automatic processing, load the package with the \texttt{[process=none]} package option. Alternatively, to re-process \emph{all} \texttt{asy} graphics, use \texttt{[process=all]} instead.
495
496 The primary feature of this package is that figures may be processed independently of the main document in order to be able to rapidly iterate changes to the graphic. This processing is performed by the \pkg{asyprocess} package in an auxiliary \LaTeX\ execution. Here is a basic shell script that I use to do this:
497 \begin{lstlisting}
498 #!/bin/sh
499 pdflatex -shell-escape -interaction=batchmode -jobname=$1-comp
500 "\RequirePackage{asyprocess}\ProcessAsy
501 \documentclass{article}\begin{document}\ShowAsy\end{document}"
502 \end{lstlisting}
503 Simply change |pdflatex| to |latex| to have \EPS\ graphics produced by Asymptote. Note that it is \emph{mandatory} to use the |-comp| suffix for the jobname.
504
505 By saving the script above into (say) |asyprocess| and making it executable, an individual \texttt{asy} graphic can be processed by running (following from the running example) `|asyprocess frf|'.
506
507 \section{Package information}
508 The most recent publicly released version of \pkg{asyfig} is available from \textsc{ctan}:\\
509 \centerline{\url{http://tug.ctan.org/pkg/asyfig/}}\\
510 \newpage
511 Historical and developmental versions are available at GitHub:\\ \centerline{\url{http://github.com/wspr/asyfig/}}\\
512 While general feedback at \url{[email protected]} is welcomed, specific bugs should be reported through the issue tracker at GitHub: \url{http://github.com/wspr/asyfig/issues}.
513
514 This package is freely modifiable and distributable under the terms and conditions of the \LaTeX\ Project Public Licence, version 1.3c or greater (your choice). The latest version of this license is available at: \url{http://www.latex-project.org/lppl.txt}. This work is maintained by \textsc{Will Robertson}.
515
516 {\addtocontents{toc}{\protect\end{varwidth}\protect\hfill}}
517 {\addtocontents{toc}{\protect\begin{varwidth}[t]{\protect\tocwidthB\protect\linewidth}}}
518 \clearpage
519 \parindent=0pt
520
521 \part{Implementation}
522 \section{The asyfig package}
523 \DocInput{asyfig.sty}
524
525 \section{The asyalign package}
526 \DocInput{asyalign.sty}
527
528 \section{The asyprocess package}
529 \DocInput{asyprocess.sty}
530
531 {\addtocontents{toc}{\protect\end{varwidth}}}
532 \end{document}