exercises/solutions/tikz-qtree.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 %    tikz-qtree.tex
     2 %    Version 1.11, 25 Dec 2010
     3 
     4 %    Copyright (C) 2002, 2009 by David Chiang
     5 
     6 %    This program is free software; you can redistribute it and/or modify
     7 %    it under the terms of the GNU General Public License as published by
     8 %    the Free Software Foundation; either version 2 of the License, or
     9 %    (at your option) any later version.
    10 
    11 %    This program is distributed in the hope that it will be useful,
    12 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
    13 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14 %    GNU General Public License for more details.
    15 
    16 %    You should have received a copy of the GNU General Public License along
    17 %    with this program; if not, write to the Free Software Foundation, Inc.,
    18 %    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    19 
    20 % New in version 1.11:
    21 % - make options compatible with standard tikz trees
    22 
    23 % New in version 1.1:
    24 
    25 % - sideways trees
    26 
    27 %% These macros facilitate building up an object recursively before
    28 %% putting it into the input stream.
    29 
    30 \newtoks\@result
    31 \def\@call#1#2{\let\@cont=#2\bgroup\@result={}#1}
    32 \def\@return{\global\@result=\@result\egroup\@cont}
    33 
    34 \def\@ifequal#1#2{\edef\testa{#1}\edef\testb{#2}\ifx\testa\testb}
    35 
    36 %% scan a tree: this just scans a subtree and then puts it onto the
    37 %% input stream
    38 
    39 \def\Tree{\@call\@subtree\@Tree}
    40 \def\@Tree{%
    41 %\showthe\@result %debug
    42 \ifpgfpicture % is there a test for tikzpicture?
    43 \pgftree{\the\@result}%
    44 \else
    45 \tikzpicture[baseline]\pgftree{\the\@result}\endtikzpicture
    46 \fi
    47 }
    48 
    49 %% scan a subtree
    50 \newtoks\child@list
    51 \newtoks\root@node
    52 
    53 \def\@subtree[{%
    54 \root@node={}%
    55 \pgfutil@ifnextchar.{\@call\@interior\@@subtree}{\@@@subtree}}
    56 \def\@@subtree{%
    57 \root@node=\@result
    58 \@@@subtree
    59 }
    60 \def\@@@subtree{%
    61 \@call\@children\@@@@subtree
    62 }
    63 \def\@@@@subtree]{%
    64 \child@list=\@result
    65 \pgfutil@ifnextchar.{\@call\@interior\@@@@@subtree}{\@@@@@@subtree}}
    66 \def\@@@@@subtree{%
    67 %%% Check for mismatch.
    68 \@ifequal{\the\root@node}{\pgfutil@empty}%
    69   \root@node=\@result
    70 \fi
    71 \@ifequal{\the\root@node}{\the\@result}\else
    72   \message{Warning: mismatched labels, \the\root@node{} and \the\@result.}%
    73 \fi
    74 \@@@@@@subtree
    75 }
    76 \def\@@@@@@subtree{%
    77 \@ifequal{\the\root@node}{\pgfutil@empty}%
    78 \edef\act{\noexpand\@result={\noexpand\pgfsubtree{\noexpand\path coordinate (\noexpand\nodename);}{\the\child@list}}}%
    79 \else
    80 \edef\act{\noexpand\@result={\noexpand\pgfsubtree{\the\root@node}{\the\child@list}}}%
    81 \fi
    82 \act
    83 \@return}
    84 
    85 %% scan a sequence of subtrees or leaves
    86 
    87 \newif\ifscanned@edge
    88 
    89 \def\@children{%
    90 \scanned@edgefalse
    91 \child@list{}%
    92 \@@children}
    93 \def\@@children{%
    94 \pgfutil@ifnextchar]{\@result\child@list\@return}{% end of children
    95 \pgfutil@ifnextchar\edge{% explicit edge
    96 \ifscanned@edge
    97 \message{Warning: more than one edge given for a single child}\let\next\@@children % ignore
    98 \else
    99 \scanned@edgetrue\let\next\@@@children
   100 \fi
   101 \@call\@edge\next}{%
   102 % else, a real node is next
   103 \ifscanned@edge\else % no explicit edge, supply default
   104 \expandafter\child@list\expandafter{\the\child@list{\edge@adapter{}}}%
   105 \fi
   106 \scanned@edgefalse
   107 \pgfutil@ifnextchar[{\@call\@subtree\@@@children}% subtree
   108 {\@call\@leaf\@@@children}% leaf
   109 }}}
   110 \def\@@@children{%
   111 % wrap child inside curly braces
   112 \expandafter\@result\expandafter{\expandafter{\the\@result}}%
   113 \edef\act{\noexpand\child@list{\the\child@list \the\@result}}\act
   114 \@@children
   115 }
   116 
   117 \def\@interior.{\@result{\node[alias=\nodename][every tree node,every internal node]}\@label}
   118 
   119 \def\@leaf{\@call\@label\@@leaf}
   120 \def\@@leaf{\edef\act{\noexpand\@result{\noexpand\pgfsubtree{\noexpand\node[alias=\noexpand\nodename][every tree node,every leaf node]\the\@result}{}}}\act\@return}
   121 
   122 \def\@edge\edge #1;{%
   123 \@result{\edge@adapter{#1}}%
   124 \@return}
   125 \def\edge@adapter#1{%
   126 \let\tikzparentnode\parentnodename
   127 \let\tikzchildnode\nodename
   128 \path edge from parent #1;%
   129 }
   130 
   131 % a label is either text or PGF/TikZ code starting with \node
   132 \def\@label{\pgfutil@ifnextchar\node{\@litlabel}{\@@label}}
   133 \def\@@label#1 {%
   134 \expandafter\@result\expandafter{\the\@result{#1};}%
   135 \@return}
   136 
   137 % try to copy \node command into \@result without stripping braces
   138 \def\@litlabel\node{\@@litlabel}
   139 \def\@@litlabel{\pgfutil@ifnextchar\bgroup{\@@@litlabel}{\@@@@litlabel}}
   140 \def\@@@litlabel#1{\expandafter\@result\expandafter{\the\@result {#1}}\@@litlabel}
   141 \def\@@@@litlabel#1;{\expandafter\@result\expandafter{\the\@result #1;}\@return}
   142 
   143 % predefined edges
   144 
   145 \def\tree@edge#1#2{(#1.\pgftree@parentanchor) -- (#2.\pgftree@childanchor)}
   146 
   147 \def\roof@edge#1#2{\csname roof@edge@\leveldirection\endcsname{#1}{#2}}
   148 \def\roof@edge@down#1#2{(#1.south) -- (#2.north west) -- (#2.north east) -- cycle}
   149 \def\roof@edge@up#1#2{(#1.north) -- (#2.south west) -- (#2.south east) -- cycle}
   150 \def\roof@edge@left#1#2{(#1.west) -- (#2.north east) -- (#2.south east) -- cycle}
   151 \def\roof@edge@right#1#2{(#1.east) -- (#2.north west) -- (#2.south west) -- cycle}
   152 
   153 %%% Options
   154 \pgfkeysgetvalue{/tikz/level distance/.@cmd}{\orig@leveldistance}
   155 \tikzoption{level distance}{\pgfmathsetlength\levelsep{#1}\orig@leveldistance#1\pgfeov}
   156 \pgfkeysgetvalue{/tikz/sibling distance/.@cmd}{\orig@siblingdistance}
   157 \tikzoption{sibling distance}{\pgfmathsetlength\subtreesep{#1}\orig@siblingdistance#1\pgfeov} % different semantics
   158 
   159 % I don't really like this scheme
   160 \pgfkeysgetvalue{/tikz/grow/.@cmd}{\orig@grow}
   161 \tikzoption{grow}{\csname grow@#1\endcsname\orig@grow#1\pgfeov}
   162 \pgfkeysgetvalue{/tikz/grow'/.@cmd}{\orig@growprime}
   163 \tikzoption{grow'}{\csname growprime@#1\endcsname\orig@growprime#1\pgfeov}
   164 \def\grow@up{\def\leveldirection{up}\def\siblingdirection{left}}
   165 \def\grow@down{\def\leveldirection{down}\def\siblingdirection{right}}
   166 \def\growprime@up{\def\leveldirection{up}\def\siblingdirection{right}}
   167 \def\growprime@down{\def\leveldirection{down}\def\siblingdirection{left}}
   168 \def\grow@left{\def\leveldirection{left}\def\siblingdirection{down}}
   169 \def\grow@right{\def\leveldirection{right}\def\siblingdirection{up}}
   170 \def\growprime@left{\def\leveldirection{left}\def\siblingdirection{up}}
   171 \def\growprime@right{\def\leveldirection{right}\def\siblingdirection{down}}
   172 
   173 % defaults appropriate for linguistic trees
   174 \tikzset{every tree node/.style={anchor=base}}
   175 \tikzset{every leaf node/.style={}}
   176 \tikzset{every internal node/.style={}}
   177 \def\tikz@edge@to@parent@path{\tree@edge{\tikzparentnode}{\tikzchildnode}}
   178 
   179 % predefined roof style
   180 \tikzset{roof/.style={edge from parent path=\roof@edge{\tikzparentnode}{\tikzchildnode}}}
   181