[Tex/LaTex] Undefined control sequence \pgfmath@

errorsexpansionmacrospgfkeyspgfmath

Why does the following code give me this error message?

Error

ERROR: Undefined control sequence.

— TeX said —
\pgfmath@dimen@ …men@@ #1=0.0pt\relax \pgfmath@

l.28 \draw (\getWickersonLeft{c},0)
— (\getWickersonRight{c},5);

Code

\documentclass{article}

\usepackage{tikz}

\pgfkeys{/wickersons/.cd,
  execute style/.style={#1},                           
  execute macro/.style={execute style/.expand once=#1}, 
  left/.code={\gdef\wickersonsleft{#1}},
  right/.code={\gdef\wickersonsright{#1}}
}

\def\getWickersonLeft#1{\csname wickerson#1left\endcsname}
\def\getWickersonRight#1{\csname wickerson#1right\endcsname}

\newcommand*\defWickersonMacros[3]{%
  \expandafter\edef\csname wickerson#1left\endcsname{#2}
  \expandafter\edef\csname wickerson#1right\endcsname{#3}
}

\begin{document}

\begin{tikzpicture}[x=1mm,y=-1mm]%
  \foreach \x/\xvalue in {c/{left=67,right=77}} { 
    \pgfkeys{/wickersons/.cd, execute macro=\xvalue} 
    \expandafter\defWickersonMacros{c}{\wickersonsleft}{\wickersonsright}
  }
  \draw (\getWickersonLeft{c},0) -- (\getWickersonRight{c},5);
\end{tikzpicture}

\end{document}

Best Answer

I would instead use the \pgfkeysvalueof way of defining things. This is a very convoluted way of avoiding the real power of pgfkeys because instead of keys you are using dummy macros. Consider sending all your options to some keys belonging to a family.

Here the problem is that the argument of a macro is used inside another definition so # chars should be doubled.

\documentclass{article}

\usepackage{tikz}

\pgfkeys{/wickersons/.cd,
  execute style/.style={#1},                           
  execute macro/.style={execute style/.expand once=#1}, 
  left/.code={\xdef\wickersonsleft{#1}},
  right/.code={\xdef\wickersonsright{#1}}
}

\edef\getWickersonLeft#1{\csname wickerson#1left\endcsname}
\edef\getWickersonRight#1{\csname wickerson#1right\endcsname}

\newcommand*\defWickersonMacros[3]{%
  \expandafter\xdef\csname wickerson##1left\endcsname{#2}% Double ##
  \expandafter\xdef\csname wickerson##1right\endcsname{#3}% Double ##
}

\begin{document}

\begin{tikzpicture}[x=1mm,y=-1mm]%
  \foreach \x/\xvalue in {c/{left=67,right=77}} { 
    \pgfkeys{/wickersons/.cd, execute macro=\xvalue} 
    \defWickersonMacros{c}{\wickersonsleft}{\wickersonsright}
  }

\draw (\getWickersonLeft{c},0) -- (\getWickersonRight{c},5);
\end{tikzpicture}

\end{document}

The output :)

enter image description here