0

I want to link subA->subB where subA and subB have all components exactly the same. When I draw it, the two subgraphs overlapping with each other. Any idea?

\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}

This is what I got:

enter image description here

but I need to separate subA and subB and keep their nodes as {a, 1, 2}, yes, their nodes are exactly the same

1 Answer 1

1

Is this what you want?

Notice the fresh nodes key which you can also give only to a sublayout.

Code

\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}

Output

enter image description here


If you find yourself needing a specifiy subgraph repeatedly, you can declare your own graph with the declare key. At the end of that section, the manual also offers another solution to you problem, the name key which prefixes its value to names.

Code

\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}

Output

enter image description here

8
  • There's of course also the possibility to use different names but the same text for nodes, even just by using explicitly aA/a -> {1A/1, 2A/2} and aB/a -> {1B/1, 2B/2}. Oct 19, 2022 at 15:08
  • my subgraphs are separated but the arrow linking two subgraphs is missing @Qrrbrbirlbel
    – Edee
    Oct 19, 2022 at 15:15
  • another thing is, Tikz's been so hard for me to look for the properties I want, their document is ok, but it's hard to retrieve, any solution ?
    – Edee
    Oct 19, 2022 at 15:17
  • Do you have any idea on how to use a variable to store a defined graph structure so I can reuse it when needed?
    – Edee
    Oct 19, 2022 at 15:21
  • @Edee 1. The arrow is there, look closely between the circles. 2. I knew there's an use existing nodes key … there must be an opposite key for that, I thought. 3. There the declare key. At the end of that section is also an example that provides an alternative solution to your problem: declare. Let me update my answer. Oct 19, 2022 at 16:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .