4

I am trying to create an environment that essentially centers and creates an asymptote diagram, given the diagram's code. Here is a minimal working example:

\documentclass{article}

\usepackage{asymptote} \newenvironment{diagram}{\begin{center}\begin{asy}}{\end{asy}\end{center}}

\begin{document}

\begin{diagram} draw((0, 0) -- (1, 1)); \end{diagram}

\end{document}

The code above gives the Runaway argument? File ended while scanning use of \next error. This also seems to be an Asymptote-specific error; if I replace the \begin{asy} and \end{asy} with something else, it works fine. This is similar to this question, but following the answer there doesn't work. Is there a workaround?

1 Answers1

3

The asy environment is type of verbatim environment, and as such it requires that the the literal string \end{asy} appears at the end of the environment. Since you hide it inside diagram it doesn't find the \end and scans too far ahead in the file, eventually reaching its end and then you get an error that the file has ended:

Runaway argument?
! File ended while scanning use of \next.
<inserted text> 
                \par 
<*> test.tex

?

Fortunately it is rather easy to change the string that asy looks for to end the environment to be that of your own environment. Here is a patch that defines \asyenv{<name>}, such that \begin{asy} will look for \end{<name>} to end the verbatim environment. In your case you have to say \asyenv{diagram}:

\documentclass{article}

\usepackage{asymptote}

\usepackage{xpatch} \makeatletter \xpatchcmd\asy {\ProcessAsymptote{asy}} {\expandafter\ProcessAsymptote\expandafter{\asy@outer@env}} {}{\errmessage{Failed to patch asy}} \def\asyenv#1{\def\asy@outer@env{#1}}%

\newenvironment{diagram}{% \begin{center}% \asyenv{diagram}% \begin{asy}% }{% \end{asy}\end{center}% }

\begin{document}

\begin{diagram} draw((0, 0) -- (1, 1)); \end{diagram}

\end{document}