如何在tikz子图片中引用同名的内部节点

如何在tikz子图片中引用同名的内部节点

请参阅我的评论以了解动机。

以下是我正在做的事情:

\begin{tikzpicture}
   \node (a) at (0,0)
     {
        \input{Diagrams/model1.tex}
     };
    \node (b) at (a) [anchor=west,xshift=50ex]
     {
        \input{Diagrams/model2.tex}
     };


     % connect nodes from these two separate plots that happen to have the same name
    \draw [<->] (a.outlier)--(b.outlier);

\end{tikzpicture}

以下是模拟这种情况的 MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}

\usetikzlibrary{shapes,positioning,arrows}

\begin{tikzpicture}[node distance=10ex]
   \node (a) at (0,0)
     {
     % please see comment, this picture is actually being imported from a separate .tex file but MWE implements differently because I can't load files in a post: 
     \begin{tikzpicture}
        \node (aSub1) {aSub1};
        \node (aSub2) [below of=aSub1] {aSub2};
        \node (outlier1) [below of=aSub2] {outlier};
        \draw [->] (aSub1) -- (aSub2);
     \end{tikzpicture}
     };
    \node (b) at (a) [anchor=west,xshift=50ex]
     {
        % please see comment, this picture is actually being imported from a separate .tex file but MWE implements differently because I can't load files in a post: 
        \begin{tikzpicture}
           \node (bSub1) {bSub1};
            \node (bSub2) [below of=bSub1] {bSub2};
            \node (outlier2) [below of=bSub2] {outlier2};
            \draw [->] (bSub1) -- (bSub2);
         \end{tikzpicture}
     };
   \draw [<->] (a)--(b);
   % now draw a line between sub-nodes of the two nested pictures where the scope of the two "outlier" nodes is local to the sub-picture
   \draw [<->] (outlier1) -- (outlier2); % this doesn't work 
\end{tikzpicture}

\end{document}

输出:

在此处输入图片描述

我想要的是:

在此处输入图片描述

答案1

您想要实现的功能可以通过 es 实现local bounding box。顺便说一句,您正在加载positioning但未使用它。我也修复了这个问题。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}

\usetikzlibrary{positioning}

\begin{tikzpicture}[node distance=8ex]
    \begin{scope}[local bounding box=a]
        \node (aSub1) {aSub1};
        \node (aSub2) [below=of aSub1] {aSub2};
        \node (outlier1) [below=of aSub2] {outlier};
        \draw [->] (aSub1) -- (aSub2);
    \end{scope} 
    \begin{scope}[local bounding box=b,xshift=50ex]
        \node (bSub1) {bSub1};
        \node (bSub2) [below=of bSub1] {bSub2};
        \node (outlier2) [below=of bSub2] {outlier2};
        \draw [->] (bSub1) -- (bSub2);
   \end{scope}
   \draw [<->] (a)--(b);
   \draw [<->] (outlier1) -- (outlier2); 
\end{tikzpicture}
\end{document}

在此处输入图片描述

您还可以使用 来使事情变得更简单name prefix

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}

\usetikzlibrary{positioning}

\begin{tikzpicture}[node distance=8ex]
    \begin{scope}[local bounding box=a,name prefix=a-]
        \node (Sub1) {aSub1};
        \node (Sub2) [below=of Sub1] {aSub2};
        \node (outlier) [below=of Sub2] {outlier};
        \draw [->] (Sub1) -- (Sub2);
    \end{scope} 
    \begin{scope}[local bounding box=b,xshift=50ex,name prefix=b-]
        \node (Sub1) {bSub1};
        \node (Sub2) [below=of Sub1] {bSub2};
        \node (outlier) [below=of Sub2] {outlier2};
        \draw [->] (Sub1) -- (Sub2);
   \end{scope}
   \draw [<->] (a)--(b);
   \draw [<->] (a-outlier) -- (b-outlier); 
\end{tikzpicture}
\end{document}

答案2

您可以这样做,使用两张图片并连接它们之间的节点。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}

\begin{document}

\usetikzlibrary{shapes,positioning,arrows}
\tikzset{node distance=2cm}
\begin{tikzpicture}[remember picture, overlay]
  \node (aSub1) {aSub1};
  \node (aSub2) [below of=aSub1] {aSub2};
  \node (outlier1) [below of=aSub2] {outlier};
  \draw [->] (aSub1) -- (aSub2);
\end{tikzpicture}
\hspace{5cm}
\begin{tikzpicture}[remember picture, overlay]
  \node (bSub1) {bSub1};
  \node (bSub2) [below of=bSub1] {bSub2};
  \node (outlier2) [below of=bSub2] {outlier2};
  \draw [->] (bSub1) -- (bSub2);
  \draw [<->] (aSub2) -- (bSub2);
  \draw [<->] (outlier1) -- (outlier2);
\end{tikzpicture}

\end{document}

相关内容