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.

sagetexpackage. 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 08 '22 at 22:08sagetex– nonreligious May 09 '22 at 12:39\documentclass, includes all relevant\usepackagecommands, ends with\end{document}and compiles without errors, even if it does not produce your desired output. – Sandy G May 09 '22 at 19:25