2

Is there a general way to pass a <key list> contained inside a macro to the \pgfkeys command, in order to process it in the usual way? In simple examples, \expandafter helps but I do not really know how to go in more complicated situations. In particular, how should I do, if I want to pass to \pgfkeys some keys explicitly and some through a macro?

\documentclass[varwidth,border=1mm]{standalone}
\usepackage{pgf}
\def\x{g/a=1,g/b=2}
\def\y{a=3,b=4}
\pgfkeys{%
    /g/.is family, /g,
    a/.code={(a is #1)},
    b/.code={(b is #1)},
    c/.code={(c is #1)}
}

\begin{document}
    %\pgfkeys{\x}                                  % CLEARLY FAILING
    \expandafter\pgfkeys\expandafter{\x} \\
    %\pgfkeys{\x,g/c=7}                            % CLEARLY FAILING
    \expandafter\pgfkeys\expandafter{\x,g/c=7} \\
    %\pgfkeys{g,c=5,\y}                            % CLEARLY FAILING
    %\pgfqkeys{/g}{c=5,\y}                         % CLEARLY FAILING
    \pgfkeys{g,c=5,a=3,b=4} \\                 % <-- What I'd like to get from
    \pgfqkeys{/g}{c=5,a=3,b=4}                 % <-- the previous two lines
\end{document}

I stumbled on this aspect applying what described in the 86.6.3 Handlers For Unprocessed Keys section of the pgf manual, trying to process again with \pgfkeys the filtered out keys saved into the \remainingoptions macro.

  • Please add \usepackage{pgf} to your preamble. – user121799 Mar 14 at 15:56
  • 2
    You should use styles, not macros if you want to store list of keys. – Ulrike Fischer Mar 14 at 15:59
  • @UlrikeFischer Thanks for the comment, in general is what I try to do. But as I wrote at the end of the question, it looks like sometimes one has a key list in a macro. This is e.g. exactly what the /pgf/key filter handlers/append filtered to={<macro>} handler does. And I would like to process then those keys via \pgfkeys. – Axel Krypton Mar 14 at 16:11
3

You can process the keys from a macro with \pgfkeysalsofrom:

\documentclass[varwidth,border=1mm]{standalone}
\usepackage{pgf}
\def\x{g/a=1,g/b=2}
\def\y{a=3,b=4}
\pgfkeys{%
    /g/.is family, /g,
    a/.code={(a is #1)},
    b/.code={(b is #1)},
    c/.code={(c is #1)}
}

\begin{document}
    \pgfkeysalsofrom{\x}    
\end{document}

But you shouldn't try to mix macros and normal keys in one \pgfkeys-argument. That's not a good programming style if you need this. You normally get macros with list of keys from some filtering and the code then should have enough control on the processing to avoid such a mix.

  • I really missed \pgfkeysalsofrom in the manual, although it was just in the section before (86.6.2 Setting Filters). I am sorry for that. The fact that this command exists makes me agree even more with what you wrote. I was not happy of mixing macros and normal keys but I was not understanding the way out there. I am much happier now. :) – Axel Krypton Mar 14 at 16:37
1

Something like this?

\documentclass[varwidth,border=1mm]{standalone}
\usepackage{pgf}
\def\x{g/a=1,g/b=2}
\def\y{a=3,b=4}
\pgfkeys{%
    /g/.is family, /g,
    a/.code={(a is #1)},
    b/.code={(b is #1)},
    c/.code={(c is #1)}
}

\begin{document}
    %\pgfkeys{\x}                                  % CLEARLY FAILING
    %\expandafter\pgfkeys\expandafter{\x} \\
    %\pgfkeys{\x,g/c=7}                            % CLEARLY FAILING
    \edef\temp{\noexpand\pgfkeys\expandafter{\x,g/c=7}}\temp \\
    %\pgfkeys{g,c=5,\y}                            % CLEARLY FAILING
    %\pgfqkeys{/g}{c=5,\y}                         % CLEARLY FAILING
    \pgfkeys{g,c=5,a=3,b=4} \\                 % <-- What I'd like to get from
    \pgfqkeys{/g}{c=5,a=3,b=4}                 % <-- the previous two lines
\end{document}

enter image description here

Or

\documentclass[varwidth,border=1mm]{standalone}
\usepackage{pgf}
\def\x{g/a=1,g/b=2}
\def\y{a=3,b=4}
\pgfkeys{%
    /g/.is family, /g,
    a/.code={(a is #1)},
    b/.code={(b is #1)},
    c/.code={(c is #1)}
}

\begin{document}
    %\pgfkeys{\x}                                  % CLEARLY FAILING
    %\expandafter\pgfkeys\expandafter{\x} \\
    %\pgfkeys{\x,g/c=7}                            % CLEARLY FAILING
    \edef\temp{\noexpand\pgfkeys\expandafter{\x,g/c=7}}\temp \\
    \edef\temp{\noexpand\pgfkeys{g,c=5,\y} }\temp 
    %\pgfqkeys{/g}{c=5,\y}                         % CLEARLY FAILING
    %\pgfkeys{g,c=5,a=3,b=4} \\                 % <-- What I'd like to get from
    %\pgfqkeys{/g}{c=5,a=3,b=4}                 % <-- the previous two lines
\end{document}

enter image description here

  • that's rather dangerous. You are assuming that all the key values are okay in a \edef. – Ulrike Fischer Mar 14 at 16:03
  • @UlrikeFischer Yes, I am assuming this because they are in the MWE. – user121799 Mar 14 at 16:04
  • @UlrikeFischer what do you mean exactly? Which would it be a non okay value? Which is the danger then? I decided to ask this question to try to learn a general approach in similar cases and I would be interested in drawbacks of marmot's answer, if any exists. – Axel Krypton Mar 14 at 16:15

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.