# pgfkeys: Store unknown keys in a command

Does pgfkeys have some some predefined handler to store unknown keys and their values in a command as a list? E.g. in the following example I would like to get a command which contains blub=1, bla=\textbf{2}. (I want to pass the unknown keys to \includegraphics).

\documentclass{article}
\usepackage{pgfkeys}

\pgfqkeys{/my key}{
x/.initial={0.5},
.unknown/.code={\pgfkeyscurrentname=\pgfkeyscurrentvalue}}

\begin{document}

\pgfkeys{/my key/.cd, x=2, blub=1, bla=\textbf{2}}
\end{document}

• Does there exist a pre-defined list of “unknown” keys that are allowed for storing in that list command? Other than that you might just add (expanded once) \pgfkeyscurrentname and \pgfkeyscurrentvalue to that list? Of course, any misspelled or really unknown key will be forwarded too. – Qrrbrbirlbel Jun 19 '13 at 16:58
• @Qrrbrbirlbel: I simply want to forward every unknown key to \includegraphics. If the list contains a key which \includegraphics doesn't know it should give the normal error messages. With xkeyval I would use \XKV@rm. – Ulrike Fischer Jun 19 '13 at 17:04

Maybe something along these lines?

The handler .unknown to list installs an .unknown/.code key that appends the unknown key and possibly its given value (enclosed in { }) to the list #1. An additional .@clear list key is used to clear the list #1, though if the keys are executed inside a group (as with TikZ on paths) there shouldn’t be much use for it, I guess.

The \pgfutil@gobble (which apparently isn’t available with pgfkeys) removes the first , from the list. (Of course, is the solution is only intended for LaTeX, the usual \@gobble can be used anyway.)

The amount of \expandafters simulates a \expandonce/\appto combo (etoolbox) because we can’t assume everything is expandable (and should be expanded at all).

## Code

\documentclass{article}
\usepackage{pgfkeys}
\makeatletter
\def\pgfutil@gobble#1{}
\pgfkeys{/handlers/.unknown to list/.code=%
\let#1\pgfutil@gobble
\pgfkeysalso{% or \pgfkeys{\pgfkeyscurrentpath/.cd,
.unknown/.code=% how to handle the difference between 'key' and 'key=value'?
\ifx\pgfkeyscurrentvalue\pgfkeysnovalue
\let\pgfkeys@temp\pgfkeyscurrentname
\else
\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\pgfkeys@temp\expandafter\expandafter\expandafter{\expandafter\pgfkeyscurrentname\expandafter=\expandafter{\pgfkeyscurrentvalue}}%
\fi
\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter#1\expandafter\expandafter\expandafter{\expandafter#1\expandafter,\pgfkeys@temp},%
.@clear list/.code=\let#1\pgfutil@gobble}%
}
\makeatother

\pgfkeys{/my key/.cd,
x/.initial={0.5},
.unknown to list=\myList
}
\begin{document}
\pgfkeys{
/my key/.cd,
x=2,
blub=1,
bla=\textbf{2},
dingenskirchen=something else
}

\meaning\myList

\pgfkeys{/my key/.@clear list}

\meaning\myList
\end{document}


## Output

• This works and give the expected output. In the meantime I also found a solution using filtered keys but I don't like it (to much install filter and handlers involved). I think I will revert to my other idea: Giving the keys not known to includegraphics some default meaning. – Ulrike Fischer Jun 20 '13 at 15:10