Research techniques and education

Making Diagram Trees with LaTeX

There are times when we want to depict hierarchical relationships in a diagram. Examples include workflow chart, organizational chart, or even a family tree. In such situations, the tree diagram feature in LaTeX can meet the need.

This post will provide an example of the development of a tree diagram used the tikz package. Specifically, we will make a vertical and a horizontal tree.

Vertical Tree

First I want you to see what our final product looks like before we go through each step to make it.

As you can see it is a simple tree.

To develop the tree you need to setup the preamble with the following.

There is nothing to see yet. All we did was set the documentclass to an article and load the tikz package which is the package we will use to make the tree.

The next step will be to make a tikzpicture environment. We also need to set some options for what we want our nodes to look like. A node is a created unit in a picture. In our completed example above there are 5 nodes or rectangles there. We have to set up how we want these nodes to look. You can set them individually or apply the same look for all nodes. We will apply the same look for all of our nodes using the every node/.style feature. Below is the initial setup for the nodes. Remeber this code goes after \begin{document} and before \end{document}

We are now ready to draw our tree. The first step is to draw the root branch below is the code. This code goes after the tikzpicture options but before \end{tikzpicture}.

\node{root branch};

We will now draw our 1st child and grandchild. This can be somewhat complicated. You have to do the following

Remove the semicolon after {root branch}

Press enter and type child

make a curly brace and type node

make another curly brace and type 1st child and close this with a second curly brace

press enter and type child

type node and then a curly brace

type grandchild and close the curly braces three times

end with a semicolon

Below is the code followed by a picture

\node{root branch}
child{node{1st child}
child{node{grandchild}}};

We now repeat this process for the second child and grandson. The key to success is keeping track of the curly braces and the semicolon. A Child node is always within another node with the exception of the root. The semicolon is always at the end of the code. Below is the code for the final vertical tree.

Horizontal trees follow all the same steps. To make a horizontal tree you need to add the argument “grow=right” to the options inside the brackets. Doing so and you will see the following.

Conclusion

As you can see, make diagram trees is not overly complicated in LaTeX. The flexibility of the tikz package is truly amazing and it seems there are no limits to what you can develop with it for visual representations.