Monday, 27 May 2013

Expansion problems with pgfkeys

Expansion problems with pgfkeys

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

No comments:

Post a Comment