summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/tikz-qtree/tikz-qtree.tex
blob: 6114da8468b5173e67d8e40ebafe3b572992ab02 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
%    tikztree.tex
%    Version 1, 22 Dec 2009

%    Copyright (C) 2002, 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.

%% These macros facilitate building up an object recursively before
%% putting it into the input stream.

\newtoks\@result
\def\@call#1#2{\let\@cont=#2\bgroup\@result={}#1}
\def\@return{\global\@result=\@result\egroup\@cont}

\def\@ifequal#1#2{\edef\testa{#1}\edef\testb{#2}\ifx\testa\testb}

%% scan a tree: this just scans a subtree and then puts it onto the
%% input stream

\def\Tree{\@call\@subtree\@Tree}
\def\@Tree{%
%\showthe\@result %debug
\ifpgfpicture\the\@result\else\tikzpicture\the\@result\endtikzpicture\fi % is there a test for tikzpicture?
}

%% scan a subtree
\newtoks\child@list
\newtoks\root@node

\def\@subtree[{%
\root@node={}%
\pgfutil@ifnextchar.{\@call\@interior\@@subtree}{\@@@subtree}}
\def\@@subtree{%
\root@node=\@result
\@@@subtree
}
\def\@@@subtree{%
\@call\@children\@@@@subtree
}
\def\@@@@subtree]{%
\child@list=\@result
\pgfutil@ifnextchar.{\@call\@interior\@@@@@subtree}{\@@@@@@subtree}}
\def\@@@@@subtree{%
%%% Check for mismatch.
\@ifequal{\the\root@node}{\pgfutil@empty}%
  \root@node=\@result
\fi
\@ifequal{\the\root@node}{\the\@result}\else
  \message{Warning: mismatched labels, \the\root@node{} and \the\@result.}%
\fi
\@@@@@@subtree
}
\def\@@@@@@subtree{%
\@ifequal{\the\root@node}{\pgfutil@empty}
\edef\act{\noexpand\@result={\noexpand\pgftree{\noexpand\path coordinate (\noexpand\nodename);}{\the\child@list}}}%
\else
\edef\act{\noexpand\@result={\noexpand\pgftree{\the\root@node}{\the\child@list}}}%
\fi
\act
\@return}

%% scan a sequence of subtrees or leaves

\newif\ifscanned@edge

\def\@children{%
\scanned@edgefalse
\child@list{}%
\@@children}
\def\@@children{%
\pgfutil@ifnextchar]{\@result\child@list\@return}{% end of children
\pgfutil@ifnextchar\edge{% explicit edge
\ifscanned@edge
\message{Warning: more than one edge given for a single child}\let\next\@@children % ignore
\else
\scanned@edgetrue\let\next\@@@children
\fi
\@call\@edge\next}{%
% else, a real node is next
\ifscanned@edge\else % no explicit edge, supply default
\expandafter\child@list\expandafter{\the\child@list{\edge@adapter{}}}%
\fi
\scanned@edgefalse
\pgfutil@ifnextchar[{\@call\@subtree\@@@children}% subtree
{\@call\@leaf\@@@children} % leaf
}}}
\def\@@@children{%
% wrap child inside curly braces
\expandafter\@result\expandafter{\expandafter{\the\@result}}
\edef\act{\noexpand\child@list{\the\child@list \the\@result}}\act
\@@children
}

\def\@interior.{\@result{\node[alias=\nodename][every tree node,every internal node]}\@label}

\def\@leaf{\@result{\node[alias=\nodename][every tree node,every leaf node]}\@label}

\def\@edge\edge #1;{%
\@result{\edge@adapter{#1}}%
\@return}
\def\edge@adapter#1{%
\let\tikzparentnode\parentnodename
\let\tikzchildnode\nodename
\path edge from parent #1;%
}

% a label is either text or PGF/TikZ code starting with \node
\def\@label{\pgfutil@ifnextchar\node{\@litlabel}{\@@label}}
\def\@@label#1 {%
\expandafter\@result\expandafter{\the\@result{#1};}%
\@return}

% try to copy \node command into \@result without stripping braces
\def\@litlabel\node{\@@litlabel}
\def\@@litlabel{\pgfutil@ifnextchar\bgroup{\@@@litlabel}{\@@@@litlabel}}
\def\@@@litlabel#1{\expandafter\@result\expandafter{\the\@result {#1}}\@@litlabel}
\def\@@@@litlabel#1;{\expandafter\@result\expandafter{\the\@result #1;}\@return}

% predefined edges

\def\tree@edge#1#2{%
\ifupsidedown
(#1.north) -- (#2.south)
\else
(#1.south) -- (#2.north)
\fi
}

\def\roof@edge#1#2{
\ifupsidedown
(#1.north) -- (#2.south west) -- (#2.south east) -- cycle
\else
(#1.south) -- (#2.north west) -- (#2.north east) -- cycle
\fi
}

%%% Options

\tikzoption{level distance}{\pgfmathsetlength\levelsep{#1}}
\tikzoption{sibling distance}{\pgfmathsetlength\subtreesep{#1}} % different semantics

% I don't really like this scheme
\tikzoption{grow}{\csname grow@#1\endcsname}
\tikzoption{grow'}{\csname growprime@#1\endcsname}
\def\grow@up{\upsidedowntrue\righttolefttrue}
\def\grow@down{\upsidedownfalse\righttoleftfalse}
\def\growprime@up{\upsidedowntrue\righttoleftfalse}
\def\growprime@down{\upsidedownfalse\righttolefttrue}

% defaults appropriate for linguistic trees
\tikzset{every tree node/.style={anchor=base}}
\tikzset{every leaf node/.style={}}
\tikzset{every internal node/.style={}}
\def\tikz@edge@to@parent@path{\tree@edge{\tikzparentnode}{\tikzchildnode}}

% predefined roof style
\tikzset{roof/.style={edge from parent path=\roof@edge{\tikzparentnode}{\tikzchildnode}}}