在 tikzpicture 中访问 tikzpicture 中的节点

在 tikzpicture 中访问 tikzpicture 中的节点

一位学生给出了以下答案。最后一行试图访问节点A和节点内的节点B,但没有成功。这种方法能成功吗?

\begin{tikzpicture}[remember picture,
  inner/.style={circle,draw=black!50,fill=white!20,thick,inner sep=3pt},
  outer/.style={draw=black,fill=black!20,thick,inner sep=10pt}
  ]
  \node[outer,draw=black] (A) {
    \begin{tikzpicture}
      \node[inner, state] (a) {$a$};
      \node[inner, state] (b) [below =of a] {$b$};
      \node[inner, state] (c) [below =of b] {$c$};
      \node[inner, state] (d) [below =of c] {$d$};
      \node[inner, state] (e) [below =of d] {$e$};
    \end{tikzpicture}
  };
  \node[outer,draw=black,right=of A] (B) {
    \begin{tikzpicture}
      \node[inner, state] (f) {$f$};
      \node[inner, state] (g) [below =of f] {$g$};
      \node[inner, state] (h) [below =of g] {$h$};
      \node[inner, state] (i) [below =of h] {$i$};
    \end{tikzpicture}
  };
  \draw[very thick,orange,-] (a) -- (i);

答案1

使用matrix库绘制图像的另一种方法:

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,
                positioning}


\begin{document}

    \begin{tikzpicture}[
M/.style = {matrix of math nodes,%
            nodes={circle, draw=gray, thick,
            minimum size=2em, text depth=0.25ex,
            inner sep=0pt},
            row sep=2em,
            draw, thin
             }
                        ]
\matrix (m) [M]
{
a \\    b \\    c \\    d \\    e\\
};
\matrix (n) [M, right=of m]
{
f \\    g \\    h \\    i \\
};
\draw[very thick,orange] (m-1-1) -- (n-4-1);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

使用matrix并明智地使用\\

\begin{tikzpicture}[remember picture, %fixed by PROF
  inner/.style={circle,draw=black!50,fill=white!20,thick,inner sep=3pt},
  outer/.style={draw=black,fill=black!20,thick,inner sep=10pt}
  ]
  \node[matrix,draw=black] (A) {
      \node[inner, state] (a) {$a$};
      \node[inner, state] (b) [below =of a] {$b$};
      \node[inner, state] (c) [below =of b] {$c$};
      \node[inner, state] (d) [below =of c] {$d$};
      \node[inner, state] (e) [below =of d] {$e$};\\
  };
  \node[matrix,draw=black,right=of A] (B) {
      \node[inner, state] (f) {$f$};
      \node[inner, state] (g) [below =of f] {$g$};
      \node[inner, state] (h) [below =of g] {$h$};
      \node[inner, state] (i) [below =of h] {$i$};\\
  };
  \draw[very thick,orange,-] (a) -- (i);
\end{tikzpicture}

相关内容