Trees (forest/qtree)

Syntax trees, taxonomies, decision trees, B-trees — drawing branching diagrams in LaTeX comes down to three tools. The most powerful and modern is **forest; the quickest standard for linguistic syntax trees is qtree / tikz-qtree; and for small trees with no extra packages there is plain TikZ**. All three let you describe the nested "parent grows into children" structure without computing a single coordinate. This page is about choosing between them and how each is written.

Three approaches

A tree is essentially a nested (recursive) structure: there is a root, children hang beneath it, those children have grandchildren, and so on. If you can mirror that nesting with nested brackets, you can build a tree without specifying any coordinates. All three tools rest on this idea, but they differ in feel and in how far they go.

  • forest — the most flexible package, built on TikZ and pgfkeys. Onto its labelled-bracket notation** [root [child] [child]] you can layer per-node options and a tree-wide style, and it packs the spacing automatically according to how complex each node’s contents are. Good for linguistics and for general trees alike.
  • qtree / tikz-qtree** — the quickest standard, focused on linguistic syntax trees. You write \Tree [.S [.NP ... ] [.VP ... ] ], where a .label marks a non-terminal (branching) node. tikz-qtree reimplements the same notation on TikZ, with nicer output and more control.
  • Plain TikZ (the trees library) — the child / node mechanism, \node {root} child {node {a}} child {node {b}};. No extra package, and well suited to drawing a small tree on the spot.

In short, **forest is the most flexible and suits elaborate trees, while qtree / tikz-qtree are fastest for constituency trees**. Plain TikZ adds no dependency and is nimble for small jobs. We look at each in turn.

forest — modern trees in brackets

forest (by Sašo Živanović, on CTAN) is a tree-drawing package built on TikZ and pgfkeys. You load it in the preamble and write the tree in labelled-bracket notation** inside a forest environment. Each node is wrapped in square brackets [ ], and its children are listed inside those brackets — the nesting of brackets *is* the nesting of the tree. For linguistics, the linguistics option makes the branches meet above the two children they join.

document.tex
\usepackage[linguistics]{forest}

\begin{forest}
[CP
  [C]
  [IP
    [I]
    [VP
      [V]
      [NP]
    ]
  ]
]
\end{forest}

This draws a tree with root CP over C and IP; IP over I and VP; and VP over V and NP. Packing it onto one line as [CP[C][IP[I][VP[V][NP]]]] yields the same tree, but with anything complex the brackets get hard to match, so the idiom is to show the hierarchy with line breaks and indentation, as above. One pitfall: **a blank line in the source makes forest fail to parse** (a blank line reads as a paragraph break). Indentation itself is free.

The strength of forest is automatic layout. It positions each node according to the complexity of what it dominates and packs the nodes horizontally so they do not collide, so even a busy tree comes out almost presentable as-is. When you do want to adjust spacing, apply for tree={...} to the whole tree or a subtree. The three you reach for most are **s sep (horizontal separation), l (the vertical distance between levels), and inner sep** (the padding around a node).

document.tex
\usepackage[linguistics]{forest}

\begin{forest}
for tree={s sep=10mm, inner sep=0, l=0}
[CP
  [C]
  [IP, for tree={s sep=20mm}
    [I, name=src]
    [VP
      [V]
      [DP, roof, name=tgt][the woman]]
  ]
]
\draw[->] (src) to[out=south west, in=south] (tgt);
\end{forest}

This example packs in several idioms. The leading for tree={...} sets the spacing for the whole tree, and placing a comma after a node label — as in [IP, for tree={s sep=20mm} — overrides the spacing for just that subtree. roof puts a triangle (a "roof") over that node’s label, used to abbreviate internal structure and show a phrase as one unit. Giving a node name= lets you draw a movement arrow after the tree’s closing bracket with \draw[->] (src) to[...] (tgt); (inside forest you can use TikZ commands directly). And to put a comma *inside* a label, wrap it in braces as {NP, PP}, distinguishing it from a layout instruction.

If you don’t want to write the same look every time, define a default style in the preamble with \forestset{default preamble={for tree={...}}}. Because forest is built on TikZ/pgf, the fine appearance of nodes — shape, color, arrow tips, highlighting — follows directly from what you know of TikZ (see "TikZ basics" for the foundations).

qtree / tikz-qtree — the shortest path to a syntax tree

To draw a constituency (syntax) tree quickly, the long-standing standard is **qtree**. Put \usepackage{qtree} in the preamble and, in the body, follow \Tree with a bracketed tree. The heart of the notation: **a label preceded by a period . marks a non-terminal (branching) node, while a bare word with no period is a leaf (terminal)**.

latex
\Tree [.S [.NP Kim ] [.VP [.V saw ] NP ] ]

That single line draws a tree with root S over NP and VP; NP with the leaf Kim; and VP over V (leaf saw) and NP. A few rules apply: the tree must come immediately after \Tree; non-terminal labels begin with a period (.S); and a **closing bracket ] must be preceded by whitespace** (as in saw ]). qtree also has conveniences: NP_i and N^0 auto-format their subscripts/superscripts in math mode inside the tree, X\1 abbreviates X$'$ (X′), and **\qroof{...} puts a triangular roof** over a phrase (e.g. \qroof{out of style}.PP). To stop the automatic centering, load the option \usepackage[nocenter]{qtree}.

tikz-qtree (by David Chiang) reimplements** this \Tree notation on TikZ. Load \usepackage{tikz} and \usepackage{tikz-qtree}, and the same \Tree [.S ... ] works unchanged, now at TikZ quality. For simple trees it is almost completely interchangeable with qtree, and because TikZ’s style machinery applies, arrows and branches are easier to tune. For instance, \tikzset{grow'=right} makes the tree grow left-to-right instead of top-down. Where the original qtree draws lines from font metrics alone, tikz-qtree draws with TikZ, so the lines are smoother and later decoration is easy.

Plain TikZ — child and node

For a small tree where you’d rather not add a package, TikZ’s own **child mechanism** is enough. After placing a node, follow it with child {...} and its contents are laid out as a child, with the parent-to-child edge drawn automatically. Inside a child you normally write node {...}, and you nest further childs to make grandchildren.

latex
\begin{tikzpicture}[level distance=12mm, sibling distance=24mm]
  \node {root}
    child { node {a} }
    child { node {b}
      child { node {c} }
      child { node {d} }
    };
\end{tikzpicture}

This draws, top-down, a root over a and b, with b further split into c and d. Note the single **semicolon ;** for the whole tree, at the end of \node ... ;. Placement is governed by two distances: the vertical gap between a parent’s level and its children’s, **level distance (default 15 mm), and the spacing between siblings side by side, sibling distance (default 15 mm). To vary them per level, use level ⟨n⟩/.style**, e.g. level 1/.style={sibling distance=4cm}. The growth direction changes with [grow=right] and the like, and the edges are controlled by edge from parent (for example edge from parent fork down for right-angled connectors).

Plain TikZ is convenient, but having no automatic packing like forest, it needs you to adjust sibling distance by hand to avoid overlaps as the number of children or the depth grows. It is plenty for a small few-level tree, but for a busy syntax tree, or one whose spacing should follow its contents, forest is less work.

Which to choose

A rule of thumb when unsure. Overall, **for a new tree, reach for forest first — its automatic layout, its range from linguistics to general trees, and the full power of TikZ for decoration. To dash off a single syntax tree**, the \Tree notation of qtree / tikz-qtree is the shortest path, and tikz-qtree if you want the output polished. For a few-level tree with no new dependency, plain TikZ’s child is enough.

ToolNotationBest for
forest[root [child] [child]] (brackets)Elaborate trees, auto-layout, linguistic and general
qtree\Tree [.S [.NP ... ] ... ]Fastest syntax trees; lightweight, font-based
tikz-qtree\Tree (same as qtree)Syntax trees at TikZ quality, more tunable
TikZ (trees)\node {r} child {node {a}}Small trees; when avoiding extra packages

All are on CTAN and ship with TeX Live and MiKTeX. forest and tikz-qtree live under CTAN’s graphics/pgf/contrib/ and depend on TikZ/pgf. For the full detail of each package, the surest reference is texdoc forest, texdoc tikz-qtree, or texdoc qtree on your own machine.