exercises/solutions/tikz-qtree.tex
changeset 9 b5c5ecd64f6a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/exercises/solutions/tikz-qtree.tex	Fri May 18 22:37:44 2012 +0200
     1.3 @@ -0,0 +1,181 @@
     1.4 +%    tikz-qtree.tex
     1.5 +%    Version 1.11, 25 Dec 2010
     1.6 +
     1.7 +%    Copyright (C) 2002, 2009 by David Chiang
     1.8 +
     1.9 +%    This program is free software; you can redistribute it and/or modify
    1.10 +%    it under the terms of the GNU General Public License as published by
    1.11 +%    the Free Software Foundation; either version 2 of the License, or
    1.12 +%    (at your option) any later version.
    1.13 +
    1.14 +%    This program is distributed in the hope that it will be useful,
    1.15 +%    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 +%    GNU General Public License for more details.
    1.18 +
    1.19 +%    You should have received a copy of the GNU General Public License along
    1.20 +%    with this program; if not, write to the Free Software Foundation, Inc.,
    1.21 +%    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    1.22 +
    1.23 +% New in version 1.11:
    1.24 +% - make options compatible with standard tikz trees
    1.25 +
    1.26 +% New in version 1.1:
    1.27 +
    1.28 +% - sideways trees
    1.29 +
    1.30 +%% These macros facilitate building up an object recursively before
    1.31 +%% putting it into the input stream.
    1.32 +
    1.33 +\newtoks\@result
    1.34 +\def\@call#1#2{\let\@cont=#2\bgroup\@result={}#1}
    1.35 +\def\@return{\global\@result=\@result\egroup\@cont}
    1.36 +
    1.37 +\def\@ifequal#1#2{\edef\testa{#1}\edef\testb{#2}\ifx\testa\testb}
    1.38 +
    1.39 +%% scan a tree: this just scans a subtree and then puts it onto the
    1.40 +%% input stream
    1.41 +
    1.42 +\def\Tree{\@call\@subtree\@Tree}
    1.43 +\def\@Tree{%
    1.44 +%\showthe\@result %debug
    1.45 +\ifpgfpicture % is there a test for tikzpicture?
    1.46 +\pgftree{\the\@result}%
    1.47 +\else
    1.48 +\tikzpicture[baseline]\pgftree{\the\@result}\endtikzpicture
    1.49 +\fi
    1.50 +}
    1.51 +
    1.52 +%% scan a subtree
    1.53 +\newtoks\child@list
    1.54 +\newtoks\root@node
    1.55 +
    1.56 +\def\@subtree[{%
    1.57 +\root@node={}%
    1.58 +\pgfutil@ifnextchar.{\@call\@interior\@@subtree}{\@@@subtree}}
    1.59 +\def\@@subtree{%
    1.60 +\root@node=\@result
    1.61 +\@@@subtree
    1.62 +}
    1.63 +\def\@@@subtree{%
    1.64 +\@call\@children\@@@@subtree
    1.65 +}
    1.66 +\def\@@@@subtree]{%
    1.67 +\child@list=\@result
    1.68 +\pgfutil@ifnextchar.{\@call\@interior\@@@@@subtree}{\@@@@@@subtree}}
    1.69 +\def\@@@@@subtree{%
    1.70 +%%% Check for mismatch.
    1.71 +\@ifequal{\the\root@node}{\pgfutil@empty}%
    1.72 +  \root@node=\@result
    1.73 +\fi
    1.74 +\@ifequal{\the\root@node}{\the\@result}\else
    1.75 +  \message{Warning: mismatched labels, \the\root@node{} and \the\@result.}%
    1.76 +\fi
    1.77 +\@@@@@@subtree
    1.78 +}
    1.79 +\def\@@@@@@subtree{%
    1.80 +\@ifequal{\the\root@node}{\pgfutil@empty}%
    1.81 +\edef\act{\noexpand\@result={\noexpand\pgfsubtree{\noexpand\path coordinate (\noexpand\nodename);}{\the\child@list}}}%
    1.82 +\else
    1.83 +\edef\act{\noexpand\@result={\noexpand\pgfsubtree{\the\root@node}{\the\child@list}}}%
    1.84 +\fi
    1.85 +\act
    1.86 +\@return}
    1.87 +
    1.88 +%% scan a sequence of subtrees or leaves
    1.89 +
    1.90 +\newif\ifscanned@edge
    1.91 +
    1.92 +\def\@children{%
    1.93 +\scanned@edgefalse
    1.94 +\child@list{}%
    1.95 +\@@children}
    1.96 +\def\@@children{%
    1.97 +\pgfutil@ifnextchar]{\@result\child@list\@return}{% end of children
    1.98 +\pgfutil@ifnextchar\edge{% explicit edge
    1.99 +\ifscanned@edge
   1.100 +\message{Warning: more than one edge given for a single child}\let\next\@@children % ignore
   1.101 +\else
   1.102 +\scanned@edgetrue\let\next\@@@children
   1.103 +\fi
   1.104 +\@call\@edge\next}{%
   1.105 +% else, a real node is next
   1.106 +\ifscanned@edge\else % no explicit edge, supply default
   1.107 +\expandafter\child@list\expandafter{\the\child@list{\edge@adapter{}}}%
   1.108 +\fi
   1.109 +\scanned@edgefalse
   1.110 +\pgfutil@ifnextchar[{\@call\@subtree\@@@children}% subtree
   1.111 +{\@call\@leaf\@@@children}% leaf
   1.112 +}}}
   1.113 +\def\@@@children{%
   1.114 +% wrap child inside curly braces
   1.115 +\expandafter\@result\expandafter{\expandafter{\the\@result}}%
   1.116 +\edef\act{\noexpand\child@list{\the\child@list \the\@result}}\act
   1.117 +\@@children
   1.118 +}
   1.119 +
   1.120 +\def\@interior.{\@result{\node[alias=\nodename][every tree node,every internal node]}\@label}
   1.121 +
   1.122 +\def\@leaf{\@call\@label\@@leaf}
   1.123 +\def\@@leaf{\edef\act{\noexpand\@result{\noexpand\pgfsubtree{\noexpand\node[alias=\noexpand\nodename][every tree node,every leaf node]\the\@result}{}}}\act\@return}
   1.124 +
   1.125 +\def\@edge\edge #1;{%
   1.126 +\@result{\edge@adapter{#1}}%
   1.127 +\@return}
   1.128 +\def\edge@adapter#1{%
   1.129 +\let\tikzparentnode\parentnodename
   1.130 +\let\tikzchildnode\nodename
   1.131 +\path edge from parent #1;%
   1.132 +}
   1.133 +
   1.134 +% a label is either text or PGF/TikZ code starting with \node
   1.135 +\def\@label{\pgfutil@ifnextchar\node{\@litlabel}{\@@label}}
   1.136 +\def\@@label#1 {%
   1.137 +\expandafter\@result\expandafter{\the\@result{#1};}%
   1.138 +\@return}
   1.139 +
   1.140 +% try to copy \node command into \@result without stripping braces
   1.141 +\def\@litlabel\node{\@@litlabel}
   1.142 +\def\@@litlabel{\pgfutil@ifnextchar\bgroup{\@@@litlabel}{\@@@@litlabel}}
   1.143 +\def\@@@litlabel#1{\expandafter\@result\expandafter{\the\@result {#1}}\@@litlabel}
   1.144 +\def\@@@@litlabel#1;{\expandafter\@result\expandafter{\the\@result #1;}\@return}
   1.145 +
   1.146 +% predefined edges
   1.147 +
   1.148 +\def\tree@edge#1#2{(#1.\pgftree@parentanchor) -- (#2.\pgftree@childanchor)}
   1.149 +
   1.150 +\def\roof@edge#1#2{\csname roof@edge@\leveldirection\endcsname{#1}{#2}}
   1.151 +\def\roof@edge@down#1#2{(#1.south) -- (#2.north west) -- (#2.north east) -- cycle}
   1.152 +\def\roof@edge@up#1#2{(#1.north) -- (#2.south west) -- (#2.south east) -- cycle}
   1.153 +\def\roof@edge@left#1#2{(#1.west) -- (#2.north east) -- (#2.south east) -- cycle}
   1.154 +\def\roof@edge@right#1#2{(#1.east) -- (#2.north west) -- (#2.south west) -- cycle}
   1.155 +
   1.156 +%%% Options
   1.157 +\pgfkeysgetvalue{/tikz/level distance/.@cmd}{\orig@leveldistance}
   1.158 +\tikzoption{level distance}{\pgfmathsetlength\levelsep{#1}\orig@leveldistance#1\pgfeov}
   1.159 +\pgfkeysgetvalue{/tikz/sibling distance/.@cmd}{\orig@siblingdistance}
   1.160 +\tikzoption{sibling distance}{\pgfmathsetlength\subtreesep{#1}\orig@siblingdistance#1\pgfeov} % different semantics
   1.161 +
   1.162 +% I don't really like this scheme
   1.163 +\pgfkeysgetvalue{/tikz/grow/.@cmd}{\orig@grow}
   1.164 +\tikzoption{grow}{\csname grow@#1\endcsname\orig@grow#1\pgfeov}
   1.165 +\pgfkeysgetvalue{/tikz/grow'/.@cmd}{\orig@growprime}
   1.166 +\tikzoption{grow'}{\csname growprime@#1\endcsname\orig@growprime#1\pgfeov}
   1.167 +\def\grow@up{\def\leveldirection{up}\def\siblingdirection{left}}
   1.168 +\def\grow@down{\def\leveldirection{down}\def\siblingdirection{right}}
   1.169 +\def\growprime@up{\def\leveldirection{up}\def\siblingdirection{right}}
   1.170 +\def\growprime@down{\def\leveldirection{down}\def\siblingdirection{left}}
   1.171 +\def\grow@left{\def\leveldirection{left}\def\siblingdirection{down}}
   1.172 +\def\grow@right{\def\leveldirection{right}\def\siblingdirection{up}}
   1.173 +\def\growprime@left{\def\leveldirection{left}\def\siblingdirection{up}}
   1.174 +\def\growprime@right{\def\leveldirection{right}\def\siblingdirection{down}}
   1.175 +
   1.176 +% defaults appropriate for linguistic trees
   1.177 +\tikzset{every tree node/.style={anchor=base}}
   1.178 +\tikzset{every leaf node/.style={}}
   1.179 +\tikzset{every internal node/.style={}}
   1.180 +\def\tikz@edge@to@parent@path{\tree@edge{\tikzparentnode}{\tikzchildnode}}
   1.181 +
   1.182 +% predefined roof style
   1.183 +\tikzset{roof/.style={edge from parent path=\roof@edge{\tikzparentnode}{\tikzchildnode}}}
   1.184 +