Well, I'm not going to give an answer to my question, but just practical examples to observe the behaviour of the key handlers .get
and .store in
. Your answers helped me a lot but I needed a good night of sleep to set things in order and these examples to be able to try different configurations.
Since setting an initial value (handler .initial
) and a default value (handler .default
) is often a related problem, I also want to write here some details I've gathered:
- if there is a default value:
pgfkeys{a}
will set the default value to the key a
- if there is no default value:
pgfkeys{a}
displays the value of the key a
Examples with the .get
handler
I have used a default value in the examples of this section to avoid displaying twice the key a
when calling \DisplayKey{a}
.
Each time, I've generated two outputs: the one on the right corresponds to the case where an initial value of 0 is used for the key a
.
Example 1
\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{pgfkeys}
\pgfkeys{/a/.default=1}
\pgfkeys{/a/.initial=0} % uncomment for image on the right
\newcommand{\DisplayKey}[1]{%
\pgfkeys{/a/.get=\aKey}% .get before setting key a
\pgfkeys{#1}%
\aKey}%
\begin{document}
a / value stored in \textbackslash aKey\par
? / \DisplayKey{}\par
1 / \DisplayKey{a}\par
2 / \DisplayKey{a=2}\par
3 / \DisplayKey{a=3}\par
4 / \DisplayKey{a=4}\par
\end{document}

Example 2
\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{pgfkeys}
\pgfkeys{/a/.default=1}
\pgfkeys{/a/.initial=0} % uncomment for image on the right
\newcommand{\DisplayKey}[1]{%
\pgfkeys{#1}%
\pgfkeys{/a/.get=\aKey}% .get after setting key a
\aKey}%
\begin{document}
a / value stored in \textbackslash aKey\par
? / \DisplayKey{}\par
1 / \DisplayKey{a}\par
2 / \DisplayKey{a=2}\par
3 / \DisplayKey{a=3}\par
4 / \DisplayKey{a=4}\par
\end{document}

Example 3
\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{pgfkeys}
\pgfkeys{/a/.default=1}
\pgfkeys{/a/.initial=0} % uncomment for image on the right
\pgfkeys{/a/.get=\aKey}% .get before every setting operation to the key
\newcommand{\DisplayKey}[1]{%
\pgfkeys{#1}%
\aKey}%
\begin{document}
a / value stored in \textbackslash aKey\par
? / \DisplayKey{}\par
1 / \DisplayKey{a}\par
2 / \DisplayKey{a=2}\par
3 / \DisplayKey{a=3}\par
4 / \DisplayKey{a=4}\par
\end{document}

Examples with the .store in
handler
I have used an initial value of 0 in every examples of this section. Indeed, the macro \aKey
doesn't exist until a
is set for the first time (\DisplayKey{}
doesn't set a
but asks for \aKey
so it needs to be defined before).
Each time, I've generated two outputs: the one on the right corresponds to the case where an default value of 1 is used for the key a
.
Example 4
\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{pgfkeys}
\pgfkeys{
/a/.default=1, % uncomment to get image on the right
/a/.store in=\aKey, % .store in at the beginning before any setting operation to the key
a=0} % set a, calls .store in and defines \aKey (otherwise \DisplayKey{} would produce an error)
\newcommand{\DisplayKey}[1]{%
\pgfkeys{#1}%
\aKey}%
\begin{document}
a / value stored in \textbackslash aKey\par
? / \DisplayKey{}\par
1 / \DisplayKey{a}\par
2 / \DisplayKey{a=2}\par
3 / \DisplayKey{a=3}\par
4 / \DisplayKey{a=4}\par
\end{document}

Example 5 (bad)
And here is what I was doing...
\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{pgfkeys}
\pgfkeys{
/a/.default=1, % uncomment to get image on the right
/a/.initial=0,
/a/.get=\aKey, % if you remove it, \DisplayKey{} generates an error (\aKey not defined)
/a/.store in=\aKey} % \aKey will be redefined each time a is set
\newcommand{\DisplayKey}[1]{%
\pgfkeys{#1}%
\aKey}%
\begin{document}
a / value stored in \textbackslash aKey\par
? / \DisplayKey{}\par
1 / \DisplayKey{a}\par
2 / \DisplayKey{a=2}\par
3 / \DisplayKey{a=3}\par
4 / \DisplayKey{a=4}\par
\end{document}

Bonus
I also want to mention another solution which makes use of the handler .code
and the macros \pgfkeyssetvalue
and \pgfkeysgetvalue
. Indeed, it makes things easy to test weather a key has been set.
\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{pgfkeys}
\pgfkeys{
/a/.default=1, % uncomment to get image on the right
/a/.code={\pgfkeyssetvalue{a}{#1}\pgfkeysgetvalue{a}{\aKey}},
a=0}
\newcommand{\DisplayKey}[1]{%
\pgfkeys{#1}%
\aKey}%
\begin{document}
a / value stored in \textbackslash aKey\par
? / \DisplayKey{}\par
1 / \DisplayKey{a}\par
2 / \DisplayKey{a=2}\par
3 / \DisplayKey{a=3}\par
4 / \DisplayKey{a=4}\par
\end{document}

.initial
is useless here but my point is that I need to use.get
and.store in
together if I want to be able to store the value..get
and.initial
: Complex objects in TikZ: pgfkeys scope and best practice