2

How can I use asymptote language inside an exsheets question?

For example, the code below will compile perfectly fine. But if the asypicture environment is moved inside the exsheets question environment, then errors happen.

(The same behavior is used if asy environment from asymptote package is used.)

\documentclass{article}
\usepackage{asypictureB}
\usepackage{exsheets}

\begin{document}

    \begin{question}
        How can I use asymptote inside a question?
    \end{question}

    \begin{asypicture}{}
        import graph3;
        import palette;

        size3(400,400,200,IgnoreAspect);
        currentprojection=orthographic(1,-3,50);

        real f(pair r) {return r.x^3-3*r.x*r.y^2;}

        real Arg(triple v) {return degrees(cos(v.z),warn=false);}

        surface s=surface(f,(-4,-4),(4,4),20,Spline);

        s.colors(palette(s.map(zpart),Rainbow()+opacity(0.75) ));
        draw(s,render(compression=Low,merge=true),meshpen=black);

        real xmin = -5;
        real xmax = +5;
        real ymin = -5;
        real ymax = +5;
        pen axisPen = red + 0.75;
        draw(Label("$x$",1),(xmin,0,0)--(xmax,0,0),axisPen,Arrow3);
        draw(Label("$y$",1),(0,ymin,0)--(0,ymax,0),axisPen,Arrow3);

        pen p=fontsize(12pt);
        pair P = (2.5,-2);
        dot("$P$",(P.x,P.y,f(P)),N,p);

        xaxis3("$x$",Bounds,InTicks);
        yaxis3("$y$",Bounds,InTicks(beginlabel=false));
        zaxis3("$f$",Bounds,InTicks);
    \end{asypicture}

\end{document}

1 Answers1

3

Short answer: You can't. In fact, you can't even use the verbatim environment inside an exsheets question environment. [Which suggests that this "environment" is really a command in disguise.]

Long answer: You can do it if you are prepared to subvert the package slightly:

\documentclass{article}
\usepackage{asypictureB}
\usepackage{exsheets}

\begin{document}

    \begin{asypicture}{}
        import graph3;
        import palette;

        size3(400,400,200,IgnoreAspect);
        currentprojection=orthographic(1,-3,50);

        real f(pair r) {return r.x^3-3*r.x*r.y^2;}

        real Arg(triple v) {return degrees(cos(v.z),warn=false);}

        surface s=surface(f,(-4,-4),(4,4),20,Spline);

        s.colors(palette(s.map(zpart),Rainbow()+opacity(0.75) ));
        draw(s,render(compression=Low,merge=true),meshpen=black);

        real xmin = -5;
        real xmax = +5;
        real ymin = -5;
        real ymax = +5;
        pen axisPen = red + 0.75;
        draw(Label("$x$",1),(xmin,0,0)--(xmax,0,0),axisPen,Arrow3);
        draw(Label("$y$",1),(0,ymin,0)--(0,ymax,0),axisPen,Arrow3);

        pen p=fontsize(12pt);
        pair P = (2.5,-2);
        dot("$P$",(P.x,P.y,f(P)),N,p);

        xaxis3("$x$",Bounds,InTicks);
        yaxis3("$y$",Bounds,InTicks(beginlabel=false));
        zaxis3("$f$",Bounds,InTicks);
        shipout("my_picture");
    \end{asypicture}

    \begin{question}
        How can I use asymptote inside a question?

        \includegraphics{my_picture}
    \end{question}

\end{document}

enter image description here

By including the shipout command with a name other than the default, you make Asymptote produce an image with a name that the asypicture environment does not expect. Consequently, the asypicture environment doesn't get a graphic to include, but you can include the real graphics at a later time of your choosing. The same trick should work for the asy environment in the asymptote package (I think).

Downsides:

  1. If you do this, the Asymptote picture will be recompiled every time you run latex (with shell-escape enabled), even if it has not changed.
  2. There may be a bit of extra space at the asypicture environment. (I'm actually not sure how this works, although I'm sure there are people on this site who could figure it out fairly easily.)

As an alternative, you might consider putting your Asymptote code in an asy file, compiling it with Asymptote, and then including only the image (but not the asymptote code) in your tex file. At least in your MWE, it doesn't look like you are using any features that really require the Asymptote code to be specified within the tex file.

  • 1
    Why can't asypicture be used as argument of a macro? Does it do catcode changes? – cgnieder Nov 10 '15 at 08:32
  • 1
    @clemens: Yes. The asypicture environment uses verbatim-like setup to write its contents to a file, which is processed with the Asymptote program to produce an image. – Charles Staats Nov 10 '15 at 16:50