0

In this example .store and \pgfkeysvalueof never match, why ?

How modify the value of the key through tikzset command ?

\documentclass{article}
\usepackage{tikz}

\pgfkeys{/tikz/Cote/.cd,
    aspect/.default=o,
    aspect/.store in=\aspect,
    foo/.initial=a,
    foo/.store in=\foo,
} 

\begin{document}

% pgfkeysvalueof is empty - what .initial is for ? never appears
% \aspect not defined
\pgfkeysvalueof{/tikz/Cote/aspect} -- %\aspect

\tikzset{/tikz/Cote/aspect/.initial=m}

% pgfkeysvalueof is m
% \aspect not defined
\pgfkeysvalueof{/tikz/Cote/aspect} -- %\aspect

\tikzset{/tikz/Cote/aspect=bob}

% pgfkeysvalueof is m
% \aspect is bob
\pgfkeysvalueof{/tikz/Cote/aspect} -- \aspect

\pgfkeys{/tikz/Cote/.cd,
    aspect=p}

% pgfkeysvalueof is m
% \aspect not defined
\pgfkeysvalueof{/tikz/Cote/aspect} -- \aspect

% pgfkeysvalueof is a
% \foo not defined
\pgfkeysvalueof{/tikz/Cote/foo} -- %\foo

\tikzset{/tikz/Cote/foo=bob}
% pgfkeysvalueof is a
% \foo not defined
\pgfkeysvalueof{/tikz/Cote/foo} -- \foo

\end{document}
  • How is this different from your other question? At any rate, the answer is the same: you are mixing up different kinds of things. \pgfkeysvalueof can't be used to access a value stored using .store in, .initial cannot be used to set its initial value and .default cannot be used to set a default value. – cfr Feb 28 '18 at 23:25
  • Isn't this a duplicate of your earlier question, tex.stackexchange.com/questions/417773/…? – cfr Feb 28 '18 at 23:26
  • I guess the .initial thing is different: the .default thing seems to be the same. (But a comment there included the .initial case, even though the question didn't.) However, I'm not sure this is the difference you're getting at, as you include the .default case again here .... – cfr Feb 28 '18 at 23:50
1

As mentioned in responses to your earlier question, you are mixing up distinct kinds of things.

\tikzset{
  Cote/.cd,
  aspect/.store in=\aspect,
  aspect=beginning value of aspect,
}

simply stores the value passed to the key in the given macro. So \aspect retrieves beginning value of aspect. You cannot set the initial value using .initial or the default using .default. This is a different kind of thing. Personally, I almost always use .store in and almost never use .initial or .default, which I find much more cumbersome. (In L3, I use the equivalents all the time, however, because there it doesn't seem cumbersome.)

So, we want, say,

Then \verb|\aspect| will retrieve the \aspect.

key stores in macro

However,

\tikzset{
  Cote/.cd,
  aspect/.initial=initial value of aspect,
}

does something different. Here the value is stored in the key itself, so must be retrieved with \pgfkeysvalueof or some intermediate step must be used to put the value into a macro. Hence, we now want something like

Then \verb|\pgfkeysvalueof{/tikz/Cote/aspect}| will retrieve the \pgfkeysvalueof{/tikz/Cote/aspect}.

initial value

Finally,

\tikzset{
  Cote/.search also={/tikz},
  Cote/.cd,
  aspect/.style={draw=#1},
  aspect/.default=red,
}

means that aspect may be used with or without passing a value. If a value is passed, that value will be used. If aspect is used with no value, the default value of red will be used instead. So, we might have something like

\begin{tikzpicture}[line width=2pt]
  \draw (0,1) -- (1,1);
  \draw [Cote/aspect] (0,.5) -- (1,.5);
  \draw (0,0) -- (1,0);
  \draw [Cote/aspect=blue] (0,-.5) -- (1,-.5);
  \draw [Cote/aspect] (0,-1) -- (1,-1);
\end{tikzpicture}

key defaults

\documentclass{article}
\usepackage{tikz}

\begin{document}

\tikzset{
  Cote/.cd,
  aspect/.store in=\aspect,
  aspect=beginning value of aspect,
}

Then \verb|\aspect| will retrieve the \aspect.

\tikzset{
  Cote/.cd,
  aspect/.initial=initial value of aspect,
}

Then \verb|\pgfkeysvalueof{/tikz/Cote/aspect}| will retrieve the \pgfkeysvalueof{/tikz/Cote/aspect}.

\tikzset{
  Cote/.search also={/tikz},
  Cote/.cd,
  aspect/.style={draw=#1},
  aspect/.default=red,
}

\begin{tikzpicture}[line width=2pt]
  \draw (0,1) -- (1,1);
  \draw [Cote/aspect] (0,.5) -- (1,.5);
  \draw (0,0) -- (1,0);
  \draw [Cote/aspect=blue] (0,-.5) -- (1,-.5);
  \draw [Cote/aspect] (0,-1) -- (1,-1);
\end{tikzpicture}

\end{document}
  • Thank you for this explanation. I wonder why the key and the macro have different value. I understand that they are different things. But why the values are not update at the same time. What is the purpose keep them different. Is there a clever use of this dichotomy? – Tarass Mar 1 '18 at 7:12
  • @Tarass They are different kinds of things. It is like asking why some code isn't executed when .boolean is invoked: that key's a Boolean, so it doesn't execute code. There is no \aspect macro when you define the key using the other methods. And the value of the key is not the definition of the macro when you use .store in. – cfr Mar 1 '18 at 17:03
  • I clearly understand the mecanism and the difference between the macro and the key. I wonder why the key and the macro have different values. Is it on purpose and then in witch case is it useful? – Tarass Mar 1 '18 at 22:33
  • @Tarass There is no macro in the one case and the key value is not the macro content in the other. There are a bunch of different kinds of keys and these are three of them. You can't have the same key be two things (generally), so I'm not sure what you mean about 'the key and the macro' having different values. I don't know why you think both exist with different values: generally, they don't. And you certainly would not want a macro created with the key name every time you added a new key with .default or .initial. Different aspect keys might do different things and \aspect another. – cfr Mar 2 '18 at 1:03
  • When one say aspect=Bob if there is no macro the key take the value Bob. If there is a declared macro the macro take the value. On purpose because one has declared this macro. But key value is now ignored and is useless it never be affected by a aspect=foo. Could you explain me why it is better as this as in the contrary the key value could be updated at the same time as the macro. And vice versa if macro exist. Is there a clever use example that keeping them apart is useful o decisive? – Tarass Mar 2 '18 at 6:46

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.