texblog

Drawing trees with TikZ/PGF and LuaLaTeX

TikZ/PGF is a flexible and powerful package for creating graphics in LaTeX. It’s syntax can be somewhat overwhelming at the beginning. In this post I show the basics of tree drawing using TikZ and LuaLaTeX.

 
The following code only works with LuaLaTeX. Make sure you use the lualatex engine rather than standard tex/latex for typesetting.

 
Setup

First, we load TikZ and it’s graphdrawing.trees library in the document preamble:

\usepackage{tikz}
\usetikzlibrary{graphdrawing.trees}

 
In the document body, we prepare the figure environment. Here, we set nodes to be circles and edges to be directed vertices using the default arrow head.

\begin{tikzpicture}[nodes={draw, circle}, ->]

	% Tree structure

\end{tikzpicture}

 
With that, we are ready to start drawing trees.

 
Tree drawing syntax

We start with a root node r which has two children, node a and node b.

\node{r}
	child { node {a} }
	child { node {b} };

The library implements an algorithm which handles the distribution of children under each parent node. Therefore, we don’t have to worry about the placement of nodes in the tree.

Next, we might want to add extra levels, for example by adding three children under the child node a.

This is not really complicated, we just have to make sure that we place children of node a within the child node.

\node{r}
	child { node {a} 
		child { node {c} }
		child { node {d} }
		child { node {e} }
	}
	child { node {b} };

Obviously, there is not enough space to add another three children to node b. As a consequence, child nodes would overlap. However, this can be fixed by adding extra missing nodes and therefore increase the angle between child nodes. I will show how to do that in a moment.

 
Missing nodes

Missing nodes do just what they say, they add space for missing nodes. In the example below, node b from the previous example was replaced by a missing node. Therefore, node a is slightly shifted towards the left, leaving space for the ‘missing’ child node.

\node{r}
	child { node {a} 
		child { node {c} }
		child { node {d} }	
	}
	child [ missing ];

 
Using the concept of missing nodes, we can now add extra children to node b, whereby we omit overlap with children of node a.

\node{r}
	child { node {a} 
		child { node {c} }
		child { node {d} }
		child { node {e} }
	}
	child [missing]
	child [missing]
	child {node {b} 
		child { node {c} }
		child { node {d} }
		child { node {e} }
	};

 
Directed and undirected edges

TikZ supports different types of edges. For trees, the following are frequently used types of edges:

-> % Directed edge
<- % Reverse directed edge
-- % Undirected edge
<-> % Bi-directed edge

 
We previously saw how to globally set the direction of edges. We can locally change the direction of a specific vertex using the edge keyword. In the example below, we change edge a to a bi-directed edge.

\begin{tikzpicture}[nodes={draw, circle}, ->]
\node{r}
	child { 
		node {a} 
		edge from parent [<->]
	}
	child { node {b} };
\end{tikzpicture}

 
Node size and line thickness

The size of a node changes, depending on its content/text. The minimum size imposes a minimal size on each node. By adjusting the value depending on the longest text/largest node size, we can ensure that all nodes have roughly the same size.

\begin{tikzpicture}[nodes={draw, circle}, minimum size=1cm]

 
We can further adjust the line thickness using the keywords thick or very thick.

\begin{tikzpicture}[nodes={draw, circle}, thick]

 
In the example below, I increased the line thickness to thick and changed nodes to a minimum size of 0.7cm. As you can see, all nodes are of the same size now. Whereas before, node b was slightly bigger due to the size of the character.

 
Further options

Below are a few more options that might be useful to improve the appearance of the tree.

Changing the level distance (parent to child):

\begin{tikzpicture}[nodes={draw,circle}, level distance=2cm]

 
Changing the sibling distance (child to child):

\begin{tikzpicture}[nodes={draw,circle}, sibling distance=1cm]

 
Using rectangular rather than circular nodes:

\begin{tikzpicture}[nodes={draw,rectangle}]

 
Colored nodes:

\begin{tikzpicture}[nodes={draw,circle,fill=blue!20}]

 
Tree layout and graph syntax

The TikZ tree layout and graph syntax make tree building even easier. Instead of child and node keywords, curly brackets are used to indicate dependencies (edges). Siblings are separated by a comma and missing nodes are created by an empty node.

Here is the example from above in graph syntax:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphs}
\usetikzlibrary{graphdrawing}
\usegdlibrary{trees}

\begin{document}

\tikz[tree layout]\graph[nodes={draw, circle}] {
r -> 
	{ a -> 
		{ c, d },
	% b missing
	}
};
\end{document}

 
Using graph syntax, we can change the direction of the tree. For example grow=0 lets the tree grow to the right rather than downwards.

\tikz[tree layout, grow=0]\graph[nodes={draw, circle}] {
r -> 
	{ a -> 
		{ c, d },
	% b missing
	}
};
\end{document}

 
For more details, please consider reading the documentation (page 405+) or drop me a comment below.

Exit mobile version