
sawine@11: \documentclass{article}
sawine@11: 
sawine@11: \usepackage{xltxtra,fontspec}
sawine@11: 
sawine@11: \iffalse
sawine@11: \setmainfont[Mapping=tex-text]{DejaVu Serif}
sawine@11: \setmonofont[Scale=0.95]{DejaVu Sans Mono}
sawine@11: \newfontfamily\verbfont[Scale=0.8]{DejaVu Sans Mono}
sawine@11: \newfontfamily\ar[Script=Arabic]{DejaVu Sans}
sawine@11: \newfontfamily\ja{VL Gothic}
sawine@11: \newfontfamily\javerbfont[Scale=0.85,LetterSpace=5.0]{VL Gothic}
sawine@11: \fi
sawine@11: 
sawine@11: \iftrue
sawine@11: \setmainfont[Mapping=tex-text]{DejaVu Serif}
sawine@11: \setmonofont[Scale=0.95]{DejaVu Sans Mono}
sawine@11: \newfontfamily\verbfont[Scale=0.8]{DejaVu Sans Mono}
sawine@11: \newfontfamily\ar[Script=Arabic]{DejaVu Sans}
sawine@11: \newfontfamily\ja{Hiragino Maru Gothic Pro}
sawine@11: \newfontfamily\javerbfont[Scale=0.85,LetterSpace=5.0]{Hiragino Maru Gothic Pro}
sawine@11: \fi
sawine@11: 
sawine@11: \usepackage{tikz}
sawine@11: \usepackage{tikz-qtree}
sawine@11: 
sawine@11: \usepackage{fullpage}
sawine@11: 
sawine@11: \usepackage{fancyvrb,fvrb-ex}
sawine@11: \fvset{gobble=0,xleftmargin=0.5in,xrightmargin=2.75in,formatcom=\verbfont}
sawine@11: \VerbatimFootnotes
sawine@11: 
sawine@11: \newcommand\tikztree{\texttt{tikz-qtree}}
sawine@11: 
sawine@11: \tikzset{>=latex}
sawine@11: 
sawine@11: \title{\tikztree: better trees with TikZ}
sawine@11: \author{David Chiang}
sawine@11: \date{Version 1.11 (Christmas 2010)}
sawine@11: 
sawine@11: \begin{document}
sawine@11: 
sawine@11: \maketitle
sawine@11: 
sawine@11: The \tikztree{} package provides a macro for drawing trees with
sawine@11: TikZ\footnote{\texttt{http://sourceforge.net/projects/pgf/}} using the
sawine@11: easy syntax of Alexis Dimitriadis'
sawine@11: Qtree\footnote{\texttt{http://www.ling.upenn.edu/advice/latex/qtree/}}. It
sawine@11: improves on TikZ's standard tree-drawing facility by laying out tree
sawine@11: nodes without collisions; it improves on Qtree by adding lots of
sawine@11: features from TikZ; and it improves on \verb|pst-qtree| in being
sawine@11: usable with pdf\TeX{} and
sawine@11: \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.}
sawine@11: 
sawine@11: \section{Basics}
sawine@11: 
sawine@11: To load the package in \LaTeX{}:
sawine@11: \begin{Verbatim}
sawine@11: \usepackage{tikz}
sawine@11: \usepackage{tikz-qtree}
sawine@11: \end{Verbatim}
sawine@11: %
sawine@11: The simplest usage is identical to Qtree:
sawine@11: \begin{center}
sawine@11: \begin{SideBySideExample}
sawine@11: \Tree [.S [.NP [.Det the ] [.N cat ] ] 
sawine@11:           [.VP [.V sat ] 
sawine@11:                [.PP [.P on ] 
sawine@11:                     [.NP [.Det the ] [.N mat ] ] ] ] ]
sawine@11: \end{SideBySideExample}
sawine@11: \end{center}
sawine@11: Subtrees are delimited by square brackets. A subtree's root label is
sawine@11: joined by a dot (\verb|.|) to its opening bracket.\footnote{You can
sawine@11: also write the label after the closing bracket instead of the opening
sawine@11: bracket, or both, or neither. Please see the Qtree documentation for
sawine@11: details.} As in Qtree, spaces are required after every (internal or
sawine@11: leaf) node label. 
sawine@11: 
sawine@11: \verb|\Tree| works inside or outside a
sawine@11: \verb|tikzpicture| environment, but many of the features described 
sawine@11: below require the explicit \verb|tikzpicture| environment.
sawine@11: 
sawine@11: \goodbreak
sawine@11: 
sawine@11: \paragraph{Options} Some options for standard TikZ trees work for \verb|\Tree| as
sawine@11: well:
sawine@11: \begin{itemize}
sawine@11: \item \verb|level distance|: vertical distance between the anchors of a parent and its children
sawine@11: \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.
sawine@11: \end{itemize}
sawine@11: These are set either by writing
sawine@11: \verb|\tikzset{|\textit{option}\verb|=|\textit{value}\verb|}| or by
sawine@11: writing \verb|[|\textit{option}\verb|=|\textit{value}\verb|]| after a
sawine@11: \verb|\begin{tikzpicture}| or \verb|\begin{scope}|.\footnote{Allowing
sawine@11: options after \verb|\Tree| would have made sense, but there would be
sawine@11: no way to disambiguate the two uses of square brackets.} For example:
sawine@11: 
sawine@11: \begin{center}
sawine@11: \begin{SideBySideExample}
sawine@11: \begin{tikzpicture}
sawine@11: \tikzset{level distance=72pt}
sawine@11: \Tree [.NP [.Adj tall ] [.N tree ] ]
sawine@11: \end{tikzpicture}
sawine@11: %
sawine@11: \begin{tikzpicture}[sibling distance=72pt]
sawine@11: \Tree [.NP [.Adj fat ] [.N tree ] ]
sawine@11: \end{tikzpicture}
sawine@11: \end{SideBySideExample}
sawine@11: \end{center}
sawine@11: 
sawine@11: 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:
sawine@11: \begin{center}
sawine@11: \begin{SideBySideExample}
sawine@11: \begin{tikzpicture}[grow'=down]
sawine@11: \Tree [.NP [.Adj reverse ] [.N tree ] ]
sawine@11: \end{tikzpicture}
sawine@11: %
sawine@11: \begin{tikzpicture}[grow'=up]
sawine@11: \Tree [.NP [.Adj upside-down ] [.N tree ] ]
sawine@11: \end{tikzpicture}
sawine@11: \end{SideBySideExample}
sawine@11: \vspace{3ex}
sawine@11: \begin{SideBySideExample}
sawine@11: \begin{tikzpicture}[grow=left]
sawine@11: \tikzset{level distance=60pt,sibling distance=18pt}
sawine@11: \tikzset{execute at begin node=\strut}
sawine@11: \Tree [.NP [.Adj sideways ] [.N tree ] ]
sawine@11: \end{tikzpicture}
sawine@11: \end{SideBySideExample}
sawine@11: \end{center}
sawine@11: 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.
sawine@11: 
sawine@11: \paragraph{Styles} TikZ lets you define \emph{styles}\/ which encapsulate multiple options:
sawine@11: \begin{Verbatim}
sawine@11: \tikzset{small/.style={level distance=20pt,sibling distance=0pt}}
sawine@11: \end{Verbatim}
sawine@11: Then the option \verb|small| causes the options in its definition to be used:
sawine@11: \begin{center}
sawine@11: \tikzset{small/.style={level distance=20pt,sibling distance=0pt}}
sawine@11: \begin{SideBySideExample}
sawine@11: \begin{tikzpicture}[small]
sawine@11: \Tree [.NP [.Adj small ] [.N tree ] ]
sawine@11: \end{tikzpicture}
sawine@11: %
sawine@11: \begin{tikzpicture}
sawine@11: \Tree [.NP [.Adj normal ] [.N tree ] ]
sawine@11: \end{tikzpicture}
sawine@11: \end{SideBySideExample}
sawine@11: \end{center}
sawine@11: The following TikZ styles are automatically applied inside trees,
sawine@11: providing a hook for you to change the appearance of particular kinds
sawine@11: of tree parts:
sawine@11: \begin{itemize}
sawine@11: \item \verb|every tree node| to every (internal and leaf) node (default: \verb|anchor=base|)
sawine@11: \item \verb|every internal node| to every internal node
sawine@11: \item \verb|every leaf node| to every leaf node
sawine@11: \item \verb|edge from parent| to every edge (default: \verb|draw|)
sawine@11: \end{itemize}
sawine@11: 
sawine@11: The options for nodes and edges are all handled by TikZ and are
sawine@11: described in detail in the TikZ documentation. For example, if you
sawine@11: have a font named \verb|\ar| and want to set all the leaf labels in
sawine@11: this font:
sawine@11: \begin{center}
sawine@11: \begin{SideBySideExample}
sawine@11: \begin{tikzpicture}
sawine@11: \tikzset{grow'=down}
sawine@11: \tikzset{every leaf node/.style={font=\ar}}
sawine@11: \Tree [.S [.NP القط ] 
sawine@11:           [.VP [.V وجلس ] 
sawine@11:                [.PP [.P على ] [.NP حصيرة ] ] ] ]
sawine@11: \end{tikzpicture}
sawine@11: \end{SideBySideExample}
sawine@11: \end{center}
sawine@11: 
sawine@11: You can make the nodes in a sideways tree line up on their left edge using \verb|anchor=base west|:
sawine@11: \begin{center}
sawine@11: \begin{SideBySideExample}
sawine@11: \begin{tikzpicture}
sawine@11: \tikzset{grow'=right,level distance=32pt}
sawine@11: \tikzset{execute at begin node=\strut}
sawine@11: \tikzset{every tree node/.style={anchor=base west}}
sawine@11: \Tree [.S [.NP [.Det the ] [.N cat ] ] 
sawine@11:           [.VP [.V sat ] 
sawine@11:                [.PP [.P on ] 
sawine@11:                     [.NP [.Det the ] [.N mat ] ] ] ] ]
sawine@11: \end{tikzpicture}                    
sawine@11: \end{SideBySideExample}
sawine@11: \end{center}
sawine@11: 
sawine@11: 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|.}
sawine@11: \begin{center}
sawine@11: \begin{SideBySideExample}
sawine@11: \begin{tikzpicture}
sawine@11: \tikzset{every tree node/.style={align=center,anchor=north}}
sawine@11: \Tree [.S [.NP Det\\the N\\cat ]
sawine@11:           [.VP V\\sat
sawine@11:                [.PP P\\on
sawine@11:                     [.NP Det\\the N\\mat ] ] ] ]
sawine@11: \end{tikzpicture}
sawine@11: \end{SideBySideExample}
sawine@11: \end{center}
sawine@11: 
sawine@11: 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:
sawine@11: \begin{center}
sawine@11: \begin{SideBySideExample}
sawine@11: \begin{tikzpicture}
sawine@11: \tikzset{edge from parent/.style={draw,thick}}
sawine@11: \Tree [.S [.NP [.Det the ] [.N cat ] ] 
sawine@11:           [.VP [.V sat ] 
sawine@11:                [.PP [.P on ] 
sawine@11:                     [.NP [.Det the ] [.N mat ] ] ] ] ]
sawine@11: \end{tikzpicture}
sawine@11: \end{SideBySideExample}
sawine@11: \end{center}
sawine@11: The \verb|draw| option is necessary, as by default they will not be
sawine@11: drawn. As a more complex example, edges have an
sawine@11: \verb|edge from parent path| option which lets you change the shape of
sawine@11: the edge. Its value is a TikZ path expressed in terms of
sawine@11: \verb|\tikzparentnode|, the parent node, and \verb|\tikzchildnode|,
sawine@11: the child node.
sawine@11: \begin{center}
sawine@11: \begin{SideBySideExample}
sawine@11: \begin{tikzpicture}
sawine@11: \tikzset{edge from parent/.style=
sawine@11:   {draw,
sawine@11:    edge from parent path={(\tikzparentnode.south) 
sawine@11:                           -- +(0,-8pt) 
sawine@11:                           -| (\tikzchildnode)}}} 
sawine@11: \Tree [.S [.NP [.Det the ] [.N cat ] ] 
sawine@11:           [.VP [.V sat ] 
sawine@11:                [.PP [.P on ] 
sawine@11:                     [.NP [.Det the ] [.N mat ] ] ] ] ]
sawine@11: \end{tikzpicture}                    
sawine@11: \end{SideBySideExample}
sawine@11: \end{center}
sawine@11: 
sawine@11: \clearpage
sawine@11: 
sawine@11: \section{Embedding TikZ nodes}
sawine@11: 
sawine@11: Inside a \verb|\Tree|, in place of a node label, you can use a TikZ
sawine@11: \verb|\node| command.\footnote{\verb|\Tree| specifically watches out
sawine@11: for the token \verb|\node|; do not use \verb|\path node| or other
sawine@11: equivalents.}
sawine@11: \begin{quote}
sawine@11: \verb|\node [|\textit{options}\verb|] (|\textit{name}\verb|) {|\textit{label}\verb|};|
sawine@11: \end{quote}
sawine@11: Don't forget the terminating semicolon. The
sawine@11: \verb|[|\textit{options}\verb|]|, which are optional, let you change
sawine@11: the appearance of the node; for example, the \verb|draw| option draws
sawine@11: a border around the node. The \verb|(|\textit{name}\verb|)|, which is
sawine@11: also optional, can be used for drawing lines/arrows to/from the node.
sawine@11: \begin{center}
sawine@11: \begin{Example}
sawine@11: \begin{tikzpicture}
sawine@11: \Tree [.CP [.NP \node(wh){what}; ] 
sawine@11:            [.C$'$ [.I did ] 
sawine@11:                   [.\node[draw]{IP}; 
sawine@11:                     [.NP [.Det the ] [.N cat ] ]
sawine@11:                     [.VP [.V sit ] 
sawine@11:                          [.PP [.P on ] 
sawine@11:                               [.\node[draw]{NP}; 
sawine@11:                                 [.NP [.Det a ] [.N book ] ] 
sawine@11:                                 [.PP [.P about ] [.NP \node(t){$t$}; ] ] ] ] ] ] ] ]
sawine@11: \draw[semithick,->] (t)..controls +(south west:5) and +(south:5) .. (wh);
sawine@11: \end{tikzpicture}
sawine@11: \end{Example}
sawine@11: \end{center}
sawine@11: 
sawine@11: You can also refer to the whole subtree rooted at the node named \textit{name}\/ using \verb|\subtreeof{|\textit{name}\verb|}|:
sawine@11: \begin{center}
sawine@11: \begin{SideBySideExample}
sawine@11: \begin{tikzpicture}
sawine@11: \Tree [.S [.NP [.Det the ] [.N cat ] ] 
sawine@11:           [.\node(site){VP}; [.V sat ] ] ]
sawine@11: \begin{scope}[shift={(1in,0.5in)}]
sawine@11: \Tree [.\node(root){VP}; VP$^\ast$
sawine@11:                          [.PP [.P on ] 
sawine@11:                               [.NP [.Det the ] [.N mat ] ] ] ]
sawine@11: 
sawine@11: \end{scope}
sawine@11: 
sawine@11: \draw[->](\subtreeof{root}.140) .. 
sawine@11:   controls +(west:1) and +(east:1) .. (site);
sawine@11: 
sawine@11: \end{tikzpicture}
sawine@11: \end{SideBySideExample}
sawine@11: \end{center}
sawine@11: 
sawine@11: \noindent Another example for machine translation people:
sawine@11: \begin{center}
sawine@11: \fvset{formatcom=\javerbfont}
sawine@11: \begin{SideBySideExample}
sawine@11: \begin{tikzpicture}
sawine@11: \Tree [.S [.NP [.Det \node(e1){the}; ] 
sawine@11:                [.N \node(e2){cat}; ] ] 
sawine@11:           [.VP [.V \node(e3){sat}; ] 
sawine@11:                [.PP [.P \node(e4){on}; ] 
sawine@11:                     [.NP [.Det \node(e5){the}; ] 
sawine@11:                          [.N \node(e6){mat}; ] ] ] ] ]
sawine@11: 
sawine@11: \begin{scope}[yshift=-5in,grow'=up]
sawine@11: \tikzset{every leaf node/.style={font=\ja}}
sawine@11: \Tree [.S [.NP \node(j1){猫が}; ] 
sawine@11:           [.VP [.PP [.NP [.NP \node(j2){マット}; ] 
sawine@11:                          [.Part \node(j3){の}; ]
sawine@11:                          [.NP \node(j4){上}; ] ] 
sawine@11:                     [.P \node(j5){に}; ] ] 
sawine@11:                [.V \node(j6){土}; ] ] ]
sawine@11: \end{scope}
sawine@11: 
sawine@11: \begin{scope}[dashed]
sawine@11: \draw (e1)--(j1);
sawine@11: \draw (e2)--(j1);
sawine@11: \draw (e3)..controls +(south:5) and +(north:4)..(j6);
sawine@11: \draw (e4)--(j4);
sawine@11: \draw (e4)--(j5);
sawine@11: \draw (e5)--(j2);
sawine@11: \draw (e6)--(j2);
sawine@11: \end{scope}
sawine@11: 
sawine@11: \end{tikzpicture}
sawine@11: \end{SideBySideExample}
sawine@11: \end{center}
sawine@11: 
sawine@11: \section{Explicit edges}
sawine@11: 
sawine@11: The edge from a parent to a child node is normally automatically drawn
sawine@11: for you, but you can do it yourself with an \verb|\edge| command
sawine@11: \emph{before}\/ the corresponding child node. It is similar to the
sawine@11: TikZ \verb|edge from parent| command.\footnote{Except that a TikZ
sawine@11: \texttt{edge from parent} comes after the child node. I thought it was
sawine@11: more logical to put it before.}
sawine@11: \begin{quote}
sawine@11: \verb|\edge [|\textit{options}\verb|];|
sawine@11: \end{quote}
sawine@11: Again, don't forget the semicolon. The
sawine@11: \verb|[|\textit{options}\verb|]|, which are optional, let you change
sawine@11: the appearance of the edge. You can also add an edge label:
sawine@11: \begin{quote}
sawine@11: \verb|\edge [|\textit{options}\verb|] node [|\textit{options}\verb|] {|\textit{label}\verb|};|
sawine@11: \end{quote}
sawine@11: Typically one will use the \verb|auto| option for edge labels, which
sawine@11: places the label to the side of the edge.
sawine@11: \begin{center}
sawine@11: \begin{SideBySideExample}[xrightmargin=1.25in]
sawine@11: \newcommand{\initial}[1]{\ensuremath{\alpha_{\textrm{\scriptsize #1}}}}
sawine@11: \newcommand{\auxiliary}[1]{\ensuremath{\beta_{\textrm{\scriptsize #1}}}}
sawine@11: 
sawine@11: \begin{tikzpicture}[level distance=36pt,sibling distance=12pt]
sawine@11: \Tree [.\initial{sat} 
sawine@11:         \edge node[auto=right]{1}; \initial{cat} 
sawine@11:         \edge[dashed] node[auto=left]{2}; 
sawine@11:         [.\auxiliary{on} 
sawine@11:           \edge node[auto=left]{2}; \initial{mat} ] ]
sawine@11: \end{tikzpicture}
sawine@11: \end{SideBySideExample}
sawine@11: \end{center}
sawine@11: The fact that \verb|auto=left| draws a label on the right and
sawine@11: \verb|auto=right| draws a label on the left makes sense if you think
sawine@11: about the tree growing from the root to the leaves.
sawine@11: 
sawine@11: There is a predefined style that draws a ``roof'' over a node, like Qtree's \verb|\qroof|:
sawine@11: \begin{center}
sawine@11: \begin{Example}
sawine@11: \begin{tikzpicture}[level distance=36pt]
sawine@11: \Tree [.S [.NP [.N this ] ] 
sawine@11:           [.VP [.V is ] 
sawine@11:                [.NP \edge[roof]; {a noun phrase 
sawine@11:                                   the complexity of which 
sawine@11:                                   is too great for me to parse} ] ] ]
sawine@11: \end{tikzpicture}
sawine@11: \end{Example}
sawine@11: \end{center}
sawine@11: 
sawine@11: \section{Qtree compatibility}
sawine@11: 
sawine@11: 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:
sawine@11: \begin{itemize}
sawine@11: \item Superscripts and subscripts outside of math mode, and \verb|\automath|
sawine@11: \item The \verb|\0|, \verb|\1|, and \verb|\2| commands, and \verb|\qtreeprimes|
sawine@11: \item The \verb|\qroof| command
sawine@11: \end{itemize}
sawine@11: For unsupported commands, warning messages are printed, but at least your file should compile.
sawine@11: 
sawine@11: \section*{Acknowledgements}
sawine@11: This was all Dan Gildea's idea. Thanks to Alan Munn for his very helpful suggestions.
sawine@11: 
sawine@11: \section*{Contact}
sawine@11: Please send suggestions to \verb|chiang@isi.edu|.
sawine@11: 
sawine@11: \end{document}




