2

Suppose I have a array of path path pat[]; I try to define a function like this:

def drawpaths(expr plists)=
  for p in plists:
    pickup defaultpen;
    draw pat[p];
  endfor
enddef

so that I can draw the path 0,2,3 simply by call drawpaths(0,2,3).

Unfortunately, metapost don't support for loops of a list, is there any solution?

5

Of course Metapost supports loops over lists. Here in ConTeXt:

\startMPpage
path pat[];
pat[0] := (0,0) -- (10,10);
pat[1] := (0,0) -- (10,20);
pat[2] := (0,0) -- (10,30);

def drawpaths(text plists) =
    for p = plists:
        pickup defaultpen;
        draw pat[p];
    endfor
enddef;

drawpaths(0,1,2);
\stopMPpage
  • That's it! Thanks a lot! We can also move the definition to the very beginning of the code and set the variable p as a local one. – user19832 Nov 22 '17 at 4:39

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.