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}
\noexpand
before the\sqrt
, but I guess that's not the solution you're looking for.\def
should suffice. Then there also won't be an error with$2+\srqt{2}$
\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.nodes
is expanded, notlabel
.pgfkeys
was so that you could use existing names likelabel
for your keys!