0

I want to use the graphs library in TikZ together with the graphdrawing algorithms to create a graph with many nodes, and connections that I will make and label later.

I have a more complicated version of this:

\tikz [nodes={fill=white, font=\footnotesize},
>={Stealth[round,sep]}]
\graph[layered layout, branch down sep, nodes=draw, edges=rounded corners]
{
"cat" -- "dog" -- {"cow", "goat", "sheep"};
"cow" -- "bull";
"dog" -- "rooster" -- "duck";
};

What I want to do is list the nodes beforehand, and label them as e.g. a, b, c. I then want to use the reference labels when creating the chains.

The reason I want to do this is because the actual text for each node is a lot longer than "cat", and I want to avoid the clutter of typing out "supercalifragalistic" every time I want to make a new connection.

Ideally I would have something like this:

\begin{tikzpicture}
\tikz [nodes={fill=white, font=\footnotesize},
>={Stealth[round,sep]}]
\node (a) {cat}; 
\node (b) {dog};
\node (c) {cow};
\node (d) {goat};
\node (e) {sheep};
\node (f) {bull};
\node (g) {rooster};
\node (i) {duck};
\graph[use existing nodes = true, layered layout, branch down sep, nodes=draw, edges=rounded corners]
{
"cat" -- "dog" -- {"cow", "goat", "sheep"};
"cow" -- "bull";
"dog" -- "rooster" -- "duck";
(a) -- (b) -- { (c), (d), (e)};
(c) -- (f);
(b) -- (g) -- (i);
};
\end{tikzpicture}

But I get errors with this approach, including attempting to create edge between nodes that are not in the graph.

Is this possible? I have seen a slightly different approach that does work, as follows:

\begin{tikzpicture}[new set=import nodes]
\begin{scope}[nodes={set=import nodes}] % make all nodes part of this set
\node [red] (a) at (0,1) {$a$};
\node [red] (b) at (1,1) {$b$};
\node [red] (d) at (2,1) {$d$};
\end{scope}
\graph{
(import nodes);
% "import" the nodes
a -> b -> c -> d -> e;
};
\end{tikzpicture}

But here there is no graph drawing algorithm (layered layout) and you have to specify the position of the nodes, which I want to avoid.

1 Answers1

2

You can just declare them without connecting them using the

<node name>/<node text>

syntax.

And then reference them via <node name> when actually connecting the nodes.

Code

\documentclass[tikz,convert]{standalone}
\usetikzlibrary{arrows.meta,graphs,graphdrawing}
\usegdlibrary{layered}
\begin{document}
\tikz[
  nodes={fill=white, font=\footnotesize},
  >={Stealth[round,sep]}
]
\graph[
  layered layout,
  branch down sep,
  nodes={draw, text height=height("a"), text depth=+0pt},
  edges=rounded corners
] {
  a/cat, b/dog, c/cow, d/goat, e/sheep, f/bull, g/rooster, i/duck,
  a -- b -- {c, d, e},
  c -- f,
  b -- g -- i,
};
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821