Menu

Multiple outputs from a single input

Help
2023-06-02
2023-06-03
  • Jim Hefferon

    Jim Hefferon - 2023-06-02

    Hello, I'm writing a book and there are a lot of illustrations (more than 2K), almost all in Asymptote. So first, thank you!

    In MetaPost I liked that the canonical way to proceed was to put many related graphics in a single file. I do the same in Asymptote, especially because I have so many figures, and of course there are often classes of related figures. I use a structure like this.

    picture pic;
    int picnum = 4;
      -- figure drawing in here --
    shipout(format(OUTPUT_FN,picnum),pic,format="pdf");
    

    I mean this to be like MetaPost's

    beginfig (4)
      -- figure drawing in here --
    endfig;
    

    One thing that I have not been able to dope out in Asymptote how to do shadowing (sorry if this is the wrong term). I want a construct where changes inside get forgotten when you leave (as with TeX grouping). For example, this file:

    // test.asy
    import settings;
    
    string OUTPUT_FN = "test%03d";
    
    write(format("dotfactor=%f",dotfactor));
    // =============
    picture pic;
    int picnum = 0;
    unitsize(pic, 1cm);
    
    dotfactor = 3;
    write(format("inside: dotfactor=%f",dotfactor));
    draw(pic, (0,0)--(1,1), red);
    shipout(format(OUTPUT_FN,picnum),pic,format="pdf");
    // =============
    
    write(format("dotfactor=%f",dotfactor));
    

    produces this result.

    jim@millstone:~/tmp/asy$ asy test
    dotfactor=6
    inside: dotfactor=3
    dotfactor=3
    jim@millstone:~/tmp/asy$ 
    

    Is there some construct that would result in the final one saying dotfactor=6? Am I missing something obvious?

    Let me mention the motivation for the question. I'm giving a small talk about some of the above at the TUG meeting in June, trying to fly the Asymotote flag a little, and I wanted to get it right.

    Regards,
    Jim Hefferon

     
  • John Bowman

    John Bowman - 2023-06-03

    Hi Jim,
    If you really want each section of code to be run in a separate asy process, you can use eval for this:

    write(format("dotfactor=%f",dotfactor));
    // =============
    eval(quote {
    string OUTPUT_FN = "test%03d";
    
    picture pic;
    int picnum = 0;
    unitsize(pic, 1cm);
    
    dotfactor = 3;
    write(format("inside: dotfactor=%f",dotfactor));
    draw(pic, (0,0)--(1,1), red);
    dot(pic,(0,0));
    shipout(format(OUTPUT_FN,picnum),pic,format="pdf");
    },false);
    
    // =============
    
    
    write(format("dotfactor=%f",dotfactor));
    

    If you are including this in a TeX document, it might be better to use
    \begin{asy}...\end{asy} as shown in latexusage.tex, or \asyinclude{},
    along with latexmk. Common code can be enclosed within \begin{asydef}...\end{asydef}.

    Enjoy the TUG meeting (and say hello to everyone from me)!

     
  • Jim Hefferon

    Jim Hefferon - 2023-06-03

    Thank you, John. I'm glad I didn't miss something obvious, as has happened to me before.

    Regards,
    Jim

     
    • Charles Staats

      Charles Staats - 2023-06-03

      Another potentially relevant technique (which you may already be familiar
      with given your use of the term "shadowing"):

      If you put everything inside a static struct, and define a new variable
      real dotfactor inside that struct, then every time you refer to
      dotfactor within that struct, it will refer to the new variable rather
      than the old one. (This is "shadowing.") BUT if you do this, then external
      functions like dot() that use the old variable will still use the old
      variable, even if called from within the struct.

      I don't really see any way a programming language can support the
      Metapost-style behavior without dynamic scoping
      https://stackoverflow.com/a/22395580/2318074, which is generally
      considered bad practice in programming language design. The better approach
      (from a language design perspective) would have been to minimize the use of
      global variables in the plain module. Unfortunately, that ship has
      sailed. And in truth, it doesn't present much of an issue in
      Asymptote's most common use case (i.e., one program <--> one shipout).

      Best,

      Charles

      On Sat, Jun 3, 2023 at 4:56 AM Jim Hefferon jhefferon@users.sourceforge.net
      wrote:

      Thank you, John. I'm glad I didn't miss something obvious, as has happened
      to me before.

      Regards,
      Jim


      Multiple outputs from a single input
      https://sourceforge.net/p/asymptote/discussion/409349/thread/cd877e3d2f/?limit=25#8c2b


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/asymptote/discussion/409349/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       

Log in to post a comment.