5

I have drawn a picture using the asymptote package (\usepackage{asymptote}), and the environment \begin{asy}...\end{asy}. I want to recycle the code in the {asy} environment without have to copy-paste it.

  • Is it possible to save result of drawing inside the {asy} block to insert it at a different location in the same document?
  • Is it possible to save chunks of code asy-code, i.e. subpictures, to re-use them somewhere else?

For example, I might draw a green disk, and somewhere else a red disk. At different points in my document I might want to insert other, exactly the same looking green and red disks without copy-pasting the code. At some point I might even want to insert a picture that contains both the green and the red disk combined/overlaid in some way to give one picture.

Thanks a lot!

EDIT: Re-written the discription in a (hopefully) clearer way.

2
  • 1
    \savebox is the fastest, most efficient way to produce multiple copies of pretty much anything. One can use tikz to arrayge other images. See tex.stackexchange.com/questions/150367/… for example. Jun 2, 2015 at 13:01
  • I just edited my question
    – Stan
    Jun 3, 2015 at 7:59

1 Answer 1

4

Here is an attempt to answer both questions using the asypictureB package (which is an alternative interface to Asymptote). It takes advantage of the fact that when using asypictureB you can name your Asymptote pictures, and use that name to recall the graphics produced. Note that \jobname is essentially the name of your master tex file; for instance, if this file were called foo.tex, then \jobname would expand to foo.

For the second question, I take advantage of the include command in Asymptote, which directly includes the code from a specified Asymptote file. Also note that within an asypicture environment, @jobname behaves like \jobname would outside the environment (with a number of very strong caveats explained in the package documentation).

\documentclass{article}
\usepackage{asypictureB}
\begin{document}
A red disk:
\begin{center}
\begin{asypicture}{name=red_disk}
size(3cm);
fill(unitcircle, red);
\end{asypicture}
\end{center}
%
A green disk:
\begin{center}
\begin{asypicture}{name=green_disk}
size(4cm);
fill(unitcircle, green);
\end{asypicture}
\end{center}
%
A red disk and a green disk:
\begin{center}
\includegraphics{\jobname-red_disk}
\hspace{1cm}
\includegraphics{\jobname-green_disk}
\end{center}
%
A red disk and a green disk in the same \verb!asypicture!:
\begin{center}
\begin{asypicture}{name=overlay}
// Execute all the code from the red_disk asypicture.
include "@jobname-red_disk";

// Save the resulting picture and start a new one.
picture reddisk = currentpicture;
currentpicture = new picture;

// Execute all the code from the green_disk asypicture.
include "@jobname-green_disk";

// Save the resulting picture and start a new one.
// Note that the scale information from the previous 
// pictures is lost. If you want to retain it, look into 
// using frames instead of pictures.
picture greendisk = currentpicture;
currentpicture = new picture;
unitsize(1cm);
add(shift(-0.3,0)*greendisk);
add(shift(0.3,0)*reddisk);
\end{asypicture}
\end{center}

\end{document}

The result:

a page showing the result of the code above

1
  • it's very helpful! it should be included in your document on asypictureB package
    – Black Mild
    Apr 19, 2019 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .