latex-draw-a-tree

How to draw simple trees in LaTeX without using TikZ.

References:

  • How can I draw simple trees in LaTeX?

There are ways to get LaTeX to draw it’s own trees that doesn’t involve learning an entirely new language like TikZ.

qtree package

Consider the following TeX:

1
2
3
4
5
6
7
\Tree[.IP [.NP [.Det \textit{the} ]
[.N\1 [.N \textit{package} ]]]
[.I\1 [.I \textsc{3sg.Pres} ]
[.VP [.V\1 [.V \textit{is} ]
[.AP [.Deg \textit{really} ]
[.A\1 [.A \textit{simple} ]
\qroof{\textit{to use}}.CP ]]]]]]

This produces the following tree:

The basic syntax is simply [.node-name subtrees... ]; \qroof, which draws the triangle, requires its node name at the end, instead. The \1 is just a shortcut for a math-mode prime. In addition, qtree will always render _ and ^ as sub- and super-scripts, too.

forest package

Here is a forest version of the code for the tree showed above:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
\begin{forest}
[IP
[NP
[Det
[\textit{the}]
]
[N\('\)
[N
[\textit{package}]
]
]
]
[I\('\)
[I
[\textsc{3sg.Pres}
]
]
[VP
[V\('\)
[V
[\textit{is}]
]
[AP
[Deg
[\textit{extremely}]
]
[A\('\)
[A
[\textit{straightforward}]
]
[CP
[\textit{to wield}, roof]
]
]
]
]
]
]
]
\end{forest}

Easy to see its syntax~ And we can select some additional features: forest allows you to specify that nodes should be aligned on a common tier. To use this feature, you just write , tier=<name of tier> after the content of the node is specified.

Here’s an extreme example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
\begin{forest}
for tree={
fit=band,% spaces the tree out a little to avoid collisions
}
[things
[cabbages, tier=vegetables
[peaches, tier=fruits]
]
[kings, tier=aristocrats]
[sealing wax
[queens, tier=aristocrats
[carrots, tier=vegetables]
[pineapple, tier=fruits]
[aubergine, tier=vegetables]
]
]
]
\end{forest}

ditree package

For non “graphical” tree, we can use the dirtree package:

1
2
3
4
5
6
7
8
9
\dirtree{%
.1 debug.
.2 filename.
.2 modules.
.3 module.
.3 module.
.3 module.
.2 level.
}

It looks like this: