[metapost] Beginners question

Taco Hoekwater taco at elvenkind.com
Tue Sep 12 16:00:04 CEST 2023


Hi,

Here is a fairly straigthforward plain mp solution for the “loose” UI:

%% a few globals
string font, style, s;

% binary operator “infant”: save the RHS to a global and retunr LHS
secondarydef a infant f =
  begingroup font:=f; a endgroup
enddef;
% binary operator “withstyle”: same approach
secondarydef a withstyle s =
  begingroup style:=s; a endgroup
enddef;
% unary operator to collect the data
def drawtext text a =
  font := "text";
  style := "normal";
  s := a; % this assignment triggers processing the binops
  show (s);
  show (font);
  show (style);  
enddef;
drawtext "my text to be output" infant "Meta Pro" withstyle "bold";
drawtext “another text to be output" withstyle “italic";
end.


(I replaced a few identifiers to avoid clashes)

Best wishes,
Taco

> On 12 Sep 2023, at 15:49, Aditya Mahajan <adityam at umich.edu> wrote:
> 
> On Tue, 12 Sep 2023, Patrick Gundlach wrote:
> 
>> Hello all,
>> 
>> I try to define a user interface such as:
>> 
>> draw "some text" infont "myfontfamily" withstyle "bold";
>> 
>> where infant ... and withstyle ... are optional ( and the defaults should be "text" and "regular").
>> 
>> (How) Is it possible to achieve this?
>> 
>> Is it advisable do to so? Or should I rather create an interface such as
>> 
>> draw text ("some text", "myfontfamily", "bold")?
> 
> IMHO, both interfaces are okay and it is a matter of personal preference. 
> 
> Another option is to have a key value option:
> 
> draw Text [ font="myfontfamily", style="bold", text="some text" ];
> 
> It is relatively simply to define such key-value interface in ConTeXt. For example, see:
> 
> https://adityam.github.io/context-blog/post/new-metafun-interface/
> 
> Everything is defined in metafun, so it might also work in metapost+metafun as well but I have never tested this outside context.
> 
> Aditya
> --
> http://tug.org/metapost/

Taco Hoekwater
Elvenkind BV







More information about the metapost mailing list.