Final solution for ex1.
1 \documentclass{article}
3 \usepackage{xltxtra,fontspec}
6 \setmainfont[Mapping=tex-text]{DejaVu Serif}
7 \setmonofont[Scale=0.95]{DejaVu Sans Mono}
8 \newfontfamily\verbfont[Scale=0.8]{DejaVu Sans Mono}
9 \newfontfamily\ar[Script=Arabic]{DejaVu Sans}
10 \newfontfamily\ja{VL Gothic}
11 \newfontfamily\javerbfont[Scale=0.85,LetterSpace=5.0]{VL Gothic}
15 \setmainfont[Mapping=tex-text]{DejaVu Serif}
16 \setmonofont[Scale=0.95]{DejaVu Sans Mono}
17 \newfontfamily\verbfont[Scale=0.8]{DejaVu Sans Mono}
18 \newfontfamily\ar[Script=Arabic]{DejaVu Sans}
19 \newfontfamily\ja{Hiragino Maru Gothic Pro}
20 \newfontfamily\javerbfont[Scale=0.85,LetterSpace=5.0]{Hiragino Maru Gothic Pro}
24 \usepackage{tikz-qtree}
28 \usepackage{fancyvrb,fvrb-ex}
29 \fvset{gobble=0,xleftmargin=0.5in,xrightmargin=2.75in,formatcom=\verbfont}
32 \newcommand\tikztree{\texttt{tikz-qtree}}
36 \title{\tikztree: better trees with TikZ}
38 \date{Version 1.11 (Christmas 2010)}
44 The \tikztree{} package provides a macro for drawing trees with
45 TikZ\footnote{\texttt{http://sourceforge.net/projects/pgf/}} using the
46 easy syntax of Alexis Dimitriadis'
47 Qtree\footnote{\texttt{http://www.ling.upenn.edu/advice/latex/qtree/}}. It
48 improves on TikZ's standard tree-drawing facility by laying out tree
49 nodes without collisions; it improves on Qtree by adding lots of
50 features from TikZ; and it improves on \verb|pst-qtree| in being
51 usable with pdf\TeX{} and
52 \XeTeX{}.\footnote{Although \XeTeX{} works with \verb|pst-qtree| using the \verb|xetex-pstricks| package. For typesetting very large trees or a large number of trees, this may be the better option.}
56 To load the package in \LaTeX{}:
59 \usepackage{tikz-qtree}
62 The simplest usage is identical to Qtree:
64 \begin{SideBySideExample}
65 \Tree [.S [.NP [.Det the ] [.N cat ] ]
68 [.NP [.Det the ] [.N mat ] ] ] ] ]
69 \end{SideBySideExample}
71 Subtrees are delimited by square brackets. A subtree's root label is
72 joined by a dot (\verb|.|) to its opening bracket.\footnote{You can
73 also write the label after the closing bracket instead of the opening
74 bracket, or both, or neither. Please see the Qtree documentation for
75 details.} As in Qtree, spaces are required after every (internal or
78 \verb|\Tree| works inside or outside a
79 \verb|tikzpicture| environment, but many of the features described
80 below require the explicit \verb|tikzpicture| environment.
84 \paragraph{Options} Some options for standard TikZ trees work for \verb|\Tree| as
87 \item \verb|level distance|: vertical distance between the anchors of a parent and its children
88 \item \verb|sibling distance|: horizontal distance between the boundaries of sister subtrees (not the anchors of their roots, as in standard TikZ trees). Note that TikZ nodes already have some horizontal space around them (\verb|inner xsep|, by default \verb|0.3333em|), so even \verb|sibling distance=0pt| leaves some room.
90 These are set either by writing
91 \verb|\tikzset{|\textit{option}\verb|=|\textit{value}\verb|}| or by
92 writing \verb|[|\textit{option}\verb|=|\textit{value}\verb|]| after a
93 \verb|\begin{tikzpicture}| or \verb|\begin{scope}|.\footnote{Allowing
94 options after \verb|\Tree| would have made sense, but there would be
95 no way to disambiguate the two uses of square brackets.} For example:
98 \begin{SideBySideExample}
100 \tikzset{level distance=72pt}
101 \Tree [.NP [.Adj tall ] [.N tree ] ]
104 \begin{tikzpicture}[sibling distance=72pt]
105 \Tree [.NP [.Adj fat ] [.N tree ] ]
107 \end{SideBySideExample}
110 The \verb|grow=|\textit{direction}\/ and \verb|grow'=|\textit{direction}\/ options control the orientation of trees just as for standard TikZ trees. However, \textit{direction}\/ must be one of \verb|up|, \verb|down|, \verb|left|, or \verb|right|. The difference between \verb|grow| and \verb|grow'| is that \verb|grow| places children counterclockwise with respect to their parent and \verb|grow'| places them clockwise:
112 \begin{SideBySideExample}
113 \begin{tikzpicture}[grow'=down]
114 \Tree [.NP [.Adj reverse ] [.N tree ] ]
117 \begin{tikzpicture}[grow'=up]
118 \Tree [.NP [.Adj upside-down ] [.N tree ] ]
120 \end{SideBySideExample}
122 \begin{SideBySideExample}
123 \begin{tikzpicture}[grow=left]
124 \tikzset{level distance=60pt,sibling distance=18pt}
125 \tikzset{execute at begin node=\strut}
126 \Tree [.NP [.Adj sideways ] [.N tree ] ]
128 \end{SideBySideExample}
130 Note that in sideways trees, \verb|level distance| is horizontal and \verb|sibling distance| is vertical. Sideways trees do take a little extra adjusting to look right, since the defaults are geared towards vertically growing trees. The meaning of the option \verb|execute at begin node=\strut| is, before typesetting the label of every node, insert the command \verb|\strut|, which is an invisible box that maximizes the height and depth of the node.
132 \paragraph{Styles} TikZ lets you define \emph{styles}\/ which encapsulate multiple options:
134 \tikzset{small/.style={level distance=20pt,sibling distance=0pt}}
136 Then the option \verb|small| causes the options in its definition to be used:
138 \tikzset{small/.style={level distance=20pt,sibling distance=0pt}}
139 \begin{SideBySideExample}
140 \begin{tikzpicture}[small]
141 \Tree [.NP [.Adj small ] [.N tree ] ]
145 \Tree [.NP [.Adj normal ] [.N tree ] ]
147 \end{SideBySideExample}
149 The following TikZ styles are automatically applied inside trees,
150 providing a hook for you to change the appearance of particular kinds
153 \item \verb|every tree node| to every (internal and leaf) node (default: \verb|anchor=base|)
154 \item \verb|every internal node| to every internal node
155 \item \verb|every leaf node| to every leaf node
156 \item \verb|edge from parent| to every edge (default: \verb|draw|)
159 The options for nodes and edges are all handled by TikZ and are
160 described in detail in the TikZ documentation. For example, if you
161 have a font named \verb|\ar| and want to set all the leaf labels in
164 \begin{SideBySideExample}
167 \tikzset{every leaf node/.style={font=\ar}}
168 \Tree [.S [.NP القط ]
170 [.PP [.P على ] [.NP حصيرة ] ] ] ]
172 \end{SideBySideExample}
175 You can make the nodes in a sideways tree line up on their left edge using \verb|anchor=base west|:
177 \begin{SideBySideExample}
179 \tikzset{grow'=right,level distance=32pt}
180 \tikzset{execute at begin node=\strut}
181 \tikzset{every tree node/.style={anchor=base west}}
182 \Tree [.S [.NP [.Det the ] [.N cat ] ]
185 [.NP [.Det the ] [.N mat ] ] ] ] ]
187 \end{SideBySideExample}
190 In Qtree, it was allowed to use a line break (\verb|\\|) inside a node. TikZ nodes by default don't allow this, but the \verb|align| option (in PGF/TikZ version 2.1 or later) enables it as a side effect:\footnote{Thanks to Alan Munn for figuring this out. Prior to PGF/TikZ version 2.1, the fix was to use the options \verb|text width=2em,text centered|.}
192 \begin{SideBySideExample}
194 \tikzset{every tree node/.style={align=center,anchor=north}}
195 \Tree [.S [.NP Det\\the N\\cat ]
198 [.NP Det\\the N\\mat ] ] ] ]
200 \end{SideBySideExample}
203 You can also define a style for all the edges in a tree. For example, if you want the edges to be a little darker:
205 \begin{SideBySideExample}
207 \tikzset{edge from parent/.style={draw,thick}}
208 \Tree [.S [.NP [.Det the ] [.N cat ] ]
211 [.NP [.Det the ] [.N mat ] ] ] ] ]
213 \end{SideBySideExample}
215 The \verb|draw| option is necessary, as by default they will not be
216 drawn. As a more complex example, edges have an
217 \verb|edge from parent path| option which lets you change the shape of
218 the edge. Its value is a TikZ path expressed in terms of
219 \verb|\tikzparentnode|, the parent node, and \verb|\tikzchildnode|,
222 \begin{SideBySideExample}
224 \tikzset{edge from parent/.style=
226 edge from parent path={(\tikzparentnode.south)
228 -| (\tikzchildnode)}}}
229 \Tree [.S [.NP [.Det the ] [.N cat ] ]
232 [.NP [.Det the ] [.N mat ] ] ] ] ]
234 \end{SideBySideExample}
239 \section{Embedding TikZ nodes}
241 Inside a \verb|\Tree|, in place of a node label, you can use a TikZ
242 \verb|\node| command.\footnote{\verb|\Tree| specifically watches out
243 for the token \verb|\node|; do not use \verb|\path node| or other
246 \verb|\node [|\textit{options}\verb|] (|\textit{name}\verb|) {|\textit{label}\verb|};|
248 Don't forget the terminating semicolon. The
249 \verb|[|\textit{options}\verb|]|, which are optional, let you change
250 the appearance of the node; for example, the \verb|draw| option draws
251 a border around the node. The \verb|(|\textit{name}\verb|)|, which is
252 also optional, can be used for drawing lines/arrows to/from the node.
256 \Tree [.CP [.NP \node(wh){what}; ]
259 [.NP [.Det the ] [.N cat ] ]
263 [.NP [.Det a ] [.N book ] ]
264 [.PP [.P about ] [.NP \node(t){$t$}; ] ] ] ] ] ] ] ]
265 \draw[semithick,->] (t)..controls +(south west:5) and +(south:5) .. (wh);
270 You can also refer to the whole subtree rooted at the node named \textit{name}\/ using \verb|\subtreeof{|\textit{name}\verb|}|:
272 \begin{SideBySideExample}
274 \Tree [.S [.NP [.Det the ] [.N cat ] ]
275 [.\node(site){VP}; [.V sat ] ] ]
276 \begin{scope}[shift={(1in,0.5in)}]
277 \Tree [.\node(root){VP}; VP$^\ast$
279 [.NP [.Det the ] [.N mat ] ] ] ]
283 \draw[->](\subtreeof{root}.140) ..
284 controls +(west:1) and +(east:1) .. (site);
287 \end{SideBySideExample}
290 \noindent Another example for machine translation people:
292 \fvset{formatcom=\javerbfont}
293 \begin{SideBySideExample}
295 \Tree [.S [.NP [.Det \node(e1){the}; ]
296 [.N \node(e2){cat}; ] ]
297 [.VP [.V \node(e3){sat}; ]
298 [.PP [.P \node(e4){on}; ]
299 [.NP [.Det \node(e5){the}; ]
300 [.N \node(e6){mat}; ] ] ] ] ]
302 \begin{scope}[yshift=-5in,grow'=up]
303 \tikzset{every leaf node/.style={font=\ja}}
304 \Tree [.S [.NP \node(j1){猫が}; ]
305 [.VP [.PP [.NP [.NP \node(j2){マット}; ]
306 [.Part \node(j3){の}; ]
307 [.NP \node(j4){上}; ] ]
308 [.P \node(j5){に}; ] ]
309 [.V \node(j6){土}; ] ] ]
312 \begin{scope}[dashed]
315 \draw (e3)..controls +(south:5) and +(north:4)..(j6);
323 \end{SideBySideExample}
326 \section{Explicit edges}
328 The edge from a parent to a child node is normally automatically drawn
329 for you, but you can do it yourself with an \verb|\edge| command
330 \emph{before}\/ the corresponding child node. It is similar to the
331 TikZ \verb|edge from parent| command.\footnote{Except that a TikZ
332 \texttt{edge from parent} comes after the child node. I thought it was
333 more logical to put it before.}
335 \verb|\edge [|\textit{options}\verb|];|
337 Again, don't forget the semicolon. The
338 \verb|[|\textit{options}\verb|]|, which are optional, let you change
339 the appearance of the edge. You can also add an edge label:
341 \verb|\edge [|\textit{options}\verb|] node [|\textit{options}\verb|] {|\textit{label}\verb|};|
343 Typically one will use the \verb|auto| option for edge labels, which
344 places the label to the side of the edge.
346 \begin{SideBySideExample}[xrightmargin=1.25in]
347 \newcommand{\initial}[1]{\ensuremath{\alpha_{\textrm{\scriptsize #1}}}}
348 \newcommand{\auxiliary}[1]{\ensuremath{\beta_{\textrm{\scriptsize #1}}}}
350 \begin{tikzpicture}[level distance=36pt,sibling distance=12pt]
351 \Tree [.\initial{sat}
352 \edge node[auto=right]{1}; \initial{cat}
353 \edge[dashed] node[auto=left]{2};
355 \edge node[auto=left]{2}; \initial{mat} ] ]
357 \end{SideBySideExample}
359 The fact that \verb|auto=left| draws a label on the right and
360 \verb|auto=right| draws a label on the left makes sense if you think
361 about the tree growing from the root to the leaves.
363 There is a predefined style that draws a ``roof'' over a node, like Qtree's \verb|\qroof|:
366 \begin{tikzpicture}[level distance=36pt]
367 \Tree [.S [.NP [.N this ] ]
369 [.NP \edge[roof]; {a noun phrase
370 the complexity of which
371 is too great for me to parse} ] ] ]
376 \section{Qtree compatibility}
378 For basic trees, \tikztree{} can be used as a drop-in replacement for Qtree, but most of Qtree's advanced features are either not accessed in the same way in \tikztree{} or not implemented at all. There is a package \verb|tikz-qtree-compat| which can be loaded to improve compatibility. Supported so far are:
380 \item Superscripts and subscripts outside of math mode, and \verb|\automath|
381 \item The \verb|\0|, \verb|\1|, and \verb|\2| commands, and \verb|\qtreeprimes|
382 \item The \verb|\qroof| command
384 For unsupported commands, warning messages are printed, but at least your file should compile.
386 \section*{Acknowledgements}
387 This was all Dan Gildea's idea. Thanks to Alan Munn for his very helpful suggestions.
390 Please send suggestions to \verb|chiang@isi.edu|.