summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/tikz-qtree/pgftree.tex
blob: 4cdaad164333ad4c5cb15c0b5a81a7fdd295437d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
%    pgftree.tex
%    Version 1, 22 Dec 2009

%    Copyright 2009 by David Chiang

%    This program is free software; you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation; either version 2 of the License, or
%    (at your option) any later version.

%    This program is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.

%    You should have received a copy of the GNU General Public License along
%    with this program; if not, write to the Free Software Foundation, Inc.,
%    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

% to do:
% if \nodename does not exist as desired, wrap inside a rectangle node
% don't use pgfsubpic internals, use anchors instead

\newdimen\levelsep \levelsep=30pt
\newdimen\subtreesep \subtreesep=2pt
\newif\ifrighttoleft
\newif\ifupsidedown
\def\drawnode#1{\pgfnode{rectangle}{base}{#1}{\nodename}{\pgfusepath{discard}}}
\def\drawedge{%
\pgfpathmoveto{\pgfpointanchor{\parentnodename}{\ifupsidedown north\else south\fi}}%
\pgfpathlineto{\pgfpointanchor{\nodename}{\ifupsidedown south\else north\fi}}%
\pgfusepath{stroke}}

% local variables that we need to assign to inside a \pgfforeach
\newdimen\pgftree@childx
\newdimen\pgftree@savechildx
\newcount\pgftree@childi
\newcount\pgftree@savechildi

\def\nodename{r}

%%% \pgftree{root}{subtrees}

% The first argument draws the root node using PGF/TikZ commands.
% The node must be named \nodename.

% The second argument is an even-length sequence of tokens.
% Token 2n-1 in the sequence draws the nth edge. It should draw an edge from \parentnodename to \nodename.
% Token 2n in the sequence draws the nth subtree. Its root must be named \nodename.

\def\pgftree#1#2{%
\pgfscope
% create root node
#1
\let\parentnodename\nodename
\ifupsidedown
\pgftransformshift{\pgfpoint{0}{\levelsep}}%
\else
\pgftransformshift{\pgfpoint{0}{-\levelsep}}%
\fi
\pgftree@savechildx=\pgftree@childx
\pgftree@savechildi=\pgftree@childi
\pgfsubpicture
\pgftree@childx=0pt%
\pgftree@childi=0%
{\process@children #2}%
\endpgfsubpicture
\ifnum\pgftree@childi>0%
% Center children so that parent is midway between origins of first and last children
\pgftransformshift{\pgfpointscale{-0.5}{\pgfpoint{\the\pgftree@childx}{0}}}%
\pgfplacesubpicture
% Draw the edges
\pgftree@childi=0%
{\process@edges #2}%
\fi
\global\pgftree@childi=\pgftree@savechildi
\global\pgftree@childx=\pgftree@savechildx
\endpgfscope
}

\def\process@children{%
\pgfutil@ifnextchar\egroup
{% Step back to origin of last child
\advance\pgftree@childx by \ifrighttoleft-\pgf@subpicminx\else-\pgf@subpicmaxx\fi
\global\pgftree@childx=\pgftree@childx
\global\pgftree@childi=\pgftree@childi
}%
{\@process@children}%
}
\def\@process@children#1#2{%
\edef\nodename{\parentnodename-\the\pgftree@childi}%
\def\child{#2}%
\pgfsubpicture
#2%
\endpgfsubpicture
\ifnum\pgftree@childi>0%
\advance\pgftree@childx by \ifrighttoleft-\pgf@subpicmaxx\else-\pgf@subpicminx\fi
\advance\pgftree@childx by \ifrighttoleft-\subtreesep\else\subtreesep\fi
\fi
\pgfscope
\pgftransformshift{\pgfpoint{\the\pgftree@childx}{0}}%
\pgfplacesubpicture
\endpgfscope
\advance\pgftree@childx by \ifrighttoleft\pgf@subpicminx\else\pgf@subpicmaxx\fi
\advance\pgftree@childi by 1%
\process@children
}

\def\process@edges{%
\pgfutil@ifnextchar\egroup
{}%
{\@process@edges}%
}
\def\@process@edges#1#2{%
\edef\nodename{\parentnodename-\the\pgftree@childi}%
#1%
\advance\pgftree@childi by 1%
\process@edges
}