I would like to perform a calculation using \pgfmathparse and then store \pgfmathresult as the value of a pgfkey.



       junk = \pgfkeysvalueof{/junk}\par


       % This second use of \pgfmathparse will change the contents of \pgfmathresult.
       % The key value will now be changed to the new contents of \pgfmathresult.
       junk is now = \pbfkeysvalueof{/junk}/par


The output of the code is:

junk = 15.0

junk is now = 40.0

So it looks like the value of the key points to the contents of the macro. If the contents of the macro change, then the value of the key changes.

This is not what I want. I want the value of the key to be the value of the macro. If the value of the macro changes, then the value of the key should remain unchanged.

I tried to use


but if I use \tempmacro later on for something else


then once again the value of the key is changed to the new value of the macro.

I don't want to have to define a macro for every key value that is stored. That seems to me to defeat the purpose of using pgfkeys. I could just use the macros, but I would really like to use the keys.

I also tried the suggestions in the post Store \pgfmathresult in a variable, but the result is the same.

  • Yes. That is exactly what is required. Searching in the pgfmanual /.expand once is explained in Section 55.4.6 "Expanded and Multiple Values". I thought I had read the pgfkeys chapter thoroughly, but obviously I missed that. Thanks! – Nicholas Hill Mar 16 '13 at 22:15

You have two to four choices (as \pgfmathresult is already fully expanded there is no different in all four).

  • The key handler /.expanded is like an \edef (recommended if you use it inside \pgfkeys{…}):

  • The key handlers /.expand once and /.expand twice do just enough \expandafters to expand the given value once or twice.

    \pgfkeys{/junk/.expand once=\pgfmathresult}
    \pgfkeys{/junk/.expand twice=\pgfmathresult}
  • I recommend the macro \pgfkeyslet if you do not need this in \pgfkeys.


    This simply performs a

    \expandafter\let\csname pgfk@/junk\endcsname\pgfmathresult

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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