The forest package

Draws trees from bracket notation: [parent [child] [child]] inside a forest environment.

Loading the package

\usepackage{forest}

A tree in plain tikz needs a position for every node. In forest you write only the structure: each pair of square brackets is a node, and brackets inside it are its children. The package computes the positions so that subtrees do not overlap.

Options follow the node text after a comma, for example [Body, fill=yellow!30]. for tree={...} at the start applies options to every node. The option names are TikZ keys plus tree-specific ones such as l sep (distance between levels) and s sep (distance between siblings).

Main commands

\begin{forest} [A [B] [C]] \end{forest}Tree with the root A and the children B and C.
for tree={draw, rounded corners}Applies options to all nodes, here a frame with rounded corners.
[Body, fill=yellow!30]Options for one node, after the text.
for tree={grow'=east}Grows the tree from left to right instead of top to bottom.
l sep=8mm, s sep=6mmMinimum distance between levels and between siblings.
edge label={node[midway, left] {yes}}Text on the edge to the parent node.
\usepackage[edges]{forest}Loads the edges library; forked edges then draws right-angled edges, folder a directory tree.

Complete example

\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={draw, rounded corners, align=center, l sep=8mm, s sep=6mm}
  [Document
    [Preamble
      [Class]
      [Packages]
    ]
    [Body, fill=yellow!30
      [Sections]
      [Figures]
      [{Tables, lists}]
    ]
  ]
\end{forest}
\end{document}
Result
The compiled example

Open in the editor

Compiled with pdfLaTeX from TeX Live 2023.

Pitfalls

Related pages