请考虑以下 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}