9.3 Growing a tree

A very common type of hierarchical graph is a tree. Tree nodes have children. These are connected by edges and are usually displayed in rows when growing down or in columns when growing horizontally.

How to do it…

We will use basic TikZ, without any extra package. Follow these steps:

  1. Start with a document class:
    \documentclass{article}

  2. Load the TikZ package:
    \usepackage{tikz}

  3. Start the document:
    \begin{document}

  4. Begin with a TikZ picture, and specify some options for it:
    \begin{tikzpicture}[sibling distance=10em,
    every node/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=blue!20}]]

  5. Draw a node and add children to it:
      \node {Formulas}
    child { node {single-line} }
    child { node {multi-line}
    child { node {aligned at}
    child { node {relation sign} }
    child { node {several places} }
    child { node {center} } }
    child { node {first left,\\centered,\\last right} }
    };

  6. End the picture and the document:
    \end{tikzpicture}
    \end{document}

  7. Compile, and take a look:

Tree

How it works…

To avoid overlapping, we defined a distance between siblings. As all the nodes were the same, we defined one style for all of them, using a rounded rectangle as the drawn border and a color that transits from white to light blue. We did this by setting these options to every node/.style. This default style simplifies writing. We can still change specific node styles, however.

There’s more…

We can play a bit with the diagram type and items to get different diagrams. If there’s a root item or a central item, it’s always the first in the list.

Creating a decision tree

Our recipe started with a vertical tree. We can also draw a tree horizontally. Let’s do it this way now. We use this occasion to introduce TikZ styles. A style is a set of options. Styles make drawing easier, similar to macros; we don’t need to repeat the same options again and again. Instead, we refer to a desired style. Styles can also be combined.

We will now go through it in detail. These are our steps:

  1. Again, start with a document class:
    \documentclass{article}

  2. Load the Tikz package:
    \usepackage{tikz}

  3. Use the \tikzset command to define your own new styles:
    \tikzset{
    treenode/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white,
    bottom color=blue!20},
    root/.style = {treenode, font=\Large,
    bottom color=red!30},
    env/.style = {treenode, font=\ttfamily\normalsize},
    dummy/.style = {circle,draw}
    }

  4. Start the document:
    \begin{document}

  5. Begin with the TikZ picture:
    \begin{tikzpicture}

  6. Add options to the TikZ picture. As before, use square brackets:
      [
    grow = right,
    sibling distance = 6em,
    level distance = 10em,
    edge from parent/.style = {draw, -latex},
    every node/.style = {font=\footnotesize},
    sloped
    ]

  7. Declare the nodes and children in the tree hierarchy. If an edge needs to be labeled, add a node using the edge from parent option after the node:
      \node [root] {Formula}
    child { node [env] {equation}
    edge from parent node [below] {single-line?} }
    child { node [dummy] {}
    child { node [dummy] {}
    child { node [env] {align\\flalign}
    edge from parent node [below]
    {at relation sign?} }
    child { node [env] {alignat}
    edge from parent node [above] {at several}
    node [below] {places?} }
    child { node [env] {gather}
    edge from parent node [above] {centered?} }
    edge from parent node [below] {aligned?} }
    child { node [env] {multline}
    edge from parent node [above, align=center]
    {first left,\\centered,}
    node [below] {last right}}
    edge from parent node [above] {multi-line?}};

  8. End the TikZ picture and the document:
    \end{tikzpicture}
    \end{document}

  9. Compile, and take a look:

Decision tree

The main difference is grow=right, for getting a horizontal tree. Besides this, we used various custom styles for the nodes. After a node that is connected to its parent by an edge, we use the edge from parent option to add a node with label text.

Navigate: