4

I'm trying to create a database using pgfkeys. It is working rather well, until I needed to execute some parameters stored in a key and pass it to some command that uses xkeyvalue to parse the parameters.

However, when that happens, the xkeyval package can't find the string that I am passing through the pgfkeysvalue.

I am using todonotes to show what is going on, to simplify the problem. But, I am using other package that is rather complicated. But the error is the same.

\documentclass{article}
\usepackage{pgffor,pgfkeys}
\usepackage{todonotes}

\newcommand{\foo}[3][]{\missingfigure[#1]{#2, #3}}
\newcommand{\bas}[3][]{mand: #2, #3\\}

\pgfkeys{/test/.cd,
  section/.code = {\section{#1}},
  type/.initial = bas,
  options/.initial=,
  question/.code 2 args = {\csname \pgfkeysvalueof{/test/type}\endcsname[\pgfkeysvalueof{/test/options}]{#1}{#2}},
  style/.estyle={#1}
}


\def\allrecords{%
section=first,
{
  type = foo,
  question={key1}{my lng question},
},
{
  type = foo,
  options = {figwidth=8cm},% error happens here
  question={key1}{my lng question},
},
}

\pagestyle{empty}
\begin{document}
\foreach \x in \allrecords {\pgfkeys{/test/.cd,style/.expanded=\x}}

\end{document}

1 Answer 1

6

The key value parser sees \pgfkeysvalueof{/test/options} as option list. Since there is no equal sign or comma, it is interpreted as key only. The example is fixed by expanding the option list first, see the \expandafter in the example which expands \romannumeral-`\x, which then triggers the expansion of \pgfkeysvalueof{/test/options} until \romannumeral sees an unexpandable token or a space (which is gobbled). This way, the exact number of \expandafter does not need to be known.

\documentclass{article}
\usepackage{pgffor,pgfkeys}
\usepackage{todonotes}

\newcommand{\foo}[3][]{\missingfigure[#1]{#2, #3}}
\newcommand{\bas}[3][]{mand: #2, #3\\}

\pgfkeys{/test/.cd,
  section/.code = {\section{#1}},
  type/.initial = bas,
  options/.initial=,
  question/.code 2 args = {%
    \csname\pgfkeysvalueof{/test/type}\expandafter\endcsname
    \expandafter[\expandafter{%
      \romannumeral-`\x\pgfkeysvalueof{/test/options}%
    }]{#1}{#2}},
  style/.estyle={#1}
}


\def\allrecords{%
section=first,
{
  type = foo,
  question={key1}{my lng question},
},
{
  type = foo,
  options = {figwidth=8cm},% error happens here
  question={key1}{my lng question},
},
}

\pagestyle{empty}
\begin{document}
\tracingmacros=1
\foreach \x in \allrecords {\pgfkeys{/test/.cd,style/.expanded=\x}}

\end{document}

Result

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .