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

\tikz [nodes={fill=white, font=\footnotesize},
\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);

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$};
(import nodes);
% "import" the nodes
a -> b -> c -> d -> e;

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
    Welcome to TeX.SE!
    – Mensch
    Commented 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
    Commented May 8, 2022 at 22:08
  • @Mensch Thank you! Commented 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 Commented 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
    Commented May 9, 2022 at 19:25

1 Answer 1


You can just declare them without connecting them using the

<node name>/<node text>


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


  nodes={fill=white, font=\footnotesize},
  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,


enter image description here

  • Fantastic, looks like what I wanted at the time! Commented 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 .