3

I am trying to create parameterized macro to draw a shape with penpos and penstroke macros in MetaPost (Metafont?) with plain.mp base but I am getting a error. Running the code below,

def curve_st(expr p) = begingroup
    save z;
    pair z[];

    z0 = p;
    z1 = p + (2,-2);
    z2 = z1 + (0, 5);
    z3 = z2 + (3, 0);

    penpos0(tip,  180);
    penpos1(stem, -30);
    penpos2(stem, 0);
    penpos3(tip,  180);

    penstroke z0e{down}.. z1e{up} .. z2e{up} .. z3e;
    %draw z0{down}.. z1{up} .. z2{up} .. z3;

endgroup enddef;
beginfig(1);
    xstep = 3; stem = 1.5; tip = .2;

    curve_st((-3,0)); curve_st((0,0)); 
endfig; end.

gives the following error:

>> unknown path path_.l
>> z0l
! Equation cannot be performed (unknown path=numeric).
<to be read again>

As far I understand, the penstroke is a macro that just calls fill and unfill after computing the "left" and "right" values of the points based on the (radius,angle) passed to the penpos macro. I suppose that this doesn't work outside beginfig and endfig. And that is why Metafont can't find z0l. Because the draw command works without any error.

How can I use penpos and penstroke here? Or, do I have to manually compute left/right values and call the fill/unfill?

Please keep in mind that I am a complete beginner in MetaPost/Metafont. Thank you.

2
  • 2
    Can you please explain if you use Metafont or Metapost? The reason I ask is I cannot find penpos and penstroke in the Metapost manual.
    – mickep
    Apr 19 at 5:54
  • 1
    I use the MetaPost compiler mpost. The penpos and penstroke are explained in details in Chapter 4 in Knuth's The METAFONTbook. If I penpos and penstroke directly inside beginfig/endfig it works as expected.
    – sigsegv
    Apr 19 at 5:59

1 Answer 1

3

The

save z;
pair z[];

seems to be the problematic part. z is special. Replacing it with

clearxy;

seems to do it here. Doing that and compiling your file, I get this output.

metapost thingies

5
  • Thank you so much! That is what I was aiming to do. Can you explain (or point me to a resource that explains) what the save, clearxy does and how do I make something local variable to a given group ?
    – sigsegv
    Apr 19 at 6:40
  • Sorry, I will not be able to explain it better than in the Metafont book.
    – mickep
    Apr 19 at 6:43
  • To avoid such issues, you can also name your variable with another letter.
    – sztruks
    Apr 20 at 7:25
  • @sztruks Then how does Metafont know that penpos0 is located at u0 and not z0? What if I have multiple arrays, z0, w0 and so on?
    – sigsegv
    Apr 24 at 10:49
  • I cannot help much: penpos0 looks to me like a METAFONT primitive. I am just a METAPOST/METAFUN user and what I said applies to its absent minded users.
    – sztruks
    Apr 24 at 21:01

You must log in to answer this question.

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