# Storing options for pgfkeys inside macro

I want to define some options as a list of key-value pairs inside a macro and use that macro inside a command that takes pgfkeys-style options. Here is a MWE:

\documentclass{standalone}
\usepackage{pgfkeys}

\pgfkeys{
/example/.is family, /example,
.unknown/.code = {
(\pgfkeyscurrentkeyRAW,#1)
\typeout{DEBUG \pgfkeyscurrentkeyRAW}
}
}

\newcommand{\defvalues}[2][]{
\pgfkeys{/example, #1}
}

\begin{document}

\def\mylist{c=3,d=4}

\defvalues[a=1,b=2]{}

\expandafter\defvalues\expandafter[\mylist]{}

\expandafter\defvalues\expandafter[f=6,\mylist]{}

\end{document}


In the last line, \mylist is not expanded correctly.

Expected output in the document:

(a,1) (b,2) (c,3) (d,4) (f,6) (c,3) (d,4)


Actual output:

(a,1) (b,2) (c,3) (d,4) (f,6) (c=3,d=4)


Alternatively, when running pdflatex test.tex|grep DEBUG, expected output:

DEBUG a
DEBUG b
DEBUG c
DEBUG d
DEBUG f
DEBUG c
DEBUG d


Actual output:

DEBUG a
DEBUG b
DEBUG c
DEBUG d
DEBUG f
DEBUG c=3,d=4


How do I get \mylist to expand before it is passed to pgfkeys?

• You should use the .style handler to define your list: \pgfkeys{/example/mylist/.style={a=1,b=2}}. – Paul Gaborit Nov 5 '18 at 12:16
• Does that mean I need to know the name of the macro (in this case \mylist) in advance? I'd like to provide everything you see in the preamble as a package. – Volker Nov 5 '18 at 12:58

Here is a solution using the .style handler to define two lists.

\documentclass{article}
\usepackage{pgfkeys}

\pgfkeys{
/example/.is family,
/example,
.unknown/.code = {
(\pgfkeyscurrentkeyRAW,#1)
\typeout{DEBUG \pgfkeyscurrentkeyRAW}
}
}

\newcommand{\defvalues}[2][]{\pgfkeys{/example,#1}}
\def\mylist{c=3,d=4}
\defvalues[mylist/.style/.expand once={\mylist}]{}
\defvalues[mylist2/.style={g=8,h=45}]{}

\begin{document}
\defvalues[a=1,b=2]{}

\defvalues[mylist]{}

\defvalues[f=6,mylist,mylist2]{}
\end{document}


Output:

(a,1) (b,2)
(c,3) (d,4)
(f,6) (c,3) (d,4) (g,8) (h,45)


I found a solution:

\newcommand{\defvalues}[2][]{
\edef\opts{/example, #1}
\expandafter\pgfkeys\expandafter{\opts}
}


This works as expected. I guess it has to do with the fact that \expandafter doesn't work on [] the way it does on {}. Or rather, in the third case, it's only expanding f, not the entire contents of the brackets.