Hi folks, hoping someone who really understands Metapost can give me a suggestion since I cannot find it in any manuals. In Therion's Metapost, there are a couple of wildcard suffixed macros like this: vardef p_label@#(expr txt,pos,rot,mode) I have a need to call the different ones depending on the value of a pair like this: if A = (-1,1): p_label.ulft(thetext,P,R,style); elseif A = (0,1): p_label.top(thetext,P,R,style); ... more ... Based on the value of another variable, I need to run a different macro instead, which also has its own suffixes, which match the suffixes above: if A = (-1,1): if Foo = "a" thelabel.ulft(txt, pos); ... other stuff ... else: p_label.ulft(thetext,P,R,style); fi; elseif A = (0,1): if Foo = "b" thelabel.ulft(txt, pos); ... other stuff ... else: p_label.top(thetext,P,R,style); fi; ... more ... This quickly gets tedious, since the same code gets repeated over and over again, just with a different suffix each time. In Metapost, is it possible to store the suffix in a variable, then use it when calling the macro like so? if A = (-1,1): sfx:="ulft"; elseif A = (0,1): sfx:="top"; ... if Foo = "b" thelabel.<sfx>(txt, pos); ... other stuff ... else: p_label.<sfx>(thetext,P,R,style); fi; Thanks for any advice. Tarquin
I do seem to like answering my own questions. 3 days searching for the solution and ... if A = (-1,1): sfx:="ulft"; elseif A = (0,1): sfx:="top"; ... if Foo = "b" thelabel.scantokens(sfx)(txt, pos); ... other stuff ... else: p_label.scantokens(sfx)(thetext,P,R,style); fi; Yes, scantokens can be used inline, as a function-like call that returns an actual suffix token which becomes part of the macro's suffix when the macro is subsequently called. This is such a weird programming language :)
This is such a weird programming language :)
It is... And despite being plenty of documentation online, this one seems to be mastering me (and i usually love programming)
Am 01.03.2020 um 21:05 schrieb Tarquin Wilton-Jones via Therion <therion@speleo.sk>:
I do seem to like answering my own questions.
3 days searching for the solution and ...
if A = (-1,1): sfx:="ulft"; elseif A = (0,1): sfx:="top"; ... if Foo = "b" thelabel.scantokens(sfx)(txt, pos); ... other stuff ... else: p_label.scantokens(sfx)(thetext,P,R,style); fi;
Yes, scantokens can be used inline, as a function-like call that returns an actual suffix token which becomes part of the macro's suffix when the macro is subsequently called.
This is such a weird programming language :) _______________________________________________ Therion mailing list Therion@speleo.sk https://mailman.speleo.sk/listinfo/therion
participants (2)
-
Benedikt Hallinger -
Tarquin Wilton-Jones