I cannot find the answer to the following question in Knuth's Metafont book.
The following definition with a trailing text argument:
def mydef (expr a, b, c) text modifier =
if
Am 21.10.2011 15:05, schrieb Hans van der Meer:
I cannot find the answer to the following question in Knuth's Metafont book. The following definition with a trailing text argument:
def mydef (expr a, b, c) text modifier = if
:
Have you already tried if modifier = "" :
else: fi enddef;
How can I do the switch on an empty or nonempty modifier argument? It is not a string because calling its length fails with an error. I must do the switch, because "fill modifier" with an empty "modifier" gives horrible results.
Usage would be calls like: mydef(1,2,3) withcolor green; % with modifier text mydef(1,2,3); % without modifier text
Thanks in advance. Hans van der Meer
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________
On 21 okt. 2011, at 17:13, Peter Rolf wrote:
Am 21.10.2011 15:05, schrieb Hans van der Meer:
I cannot find the answer to the following question in Knuth's Metafont book. The following definition with a trailing text argument:
def mydef (expr a, b, c) text modifier = if
: Have you already tried
if modifier = "" :
else: fi enddef;
I tried it now, but it doesn't work. Something like if(TEXT4) ="":;fi in the error log. No, Metapost is not happy with this construct.
How can I do the switch on an empty or nonempty modifier argument? It is not a string because calling its length fails with an error. I must do the switch, because "fill modifier" with an empty "modifier" gives horrible results.
Usage would be calls like: mydef(1,2,3) withcolor green; % with modifier text mydef(1,2,3); % without modifier text
Thanks in advance. Hans van der Meer
Peter, Thanks for trying to help. Because I solved the problem in another way you might be interested or even benefit from it. The underlying problem was drawing a border around a picture of given dimensions. The border being either colored or left uncolored. The border color should come from a withcolor modifier, with the uncolored case done simply by leaving this out, for example: borderinsideframe(u,v,2g); and borderinsideframe(u,v,2g) withcolor red; It turns out this can be done with an unfill. I learned that a bare unfill erases, but add a withcolor modifier and the effect is a coloring. Thus the trick is: def bordercolorframe (expr w, h, off, clr) text modifier = unfill (-off,-off)--(w+off,-off)--(w+off,h+off)--(-off,h+off)--cycle modifier; fill (0,0)--(w,0)--(w,h)--(0,h)--cycle withcolor clr enddef; The omission of a semicolon after the last statement of the def allows one to add in a withcolor somecolor without triggering an error. Hope this will be of use to someone. Hans van der Meer On 21 okt. 2011, at 17:13, Peter Rolf wrote:
Am 21.10.2011 15:05, schrieb Hans van der Meer:
I cannot find the answer to the following question in Knuth's Metafont book. The following definition with a trailing text argument:
def mydef (expr a, b, c) text modifier = if
: Have you already tried
if modifier = "" :
else: fi enddef;
How can I do the switch on an empty or nonempty modifier argument? It is not a string because calling its length fails with an error. I must do the switch, because "fill modifier" with an empty "modifier" gives horrible results.
Usage would be calls like: mydef(1,2,3) withcolor green; % with modifier text mydef(1,2,3); % without modifier text
Thanks in advance. Hans van der Meer
In article <0E2E620E-1148-41AD-9966-54A594FCCE94@xs4all.nl>,
Hans van der Meer
I cannot find the answer to the following question in Knuth's Metafont book. The following definition with a trailing text argument:
def mydef (expr a, b, c) text modifier = if
: else: fi enddef; How can I do the switch on an empty or nonempty modifier argument? It is not a string because calling its length fails with an error.
A text argument is a (more or less) arbitrary sequence of tokens—that is, loosely speaking, any “chunk” of MetaPost code.
I must do the switch, because "fill modifier" with an empty "modifier" gives horrible results.
Usage would be calls like: mydef(1,2,3) withcolor green; % with modifier text mydef(1,2,3); % without modifier text
This is the best I could think of: vardef emptytext?@# text t = (str @# = "empty.sandwich") enddef; def mydef(expr a,b,c) text t = if begingroup emptytext? empty t sandwich endgroup: % t is empty draw unitsquare scaled 1cm; else: % t is not empty fill unitsquare scaled 1cm t ; fi; enddef; The key is the 'emptytext? empty t sandwich;' line: if t is empty, @# matches 'empty.sandwich', otherwise it matches 'empty' possibly followed by something else (depending on what t actually is). The undelimited text parameter of emptytext? is needed to “swallow” whatever tokens are left (possibly none) after matching the implicit suffix parameter. Note that begingroup and endgroup are necessary in the if clause, otherwise emptytext?'s undelimited text parameter will match beyond the colon. In practice, you should rename 'sandwich' to some token that you are pretty sure will never occur as the beginning of the text argument when you invoke mydef. I'd be glad to hear about other solutions: this question has bugged me for some time… Regards, Nicola
participants (4)
-
Hans van der Meer
-
Meer, H. van der
-
Nicola
-
Peter Rolf