![两个具有共同“\tikzset”的 TikZ 图片之间出现奇怪的干扰](https://linux22.com/image/327210/%E4%B8%A4%E4%B8%AA%E5%85%B7%E6%9C%89%E5%85%B1%E5%90%8C%E2%80%9C%5Ctikzset%E2%80%9D%E7%9A%84%20TikZ%20%E5%9B%BE%E7%89%87%E4%B9%8B%E9%97%B4%E5%87%BA%E7%8E%B0%E5%A5%87%E6%80%AA%E7%9A%84%E5%B9%B2%E6%89%B0.png)
请考虑以下 MWE:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,positioning,chains,scopes}
\tikzset{
node distance = 10mm and 30mm,
on grid,
start chain = A going below,
start chain = B going below,
myright/.style = {draw, minimum height=4ex, minimum width=33mm,
on chain=A},
myleft/.style = {draw, fill=cyan!30, minimum height=4ex,
on chain=B}
}
\begin{document}
\begin{tikzpicture}
% LEFT
\begin{scope}[every node/.style={myleft}]
\node {X}; % name=B-1
\node {Y};
\node {Z};
\end{scope}
% RIGHT
\begin{scope}[every node/.style={myright}]
\node [right=of B-1] {belong to X}; % name=A-1
\node {belong to Y};
\node {also belong to /};
\node {belong to no one};
\end{scope}
\end{tikzpicture}
some text
\begin{tikzpicture}
% LEFT
\begin{scope}[every node/.style={myleft}]
\node {X}; % name=B-1
\node {Y};
\node {Z};
\end{scope}
% RIGHT
\begin{scope}[every node/.style={myright}]
\node [right=of B-1] {belong to X}; % name=A-1
\node {belong to Y};
\node {also belong to /};
\node {belong to no one};
\end{scope}
\end{tikzpicture}
\end{document}
上面的MWE中有两个相同的图片代码。为什么生成的图片不一样?
答案1
start chain
将链定义为在当前范围内活动的链。如果这是整个文档,那么这就是范围。因此,在第二张图片中,B-1
它不引用您认为的节点。但是,它也没有明确引用第一张图片中的节点,因为没有remember picture
给出,所以如果它在这张图片中绘制,那么它会B-1
在哪里B-1
,但事实并非如此。
要查看发生了什么,请添加\draw [red] (B-1) -- (B-2);
第二张图片:
显然,连接的是非节点B-1
与非节点,B-2
因为B-1
和B-2
在这张图中并不真实存在,并且right=of...
是相对于这张图的范围而不是链的范围。
您所指的节点实际上right=of
是B-4
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,positioning,chains,scopes}
\tikzset{
node distance = 10mm and 30mm,
on grid,
start chain = A going below,
start chain = B going below,
myright/.style = {draw, minimum height=4ex, minimum width=33mm, on chain=A},
myleft/.style = {draw, fill=cyan!30, minimum height=4ex, on chain=B}
}
\begin{document}
\begin{tikzpicture}
% LEFT
\begin{scope}[every node/.style={myleft}]
\node {X}; % name=B-1
\node {Y};
\node {Z};
\end{scope}
% RIGHT
\begin{scope}[every node/.style={myright}]
\node [right=of B-1] {belong to X}; % name=A-1
\node {belong to Y};
\node {also belong to /};
\node {belong to no one};
\end{scope}
\end{tikzpicture}
some text
\begin{tikzpicture}
% LEFT
\begin{scope}[every node/.style={myleft}]
\node {X}; % name=B-1
\node {Y};
\node {Z};
\end{scope}
% RIGHT
\begin{scope}[every node/.style={myright}]
\node [right=of B-4] {belong to X}; % name=A-1
\node {belong to Y};
\node {also belong to /};
\node {belong to no one};
\end{scope}
\draw [red] (B-1) -- (B-2) -- (B-3);
\end{tikzpicture}
\end{document}