我正在尝试绘制一个图形,并在其中一个外部节点内有一个单独的图形。我看到了这个问题,但我不明白matrix
节点将如何帮助我。我还想在外部节点和内部节点之间绘制边。
有人可以简单举一个有 3 个节点的例子吗,其中一个节点包含一个有 3 个节点的子图?
答案1
这里有一个小例子:两个“外部”节点,每个节点包含一个图(由“内部”节点形成)和一些连接外部节点到外部节点的边和箭头,以及从一个图的内部节点到另一个图的内部节点;该remember picture
选项允许您随时访问内部节点和外部节点:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[remember picture,
inner/.style={circle,draw=blue!50,fill=blue!20,thick,inner sep=3pt},
outer/.style={draw=green,fill=green!20,thick,inner sep=10pt}
]
\node[outer,draw=green] (A) {
\begin{tikzpicture}
\node [inner,draw=blue] (ai) {A1};
\node [inner,draw=blue,below=of ai] (aii) {A2};
\node [inner,draw=blue,right=of aii] (aiii) {A3};
\draw[red,thick] (ai) -- (aii) -- (aiii) -- (ai);
\end{tikzpicture}
};
\node[outer,draw=green,right=of A] (B) {
\begin{tikzpicture}
\node [inner,draw=blue] (bi) {B1};
\node [inner,draw=blue,below=of bi] (bii) {B2};
\node [inner,draw=blue,right=of bii] (biii) {B3};
\node [inner,draw=blue,right=of bi] (biv) {B4};
\draw[red,thick] (bi) -- (bii) -- (biii) -- (biv) -- (bi) -- (biii);
\end{tikzpicture}
};
\draw[thick,orange,->] (ai) -- (bii);
\draw[orange,->] (aiii) -- (bi);
\draw[orange,->] (A.90) -- ($(A.90)+(0,1)$) -| (B);
\end{tikzpicture}
\end{document}
答案2
我不知道 OP 遇到了什么样的问题,TiKZ matrices
但 Gonzalo 的例子是那种matrix
非常有用的图表。
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,matrix}
\begin{document}
\begin{tikzpicture}[
inner/.style={circle,draw=blue!50,fill=blue!20,thick,inner sep=3pt},
outer/.style={draw=green,fill=green!20,thick,inner sep=10pt, column sep=1cm, row sep=1cm}
]
\matrix (A) [matrix of nodes, outer, nodes={inner}]{
A1 \\
A2 & A3\\
};
\matrix (B) [matrix of nodes, outer, nodes={inner}, right=of A]{
B1 & B4\\
B2 & B3\\
};
\draw[red, thick] (A-1-1)--(A-2-1)--(A-2-2)--(A-1-1);
\draw[red, thick] (B-1-1)--(B-1-2)--(B-2-2)--(B-2-1)--(B-1-1);
\draw[thick, orange,->] (A-1-1)--(B-2-1);
\draw[thick, orange,->] (A-2-2)--(B-1-1);
\draw[thick, orange,->] (A.north)--([yshift=1cm]A.north)-|(B);
\end{tikzpicture}
\end{document}
答案3
构建于贡萨洛·梅迪纳答案我找到了这个简洁的代码,当想要复制图形组件时它很有用。它还在一定程度上实现了索引的自动化。
\documentclass{standalone}
\usepackage{tikz}
\newcommand{\blocknode}[1]{
\begin{scope}[every node/.style={thick, circle, draw ,minimum width=5mm}]
\draw (0,0) -- (0,5) -- (5,5) -- (5,0) -- cycle;
\node (T#1) at (2.5, 1){T#1};
\node (L#1) at ( 1, 4){L#1};
\node (R#1) at ( 4, 4){R#1};
\draw (L#1) to (T#1);
\draw (R#1) to (T#1);
\end{scope}
}
\begin{document}
\begin{tikzpicture}[remember picture]
\node (N1) at (0,0){\tikz{\blocknode{1}}};
\node (N2) at (9,3){\tikz{\blocknode{2}}};
\draw[color=red] (L1) -- (T1) --(R1) --(L1);
\draw[color=blue] (L2) -- (T2) --(R2) --(L2);
\draw[color=green] (L1) -- (L2) (R1)--(R2) (T1) --(T2);
\end{tikzpicture}
\end{document}