3

Doing my first steps with Asymptote (seems to be a great tool) I ran into the following problem. I usually try to put my graphics (tikz) into separate files to be more flexible. So I would like to handle the asy-graphics as well. But, the \ASYinput macro leads to the error message ! File ended while scanning use of \next. Is there any (simple) way to get this running?

\documentclass{scrartcl}
\usepackage{asymptote}

\newcommand{\ASYinput}[1]{%
   \begin{asy}
    \input{#1.asy}
   \end{asy}
}

\newcommand{\ASYinputx}[1]{%
    \input{#1.asy}
}

\begin{document}
blabla

\ASYinputx{test1}

blabla

\ASYinput{test2}

blabla
\end{document}

Here are the two test files.

// test2.asy
 size(10cm);
 draw((0,0)--(2,1));

and

// test1.asy
\begin{asy}
 size(10cm);
 draw((0,0)--(2,1));
\end{asy}
  • Only now I see that there is a dedicated command in asymptote package, see Christian Hupfer's answer. I think you should accept his answer instead of mine. – CarLaTeX Mar 18 '18 at 14:34
  • I am sorry, but I can't see Christian Hupfer on this thread. – Jürgen Apr 18 '18 at 12:58
  • Ops, I meant Charles Staats... – CarLaTeX Apr 18 '18 at 14:07
6

From the documentation for the asymptote package:

If you have Asymptote code in a separate file, you can include it with the \asyinclude[options]{filename} command.

  • Oups, I have to read again the doc... – O.G. Mar 10 '17 at 8:25
4

As indicated in this answer, you can't use a TeX command (\input) inside the asy environment; you have to use the asymptote command include.

Moreover, as indicated in this other answer, you can't hide the asy environment in a macro (but, of course, egreg found a solution).

Putting all this together, here's a working code:

\documentclass{scrartcl}
\usepackage{asymptote}

\makeatletter
\newcommand{\asycode}[2][]{%
    \stepcounter{asy}%
    \setkeys{ASYkeys}{#1}%
    \ifASYattach
    \ASYinlinefalse
    \fi
    \ifx\asydir\empty\else
    \def\ASYprefix{\asydir/}%
    \fi
    \immediate\write\AsyPreStream{%
        \noexpand\InputIfFileExists{%
            \ASYprefix\noexpand\jobname-\the\c@asy.pre}{}{}%
    }
    \asy@write@graphic@header
    \immediate\write\AsyStream{\detokenize{#2}}% here asy does the writing
    \asy@finalise@stream
    \asy@input@graphic
}
\makeatother
\newcommand{\ASYinput}[1]{%
    \asycode{include #1;}
}

\newcommand{\ASYinputx}[1]{%
\input{#1.asy}
}

\begin{document}

blabla

\ASYinputx{test1}

blabla

\ASYinput{test2}

blabla
\end{document}

With test1.asy:

\begin{asy}
 size(10cm);
 draw((0,0)--(2,1));
\end{asy}

and test2.asy:

size(10cm);
draw((0,0)--(2,1));

In this case, you can't use filecontents because it adds an empty line at the and of the file which is not accepted by asymptote.

If you want a piece of advice, use tikz!

enter image description here

  • Great! Thanks a lot for this thorough answer. Although I do not understand every step of the new macro, I will use it. Of course. -- Regarding your advice, which is surely welcome: I use TikZ for more than ten years and have about a thousand graphs in my presentations, lecture notes and so on. But, I haven't been successful in creating 'fine' 3-D-pictures of antenna diagrams, mainly due to numerical constraints (even with pgfplots). So I will give Asymptote a try. – Jürgen Mar 9 '17 at 6:40
  • This behavior (not possible to hide macro inside an environment without a true macro like the egreg one) is due to the fact that asy is based on the comments environment. asy has the same limitations. – O.G. Mar 9 '17 at 10:57
  • @Jürgen You're welcome, but for the macro you have to thank egreg! See also what O.G. has just written... – CarLaTeX Mar 9 '17 at 11:05
  • @O.G. Thank you for the explanation! I prefer TikZ even for the mere fact that you do not have to compile in 3 times... – CarLaTeX Mar 9 '17 at 11:08
  • @CarLaTeX We have the choice for graphics solutions... It is also possible to simply use \includegraphics and an appropriate Makefile for a complex process. Or asypictureb... – O.G. Mar 9 '17 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.