3

I am playing the following code from page 15 in Learning METAPOST by Doing

prologues := 3;
outputtemplate := "%j-%c.eps";

beginfig(1);
  save arrowhead;
  vardef arrowhead(expr p) =
    save A,u,a,b; pair A,u; path a,b;
    A := point length(p)/2 of p;
    u := unitvector(direction length(p)/2 of p);
    a := A{-u}..(A - ahlength*u rotated 30);
    b := A{-u}..(A - ahlength*u rotated -30);
    (a & reverse(a) & b & reverse(b))--cycle
  enddef;
  u:=2cm; ahlength:=0.3cm;
  drawarrow (0,0)..(u,u)..(-u,u);
endfig; end;

But running mpost arrow.mp always gives the following error:

This is MetaPost, version 2.00 (TeX Live 2019) (kpathsea version 6.3.1)
(/usr/local/texlive/2019/texmf-dist/metapost/base/mpost.mp
(/usr/local/texlive/2019/texmf-dist/metapost/base/plain.mp
Preloading the plain mem file, version 1.005) ) (./arrow.mp
! Missing argument to arrowhead.
<to be read again>
                   _apth
_finarr->...._apth(TEXT0);filldraw.arrowhead._apth
                                                  (TEXT0)
<to be read again>
                   ;
l.15   drawarrow (0,0)..(u,u)..(-u,u);

?

My Tex settings:

$ brew cask info mactex                                                                       
mactex: 2019.0508

Can anyone help me on this?

2

The tutorial you are using is from 2005, so you can probably expect to find some incompatibilities with current METAPOST versions. In this case it is sufficient to remove the parentheses in the arrowhead definition.

MWE:

prologues := 3;
outputtemplate := "%j-%c.eps";

beginfig(1);
  save arrowhead;
  vardef arrowhead expr p =
    save A,u,a,b; pair A,u; path a,b;
    A := point length(p)/2 of p;
    u := unitvector(direction length(p)/2 of p);
    a := A{-u}..(A - ahlength*u rotated 30);
    b := A{-u}..(A - ahlength*u rotated -30);
    (a & reverse(a) & b & reverse(b))--cycle
  enddef;
  u:=2cm; ahlength:=0.3cm;
  drawarrow (0,0)..(u,u)..(-u,u);
endfig; end;

Result:

enter image description here

See for other examples http://tex.loria.fr/prod-graph/zoonekynd/metapost/metapost.html, which I used to find the correct syntax for this answer.

0

Here's a small improvement that put's the arrow in the middle of the path, rather than at the middle point:

vardef arrowhead expr p =
    save A,u,a,b,t; pair A,u; path a,b; numeric t;
    t = arctime 1/2 (arclength p + ahlength) of p;
    A := point t of p;
    u := unitvector(direction t of p);
    a := A{-u}..(A - ahlength*u rotated 30);
    b := A{-u}..(A - ahlength*u rotated -30);
    (a & reverse(a) & b & reverse(b))--cycle
enddef;

like so:

enter image description here

The new things are

  • arclength p which returns the length of path p in PostScript points (instead of MP "time")

  • arctime x of p which returns the time on p at which it gets to the given length x.

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.