When using pfgmathsetmacro
in commands (defined by \newcommand
) we have a problem of locality of variables. For instance if you design two functions
\newcommand\f[4]{%
\pgfmathsetmacro#3{cosh(#1+#2)}%
\pgfmathsetmacro#4{cos(#1+#2)}%
}
\newcommand\g[4]{%
\pgfmathsetmacro#3{sqrt(1+(#1-#2)^2)}%
\pgfmathsetmacro#4{sqrt((#1)^2+(#2)^2)}%
}
and want to define the composed function
\newcommand\h[4]{%
\f#1#2\x\y%
\g\x\y#3#4%
}
How do you protect the variables \x
and \y
such that you do not have to worry about not naming other variables \x
or \y
?
If you have only one variable, the problem can be avoided using \pgfmathresult
, but what if you have two (or more !) variables ?
Best Answer
I'll show a simplified version with one variable functions:
The “local variable”
\x
is forgotten as soon as the final\x
is executed.Instead of
\x
you might want to use\AcOmMaNdNaMeThAtSpRoBaBlYnOtUsEd
or something unusual like this.Here's the response from
bc -l
, that shows that the functions are computed correctly, given the low precision with PGF: