[metapost] accessing variables in nested macros

Mojca Miklavec mojca.miklavec.lists at gmail.com
Sat Feb 10 17:26:55 CET 2007


On 2/9/07, Stephan Hennig wrote:
> Hi,
>
> within two macros that share the same initialization calculations I want
> to put those calculations into a new macro called "init".  Now, I don't
> want to pass a whole bunch of variables to "init" and tried a simple def
> declaration in the hope that "init" then shares the context (group and
> variables) of the calling macro.  That is, I expected
>
> def init=
>  calculation;
> enddef;
>
> vardef calcA(expr parameters)=
>   init;
> enddef;
>
> when called, more or less to literally replace "init;" by "calculation;"
> and therefore accessing variables from "calcA" in "init" should be ok.
> However, in the example below I can't access variables from the calling
> macro.  What's wrong with my definition?
>
> Best regards,
> Stephan Hennig
>
>
> def init=
> %%% Variable b should be saved with respect to the
> %%% local group of the calling vardef macro.
> save b;
> numeric b;
>   b := a + 1;
> enddef;
>
> vardef calcA(expr a)=
>   init;
>   show "a = " & decimal a;
> %   show "b = " & decimal b;
> enddef;
>
> beginfig(1);
>   calcA(163);
> endfig;
> end

I don't know if I understood your question properly. Are you looking
for something like the following code?

def init=
 b := a + 1;
enddef;

def calcA(expr aa)=
save a; numeric a; a := aa;
save b; numeric b;
 init;
 show "a = " & decimal a;
 show "b = " & decimal b;
enddef;

Mojca


More information about the metapost mailing list