TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I declare a length via pgfmathsetlength global?

The example results in 20pt -- 5pt -- 20pt But I had expected: 20pt -- 5pt -- 5pt




I don't want to discuss possibilities with \setlength or \deflength.

share|improve this question
What does your .log file say about using \global? – Werner Dec 20 '11 at 21:43
It's interesting that \global doesn't issue an error; this is because the token that actually it acts on is \futurelet; it definitely doesn't act on the setting of \testtl's value, unfortunately. – egreg Dec 20 '11 at 21:46
up vote 12 down vote accepted

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
% Description:
% 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
share|improve this answer
Very similar to \setlength{\global\testl}{5pt} (which works). :) – egreg Dec 20 '11 at 23:14

Use a scratch dimension


Note. I wouldn't use \testl in place of \temp, in order to avoid "save stack buildup".

share|improve this answer
Interesting. Why does save stack buildup occur if \testl is used instead of \temp. Shouldn't the local assignment not being discarded at the end of the group? – Martin Scharrer Dec 20 '11 at 22:16
@MartinScharrer It will happen if \testl has already been used in the outer level. – egreg Dec 20 '11 at 22:25
Why isn't a previously local assignment in the outer level not be discarded from the save stack as soon a global assignment is made? Or is this technical not feasible? – Martin Scharrer Dec 21 '11 at 9:44
@MartinScharrer It's clearly explained both in the TeXbook and in TeX by Topic. A local assignment makes TeX push the "outer value" in the save stack and there it will remain, since the global assignment made in the group will tell TeX not to go fetching that value again. Why the useless entry is not discarded must be asked to Knuth. – egreg Dec 21 '11 at 10:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.