| 1 |
% pgftree.tex |
| 2 |
% Version 1.2, 20 Apr 2012 |
| 3 |
|
| 4 |
% Copyright 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.1: |
| 24 |
% - major restructuring to not do arbitrary nesting of subpicture environments |
| 25 |
% - sideways trees |
| 26 |
|
| 27 |
% To do: |
| 28 |
% - trees with all leaves at same level |
| 29 |
% - if \nodename does not exist as desired, wrap inside a rectangle node |
| 30 |
% - don't use pgfsubpic internals |
| 31 |
|
| 32 |
\newdimen\levelsep \levelsep=30pt |
| 33 |
\newdimen\subtreesep \subtreesep=2pt |
| 34 |
\newdimen\smuggle@levelsep |
| 35 |
\newdimen\smuggle@subtreesep |
| 36 |
\newdimen\this@levelsep |
| 37 |
\newdimen\this@subtreesep |
| 38 |
|
| 39 |
\def\leveldirection{down} |
| 40 |
\def\siblingdirection{right} |
| 41 |
|
| 42 |
% definitions of growing directions |
| 43 |
\def\pgftree@levelshift{\csname pgftree@levelshift@\leveldirection\endcsname} |
| 44 |
\def\pgftree@parentanchor{\csname pgftree@parentanchor@\leveldirection\endcsname} |
| 45 |
\def\pgftree@childanchor{\csname pgftree@childanchor@\leveldirection\endcsname} |
| 46 |
% these assume that the current subpicture is the child |
| 47 |
\def\pgftree@presiblingshift{\csname pgftree@presiblingshift@\siblingdirection\endcsname} |
| 48 |
\def\pgftree@postsiblingshift{\csname pgftree@postsiblingshift@\siblingdirection\endcsname} |
| 49 |
|
| 50 |
\def\pgftree@levelshift@down{\pgfpoint{0}{-\levelsep}} |
| 51 |
\def\pgftree@parentanchor@down{south} |
| 52 |
\def\pgftree@childanchor@down{north} |
| 53 |
|
| 54 |
\def\pgftree@levelshift@up{\pgfpoint{0}{\levelsep}} |
| 55 |
\def\pgftree@parentanchor@up{north} |
| 56 |
\def\pgftree@childanchor@up{south} |
| 57 |
|
| 58 |
\def\pgftree@levelshift@right{\pgfpoint{\levelsep}{0}} |
| 59 |
\def\pgftree@parentanchor@right{east} |
| 60 |
\def\pgftree@childanchor@right{west} |
| 61 |
|
| 62 |
\def\pgftree@levelshift@left{\pgfpoint{-\levelsep}{0}} |
| 63 |
\def\pgftree@parentanchor@left{west} |
| 64 |
\def\pgftree@childanchor@left{east} |
| 65 |
|
| 66 |
\def\pgftree@presiblingshift@right{\pgf@process{\pgf@x-\pgf@subpicminx \advance\pgf@x\subtreesep \pgf@y 0pt}} |
| 67 |
\def\pgftree@postsiblingshift@right{\pgf@process{\pgf@x\pgf@subpicmaxx \pgf@y 0pt}} |
| 68 |
|
| 69 |
\def\pgftree@presiblingshift@left{\pgf@process{\pgf@x-\pgf@subpicmaxx \advance\pgf@x-\subtreesep \pgf@y 0pt}} |
| 70 |
\def\pgftree@postsiblingshift@left{\pgf@process{\pgf@x\pgf@subpicminx \pgf@y 0pt}} |
| 71 |
|
| 72 |
\def\pgftree@presiblingshift@up{\pgf@process{\pgf@x 0pt \pgf@y-\pgf@subpicminy \advance\pgf@y\subtreesep}} |
| 73 |
\def\pgftree@postsiblingshift@up{\pgf@process{\pgf@x 0pt \pgf@y\pgf@subpicmaxy}} |
| 74 |
|
| 75 |
\def\pgftree@presiblingshift@down{\pgf@process{\pgf@x 0pt \pgf@y-\pgf@subpicmaxy \advance\pgf@y-\subtreesep}} |
| 76 |
\def\pgftree@postsiblingshift@down{\pgf@process{\pgf@x 0pt \pgf@y\pgf@subpicminy}} |
| 77 |
|
| 78 |
% for convenience if you are using \pgftree directly |
| 79 |
\def\drawnode#1{\pgfnode{rectangle}{base}{#1}{\nodename}{\pgfusepath{discard}}} |
| 80 |
\def\drawedge{% |
| 81 |
\pgfpathmoveto{\pgfpointanchor{\parentnodename}{\pgftree@parentanchor}}% |
| 82 |
\pgfpathlineto{\pgfpointanchor{\nodename}{\pgftree@childanchor}}% |
| 83 |
\pgfusepath{stroke}} |
| 84 |
|
| 85 |
% local variables that we need to assign to inside a \pgfforeach |
| 86 |
\newdimen\pgftree@childx |
| 87 |
\newdimen\pgftree@savechildx |
| 88 |
\newdimen\pgftree@childy |
| 89 |
\newdimen\pgftree@savechildy |
| 90 |
\newcount\pgftree@childi |
| 91 |
\newcount\pgftree@savechildi |
| 92 |
\newcount\pgftree@level |
| 93 |
\newdimen\pgftree@depth |
| 94 |
|
| 95 |
%%% \pgftree{subtree} |
| 96 |
|
| 97 |
\def\pgftree#1{% |
| 98 |
\def\nodename{r}% |
| 99 |
\pgftree@level=0\relax |
| 100 |
\pgftree@depth=0pt\relax |
| 101 |
#1% |
| 102 |
\pgfplacesubpicture |
| 103 |
} |
| 104 |
|
| 105 |
%%% \pgfsubtree{root}{subtrees} |
| 106 |
% The first argument draws the root node using PGF/TikZ commands. |
| 107 |
% The node must be named \nodename. |
| 108 |
|
| 109 |
% The second argument is a sequence of 3n tokens. |
| 110 |
% Token 3n-2 draws the nth edge. It should draw an edge from \parentnodename to \nodename. |
| 111 |
% Token 3n-1 is a hook that can change \levelsep or \subtreesep for child n only. |
| 112 |
% Token 3n draws the nth subtree. Its root must be named \nodename. |
| 113 |
|
| 114 |
\pgfnewsubpicture{children} |
| 115 |
\newdimen\pgftree@lastchildx |
| 116 |
\newdimen\pgftree@lastchildy |
| 117 |
|
| 118 |
\def\pgfsubtree#1#2{% |
| 119 |
\let\parentnodename\nodename |
| 120 |
\pgftree@savechildx=\pgftree@childx |
| 121 |
\pgftree@savechildy=\pgftree@childy |
| 122 |
\pgftree@savechildi=\pgftree@childi |
| 123 |
% Build subpicture with all the children and their subtrees |
| 124 |
{\pgftree@childx=0pt% |
| 125 |
\pgftree@childy=0pt% |
| 126 |
\pgftree@childi=0% |
| 127 |
\process@children #2}% |
| 128 |
\begin{pgfsubpicture}% |
| 129 |
% Create node |
| 130 |
#1% |
| 131 |
\ifnum\pgftree@childi>0% |
| 132 |
% Place children |
| 133 |
{% center so that parent is midway between origins of first and last children |
| 134 |
\pgftransformshift{\pgfpointscale{-0.5}{\pgfqpoint{\the\pgftree@childx}{\the\pgftree@childy}}}% |
| 135 |
\pgfplacesubpicture}% |
| 136 |
% Draw the edges |
| 137 |
{\pgftree@childi=0% |
| 138 |
\process@edges #2}% |
| 139 |
\fi |
| 140 |
\end{pgfsubpicture}% |
| 141 |
\global\pgftree@childi=\pgftree@savechildi |
| 142 |
\global\pgftree@childx=\pgftree@savechildx |
| 143 |
\global\pgftree@childy=\pgftree@savechildy |
| 144 |
} |
| 145 |
|
| 146 |
\def\process@children{% |
| 147 |
\pgfutil@ifnextchar\egroup |
| 148 |
{% No more children, step back to origin of last child |
| 149 |
\global\pgftree@childx\pgftree@lastchildx |
| 150 |
\global\pgftree@childy\pgftree@lastchildy |
| 151 |
\global\pgftree@childi\pgftree@childi |
| 152 |
\ifnum\pgftree@childi>0% |
| 153 |
\pgfrestoresubpicture{children}% pass children back to caller |
| 154 |
\fi |
| 155 |
}% |
| 156 |
{\@process@children}% |
| 157 |
} |
| 158 |
\def\@process@children#1#2#3{% |
| 159 |
% #1 is the edge, #2 is the hook, #3 is the child |
| 160 |
{% call hook to get \levelsep and \subtreesep |
| 161 |
% and put them in \this@levelsep and \this@subtreesep |
| 162 |
% to hide them from descendants |
| 163 |
#2% |
| 164 |
\global\smuggle@levelsep\levelsep |
| 165 |
\global\smuggle@subtreesep\subtreesep}% |
| 166 |
\this@levelsep\smuggle@levelsep |
| 167 |
\this@subtreesep\smuggle@subtreesep |
| 168 |
% Build the current child |
| 169 |
{\edef\nodename{\parentnodename-\the\pgftree@childi}% |
| 170 |
\advance\pgftree@level by 1\relax |
| 171 |
\advance\pgftree@depth by \this@levelsep |
| 172 |
#3}% |
| 173 |
\begin{pgfsubpicture}% |
| 174 |
% Place current child |
| 175 |
{% |
| 176 |
% The \levelsep and \subtreesep set in #2 apply only here |
| 177 |
\levelsep\this@levelsep |
| 178 |
\subtreesep\this@subtreesep |
| 179 |
\ifnum\pgftree@childi>0% the first child is always at 0 |
| 180 |
\pgftree@presiblingshift \global\advance\pgftree@childx\pgf@x \global\advance\pgftree@childy\pgf@y |
| 181 |
\fi |
| 182 |
\pgftransformshift{\pgftree@levelshift}% |
| 183 |
\pgftransformshift{\pgfqpoint{\the\pgftree@childx}{\the\pgftree@childy}}% |
| 184 |
\pgfplacesubpicture |
| 185 |
\global\pgftree@lastchildx\pgftree@childx |
| 186 |
\global\pgftree@lastchildy\pgftree@childy |
| 187 |
\pgftree@postsiblingshift \global\advance\pgftree@childx\pgf@x \global\advance\pgftree@childy\pgf@y |
| 188 |
}% |
| 189 |
% Save the augmented row of children back into "children" |
| 190 |
\end{pgfsubpicture} |
| 191 |
\ifnum\pgftree@childi>0% |
| 192 |
\pgfmergesubpicture{children} |
| 193 |
\else |
| 194 |
\pgfsavesubpicture{children}% |
| 195 |
\fi |
| 196 |
\advance\pgftree@childi by 1% |
| 197 |
\process@children |
| 198 |
} |
| 199 |
|
| 200 |
\def\process@edges{% |
| 201 |
\pgfutil@ifnextchar\egroup |
| 202 |
{}% |
| 203 |
{\@process@edges}% |
| 204 |
} |
| 205 |
\def\@process@edges#1#2#3{% |
| 206 |
\edef\nodename{\parentnodename-\the\pgftree@childi}% |
| 207 |
#1% |
| 208 |
\advance\pgftree@childi by 1% |
| 209 |
\process@edges |
| 210 |
} |
| 211 |
|
| 212 |
\def\subtreeof#1{% |
| 213 |
% the subpicture which contains a node also contains exactly its subtree |
| 214 |
\csname pgf@sh@pi@#1\endcsname |
| 215 |
} |