exercises/solutions/tikz-qtree-manual.tex
author Eugen Sawin <sawine@me73.com>
Sun, 10 Jun 2012 05:35:08 +0200
changeset 20 93fd38efa60f
permissions -rw-r--r--
Added some solutions.
     1 \documentclass{article}
     2 
     3 \usepackage{xltxtra,fontspec}
     4 
     5 \iffalse
     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}
    12 \fi
    13 
    14 \iftrue
    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}
    21 \fi
    22 
    23 \usepackage{tikz}
    24 \usepackage{tikz-qtree}
    25 
    26 \usepackage{fullpage}
    27 
    28 \usepackage{fancyvrb,fvrb-ex}
    29 \fvset{gobble=0,xleftmargin=0.5in,xrightmargin=2.75in,formatcom=\verbfont}
    30 \VerbatimFootnotes
    31 
    32 \newcommand\tikztree{\texttt{tikz-qtree}}
    33 
    34 \tikzset{>=latex}
    35 
    36 \title{\tikztree: better trees with TikZ}
    37 \author{David Chiang}
    38 \date{Version 1.11 (Christmas 2010)}
    39 
    40 \begin{document}
    41 
    42 \maketitle
    43 
    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.}
    53 
    54 \section{Basics}
    55 
    56 To load the package in \LaTeX{}:
    57 \begin{Verbatim}
    58 \usepackage{tikz}
    59 \usepackage{tikz-qtree}
    60 \end{Verbatim}
    61 %
    62 The simplest usage is identical to Qtree:
    63 \begin{center}
    64 \begin{SideBySideExample}
    65 \Tree [.S [.NP [.Det the ] [.N cat ] ] 
    66           [.VP [.V sat ] 
    67                [.PP [.P on ] 
    68                     [.NP [.Det the ] [.N mat ] ] ] ] ]
    69 \end{SideBySideExample}
    70 \end{center}
    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
    76 leaf) node label. 
    77 
    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.
    81 
    82 \goodbreak
    83 
    84 \paragraph{Options} Some options for standard TikZ trees work for \verb|\Tree| as
    85 well:
    86 \begin{itemize}
    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.
    89 \end{itemize}
    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:
    96 
    97 \begin{center}
    98 \begin{SideBySideExample}
    99 \begin{tikzpicture}
   100 \tikzset{level distance=72pt}
   101 \Tree [.NP [.Adj tall ] [.N tree ] ]
   102 \end{tikzpicture}
   103 %
   104 \begin{tikzpicture}[sibling distance=72pt]
   105 \Tree [.NP [.Adj fat ] [.N tree ] ]
   106 \end{tikzpicture}
   107 \end{SideBySideExample}
   108 \end{center}
   109 
   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:
   111 \begin{center}
   112 \begin{SideBySideExample}
   113 \begin{tikzpicture}[grow'=down]
   114 \Tree [.NP [.Adj reverse ] [.N tree ] ]
   115 \end{tikzpicture}
   116 %
   117 \begin{tikzpicture}[grow'=up]
   118 \Tree [.NP [.Adj upside-down ] [.N tree ] ]
   119 \end{tikzpicture}
   120 \end{SideBySideExample}
   121 \vspace{3ex}
   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 ] ]
   127 \end{tikzpicture}
   128 \end{SideBySideExample}
   129 \end{center}
   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.
   131 
   132 \paragraph{Styles} TikZ lets you define \emph{styles}\/ which encapsulate multiple options:
   133 \begin{Verbatim}
   134 \tikzset{small/.style={level distance=20pt,sibling distance=0pt}}
   135 \end{Verbatim}
   136 Then the option \verb|small| causes the options in its definition to be used:
   137 \begin{center}
   138 \tikzset{small/.style={level distance=20pt,sibling distance=0pt}}
   139 \begin{SideBySideExample}
   140 \begin{tikzpicture}[small]
   141 \Tree [.NP [.Adj small ] [.N tree ] ]
   142 \end{tikzpicture}
   143 %
   144 \begin{tikzpicture}
   145 \Tree [.NP [.Adj normal ] [.N tree ] ]
   146 \end{tikzpicture}
   147 \end{SideBySideExample}
   148 \end{center}
   149 The following TikZ styles are automatically applied inside trees,
   150 providing a hook for you to change the appearance of particular kinds
   151 of tree parts:
   152 \begin{itemize}
   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|)
   157 \end{itemize}
   158 
   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
   162 this font:
   163 \begin{center}
   164 \begin{SideBySideExample}
   165 \begin{tikzpicture}
   166 \tikzset{grow'=down}
   167 \tikzset{every leaf node/.style={font=\ar}}
   168 \Tree [.S [.NP القط ] 
   169           [.VP [.V وجلس ] 
   170                [.PP [.P على ] [.NP حصيرة ] ] ] ]
   171 \end{tikzpicture}
   172 \end{SideBySideExample}
   173 \end{center}
   174 
   175 You can make the nodes in a sideways tree line up on their left edge using \verb|anchor=base west|:
   176 \begin{center}
   177 \begin{SideBySideExample}
   178 \begin{tikzpicture}
   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 ] ] 
   183           [.VP [.V sat ] 
   184                [.PP [.P on ] 
   185                     [.NP [.Det the ] [.N mat ] ] ] ] ]
   186 \end{tikzpicture}                    
   187 \end{SideBySideExample}
   188 \end{center}
   189 
   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|.}
   191 \begin{center}
   192 \begin{SideBySideExample}
   193 \begin{tikzpicture}
   194 \tikzset{every tree node/.style={align=center,anchor=north}}
   195 \Tree [.S [.NP Det\\the N\\cat ]
   196           [.VP V\\sat
   197                [.PP P\\on
   198                     [.NP Det\\the N\\mat ] ] ] ]
   199 \end{tikzpicture}
   200 \end{SideBySideExample}
   201 \end{center}
   202 
   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:
   204 \begin{center}
   205 \begin{SideBySideExample}
   206 \begin{tikzpicture}
   207 \tikzset{edge from parent/.style={draw,thick}}
   208 \Tree [.S [.NP [.Det the ] [.N cat ] ] 
   209           [.VP [.V sat ] 
   210                [.PP [.P on ] 
   211                     [.NP [.Det the ] [.N mat ] ] ] ] ]
   212 \end{tikzpicture}
   213 \end{SideBySideExample}
   214 \end{center}
   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|,
   220 the child node.
   221 \begin{center}
   222 \begin{SideBySideExample}
   223 \begin{tikzpicture}
   224 \tikzset{edge from parent/.style=
   225   {draw,
   226    edge from parent path={(\tikzparentnode.south) 
   227                           -- +(0,-8pt) 
   228                           -| (\tikzchildnode)}}} 
   229 \Tree [.S [.NP [.Det the ] [.N cat ] ] 
   230           [.VP [.V sat ] 
   231                [.PP [.P on ] 
   232                     [.NP [.Det the ] [.N mat ] ] ] ] ]
   233 \end{tikzpicture}                    
   234 \end{SideBySideExample}
   235 \end{center}
   236 
   237 \clearpage
   238 
   239 \section{Embedding TikZ nodes}
   240 
   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
   244 equivalents.}
   245 \begin{quote}
   246 \verb|\node [|\textit{options}\verb|] (|\textit{name}\verb|) {|\textit{label}\verb|};|
   247 \end{quote}
   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.
   253 \begin{center}
   254 \begin{Example}
   255 \begin{tikzpicture}
   256 \Tree [.CP [.NP \node(wh){what}; ] 
   257            [.C$'$ [.I did ] 
   258                   [.\node[draw]{IP}; 
   259                     [.NP [.Det the ] [.N cat ] ]
   260                     [.VP [.V sit ] 
   261                          [.PP [.P on ] 
   262                               [.\node[draw]{NP}; 
   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);
   266 \end{tikzpicture}
   267 \end{Example}
   268 \end{center}
   269 
   270 You can also refer to the whole subtree rooted at the node named \textit{name}\/ using \verb|\subtreeof{|\textit{name}\verb|}|:
   271 \begin{center}
   272 \begin{SideBySideExample}
   273 \begin{tikzpicture}
   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$
   278                          [.PP [.P on ] 
   279                               [.NP [.Det the ] [.N mat ] ] ] ]
   280 
   281 \end{scope}
   282 
   283 \draw[->](\subtreeof{root}.140) .. 
   284   controls +(west:1) and +(east:1) .. (site);
   285 
   286 \end{tikzpicture}
   287 \end{SideBySideExample}
   288 \end{center}
   289 
   290 \noindent Another example for machine translation people:
   291 \begin{center}
   292 \fvset{formatcom=\javerbfont}
   293 \begin{SideBySideExample}
   294 \begin{tikzpicture}
   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}; ] ] ] ] ]
   301 
   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){土}; ] ] ]
   310 \end{scope}
   311 
   312 \begin{scope}[dashed]
   313 \draw (e1)--(j1);
   314 \draw (e2)--(j1);
   315 \draw (e3)..controls +(south:5) and +(north:4)..(j6);
   316 \draw (e4)--(j4);
   317 \draw (e4)--(j5);
   318 \draw (e5)--(j2);
   319 \draw (e6)--(j2);
   320 \end{scope}
   321 
   322 \end{tikzpicture}
   323 \end{SideBySideExample}
   324 \end{center}
   325 
   326 \section{Explicit edges}
   327 
   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.}
   334 \begin{quote}
   335 \verb|\edge [|\textit{options}\verb|];|
   336 \end{quote}
   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:
   340 \begin{quote}
   341 \verb|\edge [|\textit{options}\verb|] node [|\textit{options}\verb|] {|\textit{label}\verb|};|
   342 \end{quote}
   343 Typically one will use the \verb|auto| option for edge labels, which
   344 places the label to the side of the edge.
   345 \begin{center}
   346 \begin{SideBySideExample}[xrightmargin=1.25in]
   347 \newcommand{\initial}[1]{\ensuremath{\alpha_{\textrm{\scriptsize #1}}}}
   348 \newcommand{\auxiliary}[1]{\ensuremath{\beta_{\textrm{\scriptsize #1}}}}
   349 
   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}; 
   354         [.\auxiliary{on} 
   355           \edge node[auto=left]{2}; \initial{mat} ] ]
   356 \end{tikzpicture}
   357 \end{SideBySideExample}
   358 \end{center}
   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.
   362 
   363 There is a predefined style that draws a ``roof'' over a node, like Qtree's \verb|\qroof|:
   364 \begin{center}
   365 \begin{Example}
   366 \begin{tikzpicture}[level distance=36pt]
   367 \Tree [.S [.NP [.N this ] ] 
   368           [.VP [.V is ] 
   369                [.NP \edge[roof]; {a noun phrase 
   370                                   the complexity of which 
   371                                   is too great for me to parse} ] ] ]
   372 \end{tikzpicture}
   373 \end{Example}
   374 \end{center}
   375 
   376 \section{Qtree compatibility}
   377 
   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:
   379 \begin{itemize}
   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
   383 \end{itemize}
   384 For unsupported commands, warning messages are printed, but at least your file should compile.
   385 
   386 \section*{Acknowledgements}
   387 This was all Dan Gildea's idea. Thanks to Alan Munn for his very helpful suggestions.
   388 
   389 \section*{Contact}
   390 Please send suggestions to \verb|chiang@isi.edu|.
   391 
   392 \end{document}