[metapost] accessing variables in nested macros

Stephan Hennig mailing_list at arcor.de
Fri Feb 9 17:19:46 CET 2007


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


More information about the metapost mailing list