[metapost] How to ignore a comma?

Mojca Miklavec mojca.miklavec.lists at gmail.com
Tue Feb 6 02:33:51 CET 2007


On 2/4/07, L. Nobre G. <nobre at cii.fc.ul.pt> wrote:
> Hint: forget about an arbitrary number of "text" arguments (I know
> that forgetting is not so easy).
>
> def somefunc( expr eone, etwo )( text tone )( text ttwo ) =
>       [...]
> enddef;
>
> beginfig(1);
>     pair vectors[];
>     path a;
>     pen b;
>     [...]
>     vectors0 = (23,34);
>     somefunc( a, b )( dashed evenly )( vectors );
> endfig;

Thanks a lot. I didn't know that multiple arguments are possible (and
that will come in handy too).

The syntax is much better that way, but after seing so many tricks ...
I simply cannot believe that it shouldn't be possible to do that
specific trick in metapost. I went reasearching through the manual one
more time and have found the "let" command. It took me quite some
trial-and-error failed examples, but perhaps I finally got it. The
following code now does what I want. Perhaps there is a more elegant
way, but I don't know any.

def do_something(expr xx, yy)(text t) =
	save c; color c; c = black;
	save d; numeric d; d = 5pt;

	let@=,;

	def set_color(expr col) =
		let,=@;
		let@=,;
		c := col;
		let,=;;
	enddef;

	def set_size(expr size) =
		let,=@;
		let@=,;
		d := size;
		let,=;;
	enddef;

	t;
	let,=@;

	fill fullcircle scaled d shifted (xx,yy) withcolor c;
enddef;

do_something(2.5cm,1.5cm,set_color(blue),set_size(8pt));

draw unitsquare scaled 3cm;


> On 04/02/07, Mojca Miklavec <mojca.miklavec.lists at gmail.com> wrote:
> > Hello,
> >
> > This might be easy to do, but I don't know how. I would like to write
> > a few simple macros accepting an arbitrary number of arguments.
> > Arguments are usually passed to metapost with a comma inbetween, but
> > if one sends "text" arguments, comma of course doesn't work. I could
> > put semicolon or a space between, but it looks a bit unnatural.
> >
> > How can I ask metapost for ignoring the next character if it happens
> > to be a comma? See the example below:
> >
> > beginfig(1);
> >
> > def do_something(expr xx, yy)(text t) =
> >
> >         % default values
> >         save c; color c; c = black;
> >         save d; numeric d; d = 5pt;
> >
> >         def set_color(expr col) =
> >                 c := col;
> >         enddef;
> >
> >         def set_size(expr size) =
> >                 d := size;
> >         enddef;
> >
> >         t;
> >
> >         fill fullcircle scaled d shifted (xx,yy) withcolor c;
> > enddef;
> >
> > % simple examples
> > do_something(1cm,2cm,set_color(blue));
> > do_something(2cm,1cm,set_size(10pt));
> >
> > % this works OK
> > do_something(2.5cm,1.5cm,set_color(red) set_size(8pt));
> >
> > % this works OK
> > do_something(2cm,2.5cm,set_color(green);set_size(8pt));
> >
> > % but the following syntax would look better
> > % do_something(2.5cm,1.5cm,set_color(red),set_size(8pt));
> >
> > draw unitsquare scaled 3cm;
> >
> > endfig;
> > end.


More information about the metapost mailing list