3

I want to pass a key=value pair as the value of another key=value pair. The problem is, that the equal sign gets interpreted by pgfkeys and thus I need to encapsulate it using curly brackets, which in turn prevent pgfkeys to interpret the equal sign (surprise).

The following minimal example doesn't make sense, but it illustrates what I'm trying to do. In reality I try to pass on the key=value pair to another command, which takes key=value pairs.

\documentclass[a4paper]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage{pgfkeys}

\begin{document}

\newcommand{\mycmd}[1]{%
    \pgfkeys{/mykey/.initial=hello}%
    \pgfkeys{/mykey2/.initial=world}%
    \pgfkeys{#1}%
    \pgfkeys{\pgfkeysvalueof{/mykey2}}%
    \pgfkeysvalueof{/mykey}%
    \pgfkeysvalueof{/mykey2}%
}

\mycmd{mykey2={mykey=test}}.

\end{document}

The error I get is of course: "I do not know the key '\mykey=test' ...". How can I solve this?

Edit:

Thanks to percusse I now use a style for what I'm doing. In the example below mycmdA gives the error 'I do not know the key '/fill/', while mycmdB works. Does anybody know why?

\documentclass[a4paper]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage{pgfkeys}

\begin{document}

This is a test

\newcommand{\mycmdA}[1]{%
    \pgfkeys{/mystyle/.style={##1}}%
    \pgfkeys{#1}%
}

\newcommand{\mycmdB}[1]{%
    \pgfkeys{/mystyle/.style={}}%
    \pgfkeys{/mystyle/.code=\pgfkeys{/style/.style={##1}}}
    \pgfkeys{#1}%
}

\mycmdA{mystyle={fill=red}}
\mycmdB{mystyle={fill=red}}

\end{document}
  • I think that the first line in \mycmdA is equivalent to\pgfkeys{/fill=red}, which will fail because the key /fill has not been defined. I don't understand what you are trying to achieve but perhaps you want \pgfkeys{/mystyle/.code=\pgfkeys{/mystyle/##1}} or \pgfkeys{/mystyle/.cd, /mystyle/.style={##1}} ? The later will still fail because /mystyle/fill has not been set but at least the error is in the right pgfkeys "directory" – Andrew May 4 '17 at 11:48
4

You don't need to reinvent the wheel. Use pgfkeys machinery for /.style and /.code handlers just as in the TikZ pictures. If mykey2 is going to be a key setter then it wouldn't be fun to also have a value. That part of the key mechanism is fairly nontrivial. But this will print test (expansion is not necessary here but just in case you need it)

\pgfkeys{mykey/.initial=hello}%
%\pgfkeys{mykey2/.initial=world}%
\pgfkeys{mykey2/.code={\expandafter\pgfkeys\expandafter{#1}}}% Expand once to remove braces
\pgfkeys{mykey2={mykey=test}}%
\pgfkeysvalueof{/mykey}%

you can instead make mykey2 a /.style and pass options to it as a key holder. Then whenever you execute mykey2 it will execute all of the stored key values (/.append style is needed for adding without resetting).

  • Thanks, but how would I use /.style. Sorry, I'm very new to pgfkeys. – Thomas Leitz May 3 '17 at 11:36
  • More specifically: What does "execute mykey2" mean in this context? I also read this in the pgf documentation, but I don't know what it means. – Thomas Leitz May 3 '17 at 12:11

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.