| 1 |
% tikz-qtree.tex |
| 2 |
% Version 1.2, 20 Apr 2012 |
| 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.2: |
| 21 |
% - level-specific styles (thanks to Andrew Stacey) |
| 22 |
|
| 23 |
% New in version 1.11: |
| 24 |
% - make options compatible with standard tikz trees |
| 25 |
|
| 26 |
% New in version 1.1: |
| 27 |
% - sideways trees |
| 28 |
|
| 29 |
%% These macros facilitate building up an object recursively before |
| 30 |
%% putting it into the input stream. |
| 31 |
|
| 32 |
\newtoks\@result |
| 33 |
\def\@call#1#2{\let\@cont=#2\bgroup\@result={}#1} |
| 34 |
\def\@return{\global\@result=\@result\egroup\@cont} |
| 35 |
|
| 36 |
\def\@ifequal#1#2{\edef\testa{#1}\edef\testb{#2}\ifx\testa\testb} |
| 37 |
|
| 38 |
%% scan a tree: this just scans a subtree and then puts it onto the |
| 39 |
%% input stream |
| 40 |
|
| 41 |
\def\Tree{\@call\@subtree\@Tree} |
| 42 |
\def\@Tree{% |
| 43 |
%\showthe\@result %debug |
| 44 |
\ifpgfpicture % is there a test for tikzpicture? |
| 45 |
\pgftree{\the\@result}% |
| 46 |
\else |
| 47 |
\tikzpicture[baseline]\pgftree{\the\@result}\endtikzpicture |
| 48 |
\fi |
| 49 |
} |
| 50 |
|
| 51 |
%% scan a subtree |
| 52 |
\newtoks\child@list |
| 53 |
\newtoks\root@node |
| 54 |
|
| 55 |
\def\@subtree[{% |
| 56 |
\root@node={}% |
| 57 |
\pgfutil@ifnextchar.{\@call\@interior\@@subtree}{\@@@subtree}} |
| 58 |
\def\@@subtree{% |
| 59 |
\root@node=\@result |
| 60 |
\@@@subtree |
| 61 |
} |
| 62 |
\def\@@@subtree{% |
| 63 |
\@call\@children\@@@@subtree |
| 64 |
} |
| 65 |
\def\@@@@subtree]{% |
| 66 |
\child@list=\@result |
| 67 |
\pgfutil@ifnextchar.{\@call\@interior\@@@@@subtree}{\@@@@@@subtree}} |
| 68 |
\def\@@@@@subtree{% |
| 69 |
%%% Check for mismatch. |
| 70 |
\@ifequal{\the\root@node}{\pgfutil@empty}% |
| 71 |
\root@node=\@result |
| 72 |
\fi |
| 73 |
\@ifequal{\the\root@node}{\the\@result}\else |
| 74 |
\message{Warning: mismatched labels, \the\root@node{} and \the\@result.}% |
| 75 |
\fi |
| 76 |
\@@@@@@subtree |
| 77 |
} |
| 78 |
\def\@@@@@@subtree{% |
| 79 |
\@ifequal{\the\root@node}{\pgfutil@empty}% |
| 80 |
\edef\act{\noexpand\@result={% |
| 81 |
\noexpand\subtree@hook |
| 82 |
\noexpand\pgfsubtree{\noexpand\path coordinate (\noexpand\nodename);}{\the\child@list}}}% |
| 83 |
\else |
| 84 |
\edef\act{\noexpand\@result={% |
| 85 |
\noexpand\subtree@hook |
| 86 |
\noexpand\pgfsubtree{\the\root@node}{\the\child@list}}}% |
| 87 |
\fi |
| 88 |
\act |
| 89 |
\@return} |
| 90 |
|
| 91 |
%% scan a sequence of subtrees or leaves |
| 92 |
|
| 93 |
\newif\ifscanned@edge |
| 94 |
|
| 95 |
\def\@children{% |
| 96 |
\scanned@edgefalse |
| 97 |
\child@list{}% |
| 98 |
\@@children} |
| 99 |
\def\@@children{% |
| 100 |
\pgfutil@ifnextchar]{\@result\child@list\@return}{% end of children |
| 101 |
\pgfutil@ifnextchar\edge{% explicit edge |
| 102 |
\ifscanned@edge |
| 103 |
\message{Warning: more than one edge given for a single child}\let\next\@@children % ignore |
| 104 |
\else |
| 105 |
\scanned@edgetrue\let\next\@@@children |
| 106 |
\fi |
| 107 |
\@call\@edge\next}{% |
| 108 |
% else, a real node is next |
| 109 |
\ifscanned@edge\else % no explicit edge, supply default |
| 110 |
\expandafter\child@list\expandafter{\the\child@list{\edge@adapter{}}}% |
| 111 |
\fi |
| 112 |
\scanned@edgefalse |
| 113 |
\pgfutil@ifnextchar[{% subtree |
| 114 |
\expandafter\child@list\expandafter{\the\child@list{\level@hook\interior@hook}}% |
| 115 |
\@call\@subtree\@@@children}% |
| 116 |
{% leaf |
| 117 |
\expandafter\child@list\expandafter{\the\child@list{\level@hook\frontier@hook}}% |
| 118 |
\@call\@leaf\@@@children}% |
| 119 |
}}} |
| 120 |
\def\@@@children{% |
| 121 |
% wrap child inside curly braces |
| 122 |
\expandafter\@result\expandafter{\expandafter{\the\@result}}% |
| 123 |
\edef\act{\noexpand\child@list{\the\child@list \the\@result}}\act |
| 124 |
\@@children |
| 125 |
} |
| 126 |
|
| 127 |
\def\@interior.{\@result{\node[alias=\nodename][every tree node,every internal node,every level \the\pgftree@level\space node/.try]}\@label} |
| 128 |
|
| 129 |
\def\@leaf{\@call\@label\@@leaf} |
| 130 |
\def\@@leaf{\edef\act{\noexpand\@result{% |
| 131 |
\noexpand\subtree@hook |
| 132 |
\noexpand\pgfsubtree{\noexpand\node[alias=\noexpand\nodename][every tree node,every leaf node,every level \noexpand\the\pgftree@level\space node/.try]\the\@result}{}}}\act\@return} |
| 133 |
|
| 134 |
\def\@edge\edge #1;{% |
| 135 |
\@result{\edge@adapter{#1}}% |
| 136 |
\@return} |
| 137 |
\def\edge@adapter#1{% |
| 138 |
\let\tikzparentnode\parentnodename |
| 139 |
\let\tikzchildnode\nodename |
| 140 |
\path edge from parent #1;% |
| 141 |
} |
| 142 |
|
| 143 |
\def\level@hook{% |
| 144 |
{\advance\pgftree@level by 1\relax |
| 145 |
\xdef\@act{\noexpand\tikzset{level \the\pgftree@level/.try}}}\@act} |
| 146 |
\def\subtree@hook{% |
| 147 |
%\edef\act{\noexpand\tikzset{every tree node/.append code={\noexpand\tikzset{every level \the\pgftree@level+ node/.try}}}}\act |
| 148 |
{\advance\pgftree@level by 1\relax |
| 149 |
\xdef\@act{\noexpand\tikzset{level \the\pgftree@level+/.try}}}\@act |
| 150 |
} |
| 151 |
\def\interior@hook{\tikzset{interior/.try}} |
| 152 |
\def\frontier@hook{\tikzset{frontier/.try}} |
| 153 |
|
| 154 |
% a label is either text or PGF/TikZ code starting with \node |
| 155 |
\def\@label{\pgfutil@ifnextchar\node{\@litlabel}{\@@label}} |
| 156 |
\def\@@label#1 {% |
| 157 |
\expandafter\@result\expandafter{\the\@result{#1};}% |
| 158 |
\@return} |
| 159 |
|
| 160 |
% try to copy \node command into \@result without stripping braces |
| 161 |
\def\@litlabel\node{\@@litlabel} |
| 162 |
\def\@@litlabel{\pgfutil@ifnextchar\bgroup{\@@@litlabel}{\@@@@litlabel}} |
| 163 |
\def\@@@litlabel#1{\expandafter\@result\expandafter{\the\@result {#1}}\@@litlabel} |
| 164 |
\def\@@@@litlabel#1;{\expandafter\@result\expandafter{\the\@result #1;}\@return} |
| 165 |
|
| 166 |
% predefined edges |
| 167 |
|
| 168 |
\def\tree@edge#1#2{(#1.\pgftree@parentanchor) -- (#2.\pgftree@childanchor)} |
| 169 |
|
| 170 |
\def\roof@edge#1#2{\csname roof@edge@\leveldirection\endcsname{#1}{#2}} |
| 171 |
\def\roof@edge@down#1#2{(#1.south) -- (#2.north west) -- (#2.north east) -- cycle} |
| 172 |
\def\roof@edge@up#1#2{(#1.north) -- (#2.south west) -- (#2.south east) -- cycle} |
| 173 |
\def\roof@edge@left#1#2{(#1.west) -- (#2.north east) -- (#2.south east) -- cycle} |
| 174 |
\def\roof@edge@right#1#2{(#1.east) -- (#2.north west) -- (#2.south west) -- cycle} |
| 175 |
|
| 176 |
%%% Options |
| 177 |
\pgfkeysgetvalue{/tikz/level distance/.@cmd}{\orig@leveldistance} |
| 178 |
\tikzoption{level distance}{\pgfmathsetlength\levelsep{#1}\orig@leveldistance#1\pgfeov} |
| 179 |
\tikzoption{distance from root}{\pgfmathsetlength\levelsep{#1}\advance\levelsep by -\pgftree@depth} |
| 180 |
\pgfkeysgetvalue{/tikz/sibling distance/.@cmd}{\orig@siblingdistance} |
| 181 |
\tikzoption{sibling distance}{\pgfmathsetlength\subtreesep{#1}\orig@siblingdistance#1\pgfeov} % different semantics |
| 182 |
|
| 183 |
% I don't really like this scheme |
| 184 |
\pgfkeysgetvalue{/tikz/grow/.@cmd}{\orig@grow} |
| 185 |
\tikzoption{grow}{\csname grow@#1\endcsname\orig@grow#1\pgfeov} |
| 186 |
\pgfkeysgetvalue{/tikz/grow'/.@cmd}{\orig@growprime} |
| 187 |
\tikzoption{grow'}{\csname growprime@#1\endcsname\orig@growprime#1\pgfeov} |
| 188 |
\def\grow@up{\def\leveldirection{up}\def\siblingdirection{left}} |
| 189 |
\def\grow@down{\def\leveldirection{down}\def\siblingdirection{right}} |
| 190 |
\def\growprime@up{\def\leveldirection{up}\def\siblingdirection{right}} |
| 191 |
\def\growprime@down{\def\leveldirection{down}\def\siblingdirection{left}} |
| 192 |
\def\grow@left{\def\leveldirection{left}\def\siblingdirection{down}} |
| 193 |
\def\grow@right{\def\leveldirection{right}\def\siblingdirection{up}} |
| 194 |
\def\growprime@left{\def\leveldirection{left}\def\siblingdirection{up}} |
| 195 |
\def\growprime@right{\def\leveldirection{right}\def\siblingdirection{down}} |
| 196 |
|
| 197 |
% defaults appropriate for linguistic trees |
| 198 |
\def\tikz@edge@to@parent@path{\tree@edge{\tikzparentnode}{\tikzchildnode}} |
| 199 |
\tikzset{every tree node/.style={anchor=base}} |
| 200 |
\tikzset{every leaf node/.style={}} |
| 201 |
\tikzset{every internal node/.style={}} |
| 202 |
|
| 203 |
% predefined roof style |
| 204 |
\tikzset{roof/.style={edge from parent path=\roof@edge{\tikzparentnode}{\tikzchildnode}}} |