[metapost] augment drawoptions

Dan Luecking luecking at uark.edu
Wed Apr 16 01:34:04 CEST 2008


At 02:35 AM 4/12/2008, Taco Hoekwater wrote:
>Stephan Hennig wrote:
> >
> > To keep changes to draw options locally, I've tried to save _op_ prior
> > to using addtodrawoptions.  But again, this doesn't work.  Any ideas?
>
>You cannot save _op_ because it is a macro, not a variable.

You _can_ save it, but it then immediately becomes undefined, and
so any effort to define it in terms of itself will have to fail.
If you do save it, its meaning will return after the group.

>You could do explicit macro definitions and restores with expandafter:
>
>def savedrawoptions =
>    expandafter def expandafter _savedop_ expandafter = _op_ enddef;
>enddef
>
>def restoredrawoptions =
>    expandafter def expandafter _op_ expandafter = _savedop_ enddef;
>enddef

If you combine these as illustrated below, you can make use
of the endgroup to restore _op_. This permits nested groups.
def save_op_ =
   expandafter def expandafter _savedop_ expandafter = _op_ enddef;
   save _op_;
   expandafter def expandafter _op_ expandafter = _savedop_ enddef;
enddef;
def addtodrawoptions (text t) =
   expandafter def expandafter _op_ expandafter = _op_ t enddef;
enddef;
% example
beginfig(100);
   pickup pencircle scaled .5bp;
   drawoptions(withcolor red);
   begingroup
     save_op_;
     addtodrawoptions(withpen pencircle scaled 10bp);
     % fat and red
     draw halfcircle scaled 100;
     begingroup
       save_op_;
       addtodrawoptions(withcolor blue);
       fat and blue
       draw halfcircle scaled 100 shifted (0,100);
     endgroup;
     % fat and red again
     draw halfcircle scaled 100 shifted (0,200);
   endgroup;
   % thin and red
   draw halfcircle scaled 100 shifted (0,300);
endfig;


Dan


Daniel H. Luecking
Department of Mathematical Sciences
University of Arkansas
"Dubito ergo cogito, cogito ergo sum" --Descarte



More information about the metapost mailing list