13

I'm trying to use pgfkeys to define commands with custom parameters, but I can't even get the example in pgfmanual to run:

\documentclass{article}
\usepackage{pgfkeys}
\begin{document}
    \pgfkeys{/my key=hallo,/your keys/main key=something\strange,key name without path=something else}
\end{document}

! Package pgfkeys Error: I do not know the key '/my key', to which you passed '
hallo', and I am going to ignore it. Perhaps you misspelled it.

See the pgfkeys package documentation for explanation.
Type  H <return>  for immediate help.
...                                              

l.4 ...range,key name without path=something else}


! Package pgfkeys Error: I do not know the key '/your keys/main key', to which 
you passed 'something\strange ', and I am going to ignore it. Perhaps you missp
elled it.

See the pgfkeys package documentation for explanation.
Type  H <return>  for immediate help.
...                                              

l.4 ...range,key name without path=something else}


! Package pgfkeys Error: I do not know the key '/key name without path', to whi
ch you passed 'something else', and I am going to ignore it. Perhaps you misspe
lled it.

See the pgfkeys package documentation for explanation.
Type  H <return>  for immediate help

What's wrong here? How can I set keys? I tried it both with PGF 2.10 and 3.0 on Centos.

Edit: I forgot to say that I did get the second example to work:

    \pgfkeys{/my key/.code=The value is ’#1’.}
    \pgfkeys{/my key=hi!}

But that is more than I want. The pgfmanual says "Typically (but not necessarily) some code is associated with a key." I don't want to add any code, I just want to set the keys and read them later. I suspect that something else is going wrong here.

Turion
  • 4,622
  • 2
    The first code is an example of how to use keys, but not how to define them: that's why pgfkeys complains that it does not know the key. If you want to use keys to store values, use \pgfkeys{/my key/.initial=hallo}. You can then access the value of the key using \pgfkeysvalueof{/my key}. – Jake Feb 03 '14 at 13:33
  • That's exactly what I was looking for, thanks a lot! Make an answer out of it and I'll accept it. – Turion Feb 03 '14 at 13:36

2 Answers2

14

The first code is an example of how to use keys, but not how to define them: that's why pgfkeys complains that it does not know the key. If you want to use keys to store values, use \pgfkeys{/my key/.initial=hallo} to initialise the key. You can then access the value of the key using \pgfkeysvalueof{/my key}.

To assign the key a different value later on, you can simply set it using \pgfkeys{/my key=bye}.

\documentclass{article}
\usepackage{pgfkeys}
\begin{document}
\pgfkeys{/my key/.initial=hallo}
\pgfkeysvalueof{/my key}

\pgfkeys{/my key=bye}
\pgfkeysvalueof{/my key}
\end{document}
Jake
  • 232,450
8

The first "example" is not a complete example. It only shows a use case for \pgfkeys.

Try the next example, that gives a definition for the key /my key and uses it:

\documentclass{article}
\usepackage{pgfkeys}
\begin{document}
  \pgfkeys{/my key/.code=The value is '#1'.}
  \pgfkeys{/my key=hi!}
\end{document}

Result

Heiko Oberdiek
  • 271,626