I have a question about \saveddimen usage inside of a \pgfdeclareshape command. My code is as follows:

      \pgf@x=20pt  %% =\mydima
        \pgf@xa=\mydima \pgf@ya=\mydima
        \pgf@xb=\mydimb \pgf@yb=\mydimb
   \draw[help lines] (0,0) grid (4,4);
   \node[myshape,draw] at (2,2) {test};

This produces a node, with top and bottom horizontal lines. In my real world example, I want to have a dimension \dimenb which is a function of \dimena. If I uncomment the %% in the \saveddimen{\dimenb} command the setting \pgf@x=\dimmena I get an error message.

Question: Is it legal to use a saved dimension in the definition of another saved dimension, and if yes, how?

  • Welcome to TeX.sx! You don't have to sign with your name since it automatically appears in the lower right corner of your post.
    – Werner
    Commented Mar 8, 2012 at 22:11
  • Only \saveddimen{\mydima}{\pgf@x=20pt}\saveddimen{\mydimb}{\pgf@x=-\pgf@x} would be enough. Since \pgf@x is overwritten by \mydima previously.
    – percusse
    Commented Mar 8, 2012 at 22:49
  • Thank you for this answer! You tell me (correct?) that I should use the fact that after defining \mydima the register \pgf@x still holds the dimension, and I can use it in the definition of \mydimb. That is easily breakable code. Add additional code bewtween the statements to break it \saveddimen{\mydima}{ --set \pgf@x here} \some-code-manipulating \pgf@x \saveddimen{\mydimb}{ --\pgf@x here is no longer set to \mydima} I still wonder why I cannot use the \mydima macro in another \saveddimen command, as it works in the \backgroundpath ? Commented Mar 9, 2012 at 15:18
  • @BertfriedFauser I'm really sorry for the delay. If you want to ping someone in the comments you can put an @ character and the nickname just as I did in this comment. There is even an autocomplete feature if you type a few characters and press TAB key. I will try to address your question as soon as possible.
    – percusse
    Commented Mar 22, 2012 at 16:27
  • @percusse No worries, I see your answer `works' for me, so I am not stuck. As I am new to stack exchange, I'll need to learn how it properly works :-\ Commented Mar 23, 2012 at 17:52

1 Answer 1


In the comments, percusse suggests writing:


To see why this works, we need to trace through the definitions. Within \pgfdeclareshape then \saveddimen is actually \pgf@sh@saveddimen whose definition is:

  \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@\shape@name\endcsname{\pgf@sh@resaveddimen{#1}{#2}}}

So when the shape is declared, we see that \pgf@sh@s@<shapename> gains the command \pgf@sh@resaveddimen{#1}{#2}. Presumably, the macro \pgf@sh@s@<shapename> is run when the shape is used to set all the dimensions and so forth. So to find out what happens when it is run, we need to look at \pgf@sh@resaveddimen. This is:


So #2 (which was the execution code) gets run, then \pgf@x gets set to a global version of itself (this is important). Now this doesn't get saved as the given name, but the code \def\<given name>{<the actual value of \pgf@x>} gets saved to \pgf@sh@savedpoints. Note that this is the actual value, not the macro \pgf@x.

We have three stages, therefore: declaration when the template is set up, definition where a shape is defined, and use where the various parts are used (parts of which are in the definition stage, so this can get confusing).

Let's trace this through with the above code, and its variations.

  1. Declaration. So here we run the \saveddimen commands which do the code appendages. At the end of this, \pgf@sh@s@myshape has (in addition to whatever else it has):

  2. Definition. Now \pgf@sh@s@myshape is run. This executes the above two commands (amongst other things). After the first is run, the state of play is the following: \pgf@x is globally set to 20pt and \pgf@sh@savedpoints has \def\mydima{20pt} in it. The first of these is the reason why we can use \pgf@x in the definition of \mydimb. The second is the reason why we cannot use mydima.

    Then at the end, \pgf@sh@savedpoints contains \def\mydima{20pt}\def\mydimb{-20pt}.

  3. Use. This is where \pgf@sh@savedpoints is executed and all the macros are finally defined. So everything is set up before the assignments are made.

In conclusion, \mydima is not available when the code for \mydimb is set up but so long as they are declared one after the other then when the definition code for \mydimb is executed then the value of \pgf@x is whatever \mydima would have been set to.

  • Wow, TeXbook explanation compared to my saved (and forgotten again!) draft.
    – percusse
    Commented Apr 14, 2012 at 22:18
  • @AndrewStacy Great explanation, that helps a lot to understand whats going on. Commented Apr 16, 2012 at 10:05
  • @percusse Thanks. I was a bit worried I might be stepping on toes, but decided from the timings that it was okay. Bertfried: Your question, and percusse's solution, piqued my curiosity! I hate not understanding something. Commented Apr 16, 2012 at 10:15
  • No worries, that can never happen. At most, a bad joke would follow :)
    – percusse
    Commented Apr 16, 2012 at 10:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .