10

Why is the parameter value not being passed to my code for the unknown key value here?

\documentclass{article}
\usepackage{pgfkeys}

\makeatletter
\def\ae@size{2cm}
\pgfkeys{/ae/example/.cd,
  size/.is choice,
  size/small/.code=\def\ae@size{I am small},
  size/medium/.code=\def\ae@size{I am medium},
  size/large/.code=\def\ae@size{I am large},
  size/.unknown/.code={\typeout{UNKNOWN  ----> #1}\def\ae@size{I am customized to#1}},
}
\newcommand\problem[1]{%%
  \pgfkeys{/ae/example/.cd,#1}%%
  \ae@size  
  \par
}
\makeatother
\begin{document}

\problem{size=small}
\problem{size=medium}
\problem{size=large}
\problem{size=a different size}

\end{document}

enter image description here

  • @Qrrbrbirlbel Seems to work without expansion. Why the recommendation to also expand? – A.Ellett Dec 14 '13 at 19:51
13

The .is choice handler basically does two things.

In your case, it setups the size key as if you would do:

size/.style={/ae/example/size/#1}

However, before that style (size/#1) is executed, the argument #1 is saved in \pgfkeys@was@choice.

This macro \pgfkeys@was@choice is then used in the definition of the size/.unknown code to give a proper error message.

You can overwrite that .unknown handler to do things, of course. In this case, you can use \pgfkeys@was@choice. You can also use \pgfkeyscurrentname.

The difference can be seen, if one misuses a .is choice key by using something like:

size={unknown=arg}

The macro \pgfkeyscurrentname would give unknown (just the key name), \pgfkeys@was@choice would expand to unknown=arg. The #1 you used in your definition of the .unknown code would be arg then.

As key names can only consist of expandable content, you can safely \edef \pgfkeys@was@choice or \pgfkeyscurrentname. This also means, you cannot do size=a \emph{different} size. (The possible, unusual #1 may contain anything, of course, but I am using \edef on that in this example anyway.)

Code

\documentclass{article}
\usepackage{pgfkeys}
\makeatletter
\def\ae@size{2cm}
\pgfkeys{/ae/example/.cd,
  size/.is choice,
  size/small/.code=\def\ae@size{I am small},
  size/medium/.code=\def\ae@size{I am medium},
  size/large/.code=\def\ae@size{I am large},
  size/.unknown/.code=%
    \edef\ae@size{I am customized to ``\pgfkeys@was@choice''. Argument: ``#1''}}
\newcommand\problem[1]{%%
  \pgfkeys{/ae/example/.cd,#1,because/.code=}%%
  \ae@size\par}
\makeatother
\begin{document}
\problem{size=small}
\problem{size=medium}
\problem{size=large}
\problem{size=a different size}
\problem{size={Where is my=argument?}}
\end{document}

Output

enter image description here

| improve this answer | |

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.