Draw a tree diagram in LaTeX (forest, TikZ)

The forest package draws a tree from nested brackets: [Root [Child A] [Child B]]. It computes the positions itself, so the nodes do not overlap.

Preamble

The snippets on this page need these lines before \begin{document}:

\usepackage{forest}

Write the tree with brackets

Every pair of brackets is a node. Its text comes first, followed by the brackets of its children. Line breaks and indentation in the source have no effect.

\begin{forest}
[Animals
  [Mammals
    [Dog]
    [Cat]
  ]
  [Birds
    [Owl]
    [Eagle]
  ]
]
\end{forest}
Result
The compiled example

Open in the editor

Style the nodes and change the direction

for tree={...} at the start applies options to every node. draw and rounded corners are TikZ options for the frame. grow'=east lets the tree grow to the right with the children in source order from top to bottom, l sep is the distance between levels and s sep the distance between siblings.

\begin{forest}
for tree={draw, rounded corners, grow'=east, l sep=1cm, s sep=3mm}
[Animals
  [Mammals
    [Dog]
    [Cat]
  ]
  [Birds
    [Owl]
    [Eagle]
  ]
]
\end{forest}
Result
The compiled example

Open in the editor

Label the edges

Options of a node follow its text after a comma. edge label={node[midway, left] {0.6}} puts a label on the edge to the parent, as needed for a probability tree.

\begin{forest}
for tree={l sep=1.2cm, s sep=8mm}
[Start
  [Rain, edge label={node[midway, left] {0.6}}
    [Late, edge label={node[midway, left] {0.3}}]
    [On time, edge label={node[midway, right] {0.7}}]
  ]
  [Sun, edge label={node[midway, right] {0.4}}
    [Late, edge label={node[midway, left] {0.1}}]
    [On time, edge label={node[midway, right] {0.9}}]
  ]
]
\end{forest}
Result
The compiled example

Open in the editor

Draw a tree with plain TikZ

TikZ draws trees without a further package: each child {...} after a node adds a child. You set the distances yourself. level distance is the distance between levels, and sibling distance has to be large enough on each level that the subtrees do not overlap.

\begin{tikzpicture}[
  level distance=1.2cm,
  level 1/.style={sibling distance=3cm},
  level 2/.style={sibling distance=1.5cm}
]
\node {Animals}
  child {node {Mammals}
    child {node {Dog}}
    child {node {Cat}}
  }
  child {node {Birds}
    child {node {Owl}}
    child {node {Eagle}}
  };
\end{tikzpicture}
Result
The compiled example

Open in the editor

Draw a directory tree

Loaded with the option edges, forest offers the style folder. Together with grow'=0 it draws the angled lines of a file listing.

Complete document:

\documentclass{article}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={folder, grow'=0, font=\ttfamily}
[thesis
  [main.tex]
  [chapters
    [intro.tex]
    [results.tex]
  ]
  [images
    [setup.png]
  ]
]
\end{forest}
\end{document}
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages