如果我在 tikz-pic 的声明中使用先前定义的坐标的名称,则\pgfpointanchor{}
我会得到Package pgf Error: No shape named `COORD` is known.
下面可以看到此行为的一个示例。我定义了两个坐标 A 和 B,然后我想计算这两个点之间的角度。但这失败了,因为在 pic 定义中 latex 不知道坐标 A 或 B 是什么。
如果我在 tikzpicture 中使用相同的代码,它会按预期工作。我该怎么做才能在 pic-definition 中启用定义的坐标?
\documentclass{standalone}
\RequirePackage{tikz}
\RequirePackage{pgfplots}
\pgfplotsset{compat=1.11}
\usetikzlibrary{positioning,shapes,arrows,shadows,fit,calc,matrix,decorations.text,arrows.meta,shadows.blur,shapes.symbols,automata,fpu,intersections}
\tikzset{
Test/.pic = {
\coordinate (A) at (1, 1);
\coordinate (B) at (0, 0);
\pgfmathanglebetweenpoints{\pgfpointanchor{A}{center}}{\pgfpointanchor{B}{center}}
\draw[red] (B) arc[start angle=0, end angle=360, radius=5mm];
\draw[green] (B) arc[start angle=0, end angle=\pgfmathresult, radius=5mm];
}
}
\begin{document}
\begin{tikzpicture}
%original pic, which is not working
\pic[](test1) at (0, 12mm) {Test};
%this is the reference, which is working, but should be replaced by the pic
\coordinate (A) at (1, 1);
\coordinate (B) at (0, 0);
\pgfmathanglebetweenpoints{\pgfpointanchor{B}{center}}{\pgfpointanchor{A}{center}}
\draw[red] (B) arc[start angle=0, end angle=360, radius=5mm];
\draw[green] (B) arc[start angle=0, end angle=\pgfmathresult, radius=5mm];
\end{tikzpicture}
\end{document}
答案1
这是语法中有点烦人的部分,pic
因为所有节点都name prefix
从图片名称中推断出一个,例如这里test1
。
您可以做几件事,但如果您不打算引用中的任何节点pic
,则范围是所有内容,并向[name prefix ..]
范围添加选项以重置名称前缀
\documentclass[tikz]{standalone}
\tikzset{Test/.pic = {
\begin{scope}[name prefix ..]
\coordinate (A) at (1, 1);
\coordinate (B) at (0, 0);
\pgfmathanglebetweenpoints{\pgfpointanchor{A}{center}}{\pgfpointanchor{B}{center}}
\let\myresult\pgfmathresult
\draw[red,thick] (B) arc[start angle=0, end angle=360, radius=5mm];
\draw[ultra thick, blue] (B) arc[start angle=0, end angle=\myresult, radius=5mm];
\end{scope}
}
}
\begin{document}
\begin{tikzpicture}
\pic[](test1) at (0, 12mm) {Test};
\end{tikzpicture}
\end{document}