I'm using pgfkeys, and a fairly adventurous syntax in which the values for some keys contain additional key/value pairs. (For instance, the value of the nodes key is a list of pairs, and the second component of each pair is a key-value list.)
I get a compilation error whenever I put anything too fancy into the label key. The code below works fine as-is, but if I replace 2+2 with 2+\sqrt{2}, it breaks. I think it's a macro-expansion problem. How can I arrange it that the contents of the label key does not get expanded until it needs to be?
Code
\documentclass{article}
\usepackage{tikz}
\makeatletter
\pgfkeys{/wickerson/.cd,
% The following two lines are from:
% http://tex.stackexchange.com/q/85637/86
execute style/.style = {#1},
execute macro/.style = {execute style/.expand once=#1},
left/.code= {\xdef\wickerson@left{#1}},
top/.code= {\xdef\wickerson@top{#1}},
label/.code= {\gdef\wickerson@label{#1}},
nodes/.code= {\xdef\wickerson@nodes{#1}},
colour/.code= {\xdef\wickerson@colour{#1}},
}
\newcommand\myDiagram[1][]{%
\pgfkeys{/wickerson/.cd,colour=black,nodes={},#1}
\node[text=\wickerson@colour] at (0,0) {My Diagram...};
\foreach \i/\values in \wickerson@nodes {
\pgfkeys{/wickerson/.cd,left=0,top=0,label={},%
execute macro=\values}
\node[shape=circle,draw=black]
at (\wickerson@left, \wickerson@top)
{\wickerson@label};
}
}
\makeatother
\begin{document}
\begin{tikzpicture}[x=1mm,y=-1mm]
\myDiagram[%
colour=red, %
nodes={%
a/{left=0,top=10,label={$2+2$}}, %
b/{left=15,top=10,label={$\log 4$}}%
}%
]
\end{tikzpicture}
\end{document}
Output

\noexpandbefore the\sqrt, but I guess that's not the solution you're looking for. – Jake May 27 '13 at 12:48\noexpandto everything in the label, so to speak. – John Wickerson May 27 '13 at 13:36\defshould suffice. Then there also won't be an error with$2+\srqt{2}$– cgnieder May 27 '13 at 15:47\edef\foo{\unexpanded{#1}}is slightly better than\def\foo{#1}, because it will correctly escape macro parameter characters#that are given inside the key, for even wilder labels. – Bruno Le Floch May 27 '13 at 15:51nodesis expanded, notlabel. – Ryan Reich May 27 '13 at 16:20wickersonfamily or changing directory is delayed. – percusse May 27 '13 at 17:13pgfkeyswas so that you could use existing names likelabelfor your keys! – John Wickerson May 27 '13 at 17:19pgfplotshas that feature too and sometimes it is really difficult to differentiate the TikZ family from thepgfplotsfamily especially with interoperation say drawing features and plot features. – percusse May 27 '13 at 18:39/.store inand/.estore inkeys to avoid those problems. – percusse May 27 '13 at 20:11/.code, it's a little less tedious work and easier to maintain. – percusse May 27 '13 at 20:14estore/storesyntax makes it a little clearer what one is trying to achieve. Thanks for the suggestion. By the way, I realise that I don't understand what you meant by your earlier comment about changing directory being delayed - could you say a little more about that? – John Wickerson May 28 '13 at 04:14