I try to write a command that accepts options, using pgfkeys. Unknown keys are collected in a style, that I try to use as global options for a tikzpicture environement. A minimal example would be:
\documentclass{article}
\usepackage{tikz}
\newif\ifsquare
\pgfqkeys{/test}{%
square/.is if=square,
square/.default=true,
% collect unknown keys in style 'remainingkeys':
.unknown/.code={%
\let\currname\pgfkeyscurrentname%
\let\currval\pgfkeyscurrentvalue%
\ifx#1\pgfkeysnovalue%
\pgfqkeys{/tikz}{remainingkeys/.append style={\currname}}%
\else%
\pgfqkeys{/tikz}{remainingkeys/.append style={\currname=\currval}}%
\fi%
}%
}
\newcommand\myfigure[1][]{
% initialize:
\pgfqkeys{/tikz}{remainingkeys/.style={}}
\pgfqkeys{/test}{square}
% set user keys:
\pgfqkeys{/test}{#1}
\begin{tikzpicture}[/tikz/remainingkeys]
\ifsquare
\fill (0,0)rectangle(1,1);
\else
\fill (0,0)rectangle(1.2,.8);
\fi
\end{tikzpicture}
}
\begin{document}
\myfigure[square,rotate=45] % rotated 45 degs; ok
\myfigure[square,fill=red] % red; ok
\myfigure[square,rotate=45,fill=red] % red, but doesn't rotate
\myfigure[square=false] % not square
\myfigure[square=false,fill=red,rotate=45] % not square, not red and rotated 90 degs!
\end{document}
The command \myfigure simply draws a square or rectangle, depending on the value of the 'square'-key. This key is handled correctly. All other options are handled by the .unknown/.code handler and collected into the style 'remainingkeys', which eventually is passed to \begin{tikzpicture}. The first call of \myfigure indeed draws a square, and rotates it, but the third call doesn't! Somehow not all unknown keys are stored in 'remainingkeys'. In the fifth call even the rotation angle isn't right.
I tried a lot of things, but this is the best that I can come up with, at least without producing any TeX-errors. I'm doing something seriously wrong, but what? I'm dead in the water...
`
to mark your inline code.