pgfkeys 的扩展问题

pgfkeys 的扩展问题

我正在使用pgfkeys,以及一种相当大胆的语法,其中一些键的值包含额外的键/值对。(例如,键的值nodes是一个对的列表,而每对的第二个组件是一个键值列表。)

每当我在密钥中放入任何过于花哨的东西时,我都会收到编译错误label。下面的代码按原样运行良好,但如果我用 替换2+2,它就会中断。我认为这是一个宏扩展问题。我该如何安排它,使密钥2+\sqrt{2}的内容label不是直到需要为止?

代码

\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}

输出

在此处输入图片描述

答案1

只是为了确认cgnieder 的评论Ryan Reich 的解释正中目标。我已经设法修复了我的代码,也理解了问题所在。谢谢大家!

\documentclass{article}
\usepackage{tikz}

\makeatletter

\pgfkeys{/wickerson/.cd,
  % The following two lines are from:
  % https://tex.stackexchange.com/q/85637/86
  execute style/.style = {#1},
  execute macro/.style = {execute style/.expand once=#1},
  left/.code=            {\def\wickerson@left{#1}},
  top/.code=             {\def\wickerson@top{#1}},
  label/.code=           {\def\wickerson@label{#1}},
  nodes/.code=           {\def\wickerson@nodes{#1}},
  colour/.code=          {\def\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={$\sqrt{2}+2$}}, %
    b/{left=15,top=10,label={$\log 4$}}%
  }%
]
\end{tikzpicture}

\end{document}

答案2

这是一个解决方案斯基瓦尔包裹。

\documentclass{article}
\usepackage{tikz}
\usepackage{skeyval}[2013/05/01]
\makeatletter
\directkeys{%
  .family=wickerson,
  .holder prefix=wic@,
  .define keys={%
    .cmd/left/0,
    .cmd/top/0,
    .cmd/label//,
    .cmd/colour/black,
    .zcmd/title colour/black,
    .ord/nodes//
      \skvifblankTF{#1}{}{%
        \node[text=\wic@titlecolour] at (0,0) {My Diagram ...};
        \edef\tempa{\unexpanded{#1}}%
        \skvstripouterbraces{1}\tempa
        \newforeach [expand list once] \x in \tempa {%
          % Use the parameter rep of \x:
          \skvsetkeys{wickerson}{##1}%
          \node[shape=circle,draw=\wic@colour] at (\wic@left,\wic@top){\wic@label};
        }%
      },
  },
  % Allow 'color' to be a clone of 'colour':
  .link=color/colour,
}
\newcommand\myDiagram[1][]{%
  \directkeys{%
    .family=wickerson,
    .set keys={nodes,left,top,label,colour,title colour},
    .set keys={#1}
  }%
}
\makeatother
\begin{document}
\begin{tikzpicture}[x=1mm,y=-1mm]
\myDiagram[%
  title colour=green,
  nodes={
    {color=red,left=0,top=10,label={$\sqrt{2}+2$}},
    {color=blue,left=15,top=10,label={$\log 4$}}
  }%
]
\end{tikzpicture}
\end{document}

注意:我没有本网站所坚持的 jpeg、png、tiff、gif 或 bmp 格式的输出。

相关内容