You can use
\global like this :
Some remarks : Egreg notices this code is very similar to
It may be interesting to know what the macro
pgfmathsetlength provides. In the file
pgfmathcalc.code.tes we can read :
% #1 = dimension register
% #2 = expression
% These functions work similar to \setlength and \addtolength. Only,
% they allow #2 to contain an expression, which is evaluated before
% assignment. Furthermore, the font is setup before the assignment is
% done, so that dimensions like 1em are evaluated correctly.
% If #2 starts with "+", then a simple assignment is done (but the
% font is still setup). This is orders of magnitude faster than a
% parsed assignment.
Another possibility to declare a length global is to redefine
pgfmathsetlength but it's bad or to use a new macro :
globalpgfmathsetlength based on
pgfmathsetlength. We need to add
\global in two lines but simplest is my first answer.
% Ok, quick version:
\global#1\pgfmathresult pt\relax% here add \global before #1
\global#1\pgfmathresult pt\relax% and here