Is it possible to store \pgfmathresult in a pgfkey?

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

    \documentclass{article}
\usepackage{tikz}
\begin{document}
\pgfkeys{/junk/.initial=0}

\pgfmathparse{5+10}
\pgfkeys{/junk=\pgfmathresult}

junk = \pgfkeysvalueof{/junk}\par

\pgfmathparse{8*5}

% 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

\end{document}


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

\pgfmathsetmacro{\tempmacro}{5+30}
\pgfkeys{/junk=\tempmacro}


but if I use \tempmacro later on for something else

\pgfmathsetmacro{\tempmacro}{8*5}


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{…}):

\pgfkeys{/junk/.expanded=\pgfmathresult}

• 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.

\pgfkeyslet{/junk}\pgfmathresult


This simply performs a

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