5

Consider the following code:

beginfig(1);

i:=0;

forever: 

    if odd i:
        show(i);
        exitif true;
    fi

    i:=i+1;
endfor

endfig;

end

When I run it, I get (end occurred when if on line 13 was incomplete).

Is there is a way to turn these messages off?

1 Answer 1

6

There might be a better way, but similarly to conditionals in TeX, one way is to ensure that fi is expanded before exitif is executed. So e.g.

beginfig(1);

i:=0;

forever: 

    if odd i:
        show(i);
        expandafter exitif expandafter true expandafter;
    fi

    i:=i+1;
endfor
endfig;

end

or

beginfig(1);

i:=0;

forever: 

    if odd i:
        show(i);
        scantokens "fi"
        exitif true;
    fi

    i:=i+1;
endfor
endfig;

end

In the second version, the scantokens hides the first fi when the condition is false, while it is executed when the condition is true and the second fi is skipped.

5
  • This works, but I wonder why I find no occurrence of expandafter in the MetaPost manual (version 2.00, June 1, 2019). Do you have an idea? Thanks!
    – frougon
    Commented Apr 1, 2020 at 14:45
  • 1
    @frougon As chapter B.1 says: "Certain other features from METAFONT are omitted entirely because they are of limited interest to the MetaPost users and/or would require long explanations. All of these are documented in The METAFONTbook as explained in Appendix C.1." Commented Apr 1, 2020 at 14:53
  • 2
    But the short version is: expandafter works exactly the same as \expandafter in TeX, you just have to think about what a token is here. Commented Apr 1, 2020 at 14:54
  • This is very nice, thanks. So def break = expandafter exitif expandafter true expandafter enddef; is all I need now.
    – Patrik Bak
    Commented Apr 1, 2020 at 14:54
  • @PatrikBak That version of break has to be followed by fi (or else) directly. Especially, it should never be used before endfor. (Otherwise you break out of the wrong loop) Commented Apr 1, 2020 at 14:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .