# Simple way to switch inline asymptote figures on and off

I use the inline asymptote environment \begin{asy} to draw a lot of the figures in my papers. To those not familiar with asymptote, the asymptote package creates *.asy files for each of the asy environments in the document, so that if my document is article.tex running LaTeX will generate article-1.asy, article-2.asy, etc. Then you run asymptote (the program) to compile these into eps files article-1.eps, article-2.eps, etc. and the next time you run LaTeX it will include these graphics files where the corresponding asymptote environment was used.

However, this doesn't seem to be a standard package, so when I send the TeX file to arXiv or to a publisher I need to replace each \begin{asy}...\end{asy} with \includegraphics{article-1}, etc. Can anybody suggest a good way that I can make this switch back and forth easily by only changing one line? All my attempts have failed due to apparent quirks in how the asymptote package works.

• Welcome to TeX.sx! – Mensch May 1 '13 at 2:11
• Have a look at asyfig package which does "read­ing Asymp­tote fig­ures from sep­a­rate .asy files, rather than within the doc­u­ment," very useful incase of a large doc­u­ments. – texenthusiast May 1 '13 at 2:22
• May be generate all figures in pdf from fig.asy files using asy -f pdf *.asy inside the figures folder(Linux). – texenthusiast May 1 '13 at 2:29
• @texenthusiast: Having the asymptote code in a separate file would definitely make switching more snappy. I don't see how your second suggestion is supposed to help. I already do that as part of the compilation process (but without -f pdf). – Kallus May 1 '13 at 2:33

The following should do the trick:

\documentclass{article}
%\usepackage{asymptote}% http://ctan.org/pkg/asymptote
\newenvironment{asy}{\par}{\par}% Fake asymptote package
\usepackage{environ}% http://ctan.org/pkg/environ
\usepackage[demo]{graphicx}% http://ctan.org/pkg/graphicx
\newcounter{asyfigcntr}
\makeatletter
\@ifundefined{asy}{%
\NewEnviron{asy}{%
\par\stepcounter{asyfigcntr}%
\includegraphics{\jobname-\theasyfigcntr}%
\par%
}
}{}
\makeatother
\begin{document}
This is some text.

\begin{asy}
Here is some \texttt{asy} code
\end{asy}

This is some more text.

\begin{asy}
Here is some more \texttt{asy} code.
\end{asy}

Here is some text again.
\end{document}


Since I'm unfamiliar with the interface of asymptote, I've created a "fake asymptote package" through the environment asy. If this environment exists (done by checking for the existence of \asy, since the asy environment is made up of \asy and \endasy, primitively speaking), then compilation continues as expected. If the environment does not exist, I create a new one that gobbles its contents (thanks to the collection capability supplied by the environ package) and display the image given by \jobname-\theasyfigcntr (which expands to <filename>-<num>, where <num> is sequentially incremented).

When you comment out the "fake asymptote package", you get the following output:

The black boxes are from the [demo] option to graphicx, which you should not use in your own document.

For practical purposes and to be feasible for usage in your case, the assumption is that you have at least one successful compile of your document under asymptote in order to produce the figures.

• Works like a charm. Many thanks, and I've learned some new stuff (environ, demo) to boot. – Kallus May 1 '13 at 2:57
• @Werner is it possible to apply the same for How to turn off an environment (e.g. tikzpicture) for faster draft compiling – texenthusiast May 1 '13 at 3:06
• @texenthusiast: I'm sure it could. I'll add an answer there as well, for posterity. I don't consider the two questions duplicates though (definitely related)... – Werner May 1 '13 at 3:18
• @Werner Your answer is very nice hence it's worth to see on other question also :). Thanks a lot. – texenthusiast May 1 '13 at 3:28