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.

5
  • 1
    Welcome to TeX.SE!
    – Mensch
    May 8, 2022 at 20:07
  • This is possible if you use the sagetex package. There's a learning curve; the approach makes more sense as your graphs get complicated. My answer here explains some of the basics. Relabelling the vertices is done in my answer here. Example with text labels: AskSagemath site
    – DJP
    May 8, 2022 at 22:08
  • @Mensch Thank you! May 9, 2022 at 12:36
  • @DJP Thanks! I am familiar with SageMath, so this looks quite useful. But looking at the answer I wonder if this can be recreated using matrix positioning of nodes, without using sagetex May 9, 2022 at 12:39
  • Welcome to TeX.SX. When you post a question, please provide a "Minimal Working Example" (MWE) that starts with \documentclass, includes all relevant \usepackage commands, ends with \end{document} and compiles without errors, even if it does not produce your desired output.
    – Sandy G
    May 9, 2022 at 19:25

1 Answer 1

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

1
  • Fantastic, looks like what I wanted at the time! Oct 5, 2022 at 10:07

You must log in to answer this question.

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