两个相同的子图具有相同的节点,彼此重叠 - tikz

两个相同的子图具有相同的节点,彼此重叠 - tikz

我想链接 subA->subB,其中 subA 和 subB 的所有组件完全相同。当我绘制它时,两个子图相互重叠。有什么想法吗?

\documentclass{article}
% \usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usetikzlibrary {graphs, graphdrawing, positioning} 
\usetikzlibrary{automata,arrows,positioning,calc}
\usegdlibrary {layered,force, circular, trees}

\begin{document}
\begin{tikzpicture}[>=stealth, auto]
  \graph [layered layout, sibling distance=15mm, grow'=right] {
  subA [draw, circle] // [tree layout] {a -> {1,2};};
  subB [draw, circle] // [tree layout] {a -> {1,2};};

  subA -> subB
  };
\end{tikzpicture}
\end{document}

这是我得到的:

在此处输入图片描述

但我需要分离 subA 和 subB 并将它们的节点保留为 {a, 1, 2},是的,它们的节点完全相同

答案1

这是你想要的吗?

注意fresh nodes钥匙您也可以只将其提供给子布局。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{graphs, graphdrawing} 
\usegdlibrary {layered, trees}

\begin{document}
\begin{tikzpicture}[>=stealth, auto]
\graph [layered layout, sibling distance=15mm, grow'=right, fresh nodes] {
  subA [draw, circle] // [tree layout] {a -> {1, 2};};
  subB [draw, circle] // [tree layout] {a -> {1, 2};};
  subA -> subB
};
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述


如果你发现自己需要重复使用一个特定的子图,你可以使用declare钥匙。在本节的最后,手册还为您的问题提供了另一种解决方案,name即将其值作为名称前缀的键。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{graphs, graphdrawing} 
\usegdlibrary {layered, trees}
\begin{document}
\tikz[>=stealth, auto]
\graph [
  layered layout, sibling distance=15mm, grow'=right,
  declare={myGraph}{a -> {1, 2}}
] {
  subA [draw, circle] // [tree layout] { myGraph[name=subA] };
  subB [draw, circle] // [tree layout] { myGraph[name=subB] };
  subA -> subB;
  subA a <->[red] subB 1;
};
\end{document}

输出

在此处输入图片描述

相关内容