如何获取 \pgfdeclareshape 中键的正确值

如何获取 \pgfdeclareshape 中键的正确值

以下是一段测试代码。日志文件中的输出为

outside test: initial
(../output/test.aux) ABD: EveryShipout initializing macros
Run number 1 of rule 'xelatex'
savedanchor test: changed
saveddimen test: changed
anchor test: initial
\documentclass[tikz]{standalone}
\pgfkeys{
  test/.store in=\test,
  test=initial,
}
\pgfdeclareshape{myshape}{
  \inheritsavedanchors[from=rectangle]
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \savedanchor{\anyanchor}{
    \typeout{savedanchor test: \test}
  }
  \saveddimen{\anydimen}{
    \typeout{saveddimen test: \test}
  }
  \typeout{outside test: \test}
  \anchor{any}{
    \typeout{anchor test: \test}
  }
}
\tikzset{
  base/.code={
    \tikzset{/.cd, #1}
    \pgfkeysalso{myshape}
  }
}

\begin{document}
\begin{tikzpicture}
  \node[base={test=changed}] (a){};
  \node at (a.any) {};
\end{tikzpicture}
\end{document}

我怎样才能使所有输出成为changed

答案1

这里有一些可以用来回答这个问题的东西。我不知道它是否能让你实现你的最终目标。我建议如下:你可以将的本地值存储test在一个包含节点名称的宏中,这样我们就可以将此值与该特定节点关联起来。当我们从外部调用锚点时,我们会使用原始节点的名称来调用此宏,该节点存储在\pgfreferencednodename(参见例如这里)。

\documentclass[tikz]{standalone}
\pgfkeys{
  test/.store in=\test,
  test=initial,
}
\makeatletter
\pgfdeclareshape{myshape}{
  \inheritsavedanchors[from=rectangle]
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \savedanchor{\anyanchor}{
    \typeout{savedanchor test: \test}
    \expandafter\xdef\csname tikz@td@test@\tikz@fig@name\endcsname{\test}
  }
  \saveddimen{\anydimen}{
    \typeout{saveddimen test: \test}
  }
  \typeout{outside test: \test}
  \anchor{any}{
    \anyanchor
    \edef\localtest{\csname tikz@td@test@\pgfreferencednodename\endcsname}
    \typeout{anchor test: \localtest}
  }
}
\tikzset{
  base/.code={
    \pgfkeysalso{myshape}
    \tikzset{/.cd, #1}
  }
}
\makeatother
\begin{document}
\begin{tikzpicture}
  \node[base={test=changed}] (a){};
  \node[base={test=changed}] (b) at (1,0) {};
  \node at (a.any) {};
\end{tikzpicture}
\end{document}

现在输出是

 savedanchor test: changed 
 saveddimen test: changed 
 savedanchor test: changed 
 saveddimen test: changed 
 anchor test: changed

所以一切都是一致的。(在某种程度上,这提供了在节点中存储任何东西的惊人可能性,例如它的颜色。这无疑为所有其他制作 Ti 的方式提供了新的选择Z 更适合 3D。因此谢谢提出这个好问题!!!;-)

相关内容