两个具有共同“\tikzset”的 TikZ 图片之间出现奇怪的干扰

两个具有共同“\tikzset”的 TikZ 图片之间出现奇怪的干扰

请考虑以下 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-1B-2在这张图中并不真实存在,并且right=of...是相对于这张图的范围而不是链的范围。

您所指的节点实际上right=ofB-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}

加入鬼魂并相对于非鬼魂进行定位

相关内容