[metapost] variable parameter list

Jens-Uwe Morawski s5d5 at gmx.net
Thu Feb 9 19:43:54 CET 2006


Am Donnerstag, den 09.02.2006, 10:34 -0600 schrieb Dan Luecking:

> I would like to be able to have a key-value syntax without the
> quotation marks, but I don't see any way to do that.


vardef mymacro (expr a) text T =

    save b, c, p, _pic_, _A_ ;
    numeric b ; color c ; pair p, _A_ ; picture _pic_ ;
    _pic_ := nullpicture ;
    
    if pair a: _A_ := a ; else: _A_:=(0,a) ; fi;
    
    processKeyvals(T) ;

    if not known b: b:=1      ; fi;
    if not known c: c:=red    ; fi;
    if not known p: p:=origin ; fi;

addto _pic_ doublepath (0,0)--_A_ scaled b shifted p withcolor c ;
	_pic_
enddef;

def processKeyvals (text _kvps_)=
  begingroup;
    save _equals, _assign ;
    let _equals= = ;
    
    primarydef _ll_ _assign _rr_ =
      hide(_ll_ _equals _rr_ ) 1
    enddef;

    save = ;
    let = _equals _assign ;
    for _xx_ _equals _kvps_ : endfor ;% NOP
  endgroup;
enddef;

beginfig(1);

draw mymacro(1cm) ;
draw mymacro((1cm,2cm)) c=blue;
draw mymacro((-1cm,2cm)) c=blue,p=(5mm,5mm);
draw mymacro(-1cm) b=2,c=green ;

endfig;
end



More information about the metapost mailing list