every node
测试运行:
\documentclass[border=5mm,varwidth=100mm]{standalone}
\usepackage{tikz}
\newcounter{myCounter}
% following properly creates aliases and names in "every NODE"
\tikzset
{ every node/.style=
{ /utils/exec=\stepcounter{myCounter},
alias=nodeAlias\the\value{myCounter},
name=nodeName\the\value{myCounter}
}
}
\begin{document}
\begin{tikzpicture}
\path node{node text};
% following references to name and alias WORK
\path[draw](nodeAlias\the\value{myCounter}.north)--(0,-1);
\path[draw](nodeName\the\value{myCounter}.north)--(1,-1);
\end{tikzpicture}
\end{document}
every path
测试运行:
\documentclass[border=5mm,varwidth=100mm]{standalone}
\usepackage{tikz}
\newcounter{myCounter}
% "alias" generates error in every PATH (undefined control sequence)
% although "name" does not generate error in every PATH, it is useless and anything that references this name will generate error (no shape named "path#" is known, where # is counter value)
\tikzset
{ every path/.style=
{ /utils/exec=\stepcounter{myCounter},
name=pathName\the\value{myCounter}
}
}
\begin{document}
\begin{tikzpicture}
\path node{node text};
% following generates error (no shape named pathName1 is known, but it was supposed to exist)
\path[draw](pathName\the\numexpr\value{myCounter}-1.north)--(0,-1);
\end{tikzpicture}
\end{document}
答案1
如果我们在 pgfmanual 中查找/tikz/name
,则有四个结果。第一个结果在第 129 页,与范围无关。第二个结果在第 150 页,内容如下
这是您在第二个不起作用的示例中设置的密钥。它是不是在第 219 页可以找到的钥匙
您在第一个工作示例中设置了它,您可能也想在第二个示例中设置它。这就是您的第二个示例不起作用的原因。但是,后者是要设置的关键,如果您想引用形状名称稍后。第四个结果可以在第 352 页找到,与第一个结果一样,与本问题无关。
整个讨论可以通过这个简单的例子来说明:
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
\path node[name=A] {xyz};
% \path[name=A] node {xyz}; doesn't work
\draw (A) -- ++ (1,1);
\end{tikzpicture}
\end{document}
此示例有效,但是如果我们使用\path[name=A] node {xyz};
而不是 则\path node[name=A] {xyz};
无效,因为这样会将路径名称(而不是节点名称)设置为A
。在更复杂的代码中也会出现相同的效果。