summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/xytree/xytree.sty
blob: 29adb6e030213c2e310ea4e4af1626aceb814a1b (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
\NeedsTeXFormat{LaTeX2e}
%%%
%%% xytree.sty
%%%
%%% Ver 1.0 xytree.sty written by Koaunghi Un <2004/08/10>
%%% Ver 1.1 modified by Koaunghi Un <2004/11/05>
%%%         \barnodeconnect with label
%%% Ver 1.2 added two new macros by Koaunghi Un <2004/12/20>
%%%         \yytree: a new tree macro
%%%         \xynodefont: font changing macro for all nodes in \xytree
%%%         and \yytree.
%%% Ver 1.3 added three new macros by Koaunghi Un <2006/02/27>
%%%         for drawing diagrams for Discourse Representation Structure
%%%         \drsbox: with referent and predicate
%%%         \drsrectbox: only for predicate
%%%         \drsdiabox: diamond-shaped box for DRS conditions.
%%% Ver 1.4 Bug fixed by Koaunghi Un <2006/03/07>
%%%         \drop'ing a frame after \xytrinode shifted the reference
%%%         point to the lower right corner of the triangle.
%%% Ver 1.5 correct font change of \xyterminal by Koaunghi Un <2006/08/15>
%%% 	    italic font shape of \xyterminal after \\
%%%

\ProvidesPackage{xytree}[2006/8/15 Pacakge for drawing trees]
\typeout{`xytree.sty' package v1.5 for drawing trees.}

\RequirePackage[all]{xy}

%%
%% \xytree[column spacing in pc]{matrix}
%% -------
%% A tree is a simple macro of the form "\xytree{...}"
%% It is an xymatrix, with "&" as column separator and "\\" as row
%% separator:
%%
%% \rowheight: sets row spacing.
%%

\newdimen\rowheight\newdimen\XyColumnSpacing \rowheight2pc
\makeatother
\newcommand{\xytree}[2][0.5]{\XyColumnSpacing=#1pc\relax%
  \ifdim\XyColumnSpacing>0pt\relax\else\XyColumnSpacing=0.01.pc\fi%
  \rule{0pt}{0pt}%
  \xymatrix @R=\rowheight @!C=\XyColumnSpacing @M=0pc {#2}}
\makeatletter

%%
%% \xyconnect[line shape]
%%      [line bend]
%%      (source corner, target corner)
%%      {target row, target col}
%%      "label"
%%

\def\xyconnect{\@ifnextchar[{\n@deconnect}{\n@deconnect[-]}}%]
\def\n@deconnect[#1]{\@ifnextchar[{\n@dec@nnect[#1]}%]
  {\n@dec@nnect[#1][\empty]}}
\def\n@dec@nnect[#1][#2]{\@ifnextchar({\n@d@c@nnect[#1][#2]}%)
  {\n@d@c@nnect[#1][#2](D,U)}}
\def\n@d@c@nnect[#1][#2](#3)#4{\@ifnextchar"{\connectwitharrow[#1][#2](#3)#4}%
  {\connectwitharrow[#1][#2](#3)#4""}}
\makeatother
\def\connectwitharrow[#1][#2](#3,#4)#5"#6"{%
  \ifx\empty#2\relax
  \ar @{#1} []+#3;[#5]+#4 #6
  \else
  \ar @{#1} @/#2/ []+#3;[#5]+#4 #6
  \fi}
\makeatletter

\def\labelstyle{\hbox}
\def\ind#1{{\fboxsep1pt\fbox{\small#1}}}

\def\@drawlines#1,#2\relax{%
  \xyconnect{1,#1}%
  \ifx#2\@empty\else\@drawlines#2\relax\fi%
}

%%
%% Tree Node
%%
%% \xynode[connecting nodes delimited by ,]{entry}
%% \xytrinode[+/-triangle width]{entry}
%% \xyterminal{entry}
%%
%% Following code
%%   \xynode{ & xynode[-1,1]{S} \\
%%           \xynode[0]{NP} & & \xytrinode{VP} \\
%%           \xyterminal{I} & & \xyterminal{ate an apple}
%%   }
%% will draw following tree diagram:
%%          S
%%         /\
%%        /  \
%%       /    \
%%      NP    VP
%%      |     /\
%%      |    /  \
%%      |    ____
%%      I ate an apple
%% with the terminal nodes written in italic shape.
%% Lines are drawn with the macro \xyconnect defined above.
%%

\def\xynode{\@ifnextchar[{\xyn@de}{\xyn@de[]}}%]
\def\xyn@de[#1]#2{{\txt{{\strut\xyn@def@nt #2}}}%
  \ifx#1\@empty\else\expandafter\@drawlines#1,\relax\fi}
\newcommand{\xytrinode}[2][0]{{\txt{{\strut#2}}}
  \POS[]+D;\POS[d]+UL\POS-(#1,0)**\dir{-}%
  \POS[]+D;\POS[d]+UR\POS+(#1,0)**\dir{-}%
  \POS[]\POS[d]+UL\POS-(#1,0);\POS[]\POS[d]+UR\POS+(#1,0)**\dir{-};\POS[]}
\newcommand{\xyterminal}[1]{\textit{\txt{\strut #1}}}

%%
%% \xytext
%% -------
%% For morph-syntactic agrement "\xytext{...}" is used in place of "\xytree"
%% It is \xytree with one column:
%%

\makeatother
\newcommand{\xytext}[1]{\raisebox{3pt}{\def\objectstyle{\hbox}%
    \xymatrix @C=1ex @M=0pt {#1}}\hspace{-2pt}}
\makeatletter

%%
%% \xybarconnect[line displacement]
%%      [line shape]
%%      (source corner, target corner)
%%      {target row, target col}
%%      "label"
%%

\def\xybarconnect{\@ifnextchar[%]
  {\b@rconnect}{\b@rconnect[3]}}
\def\b@rconnect[#1]{\@ifnextchar[%]
  {\b@rc@nnect[#1]}{\b@rc@nnect[#1][->]}}
\def\b@rc@nnect[#1][#2]{\@ifnextchar(%)
  {\b@rc@nn@ct[#1][#2]}{\b@rc@nn@ct[#1][#2](U,U)}}
\def\b@rc@nn@ct[#1][#2](#3,#4)#5{\@ifnextchar"%
  {\b@rc@nn@ct@[#1][#2](#3,#4)#5}%
  {\b@rc@nn@ct@[#1][#2](#3,#4)#5""}}
\def\b@rc@nn@ct@[#1][#2](#3,#4)#5"#6"{%
  \edef\reserved@a{%
    \csname c@rner@#3\endcsname#1\relax/%
    \csname c@rner@#4\endcsname#1\relax}%
  \expandafter\DrawHorizontal\reserved@a/#1/#2/#3/#4/#5/#6\end\ignorespaces}
\def\c@rner@U#1\relax{#1}
\let\c@rner@UL\c@rner@U
\let\c@rner@UR\c@rner@U
\let\c@rner@R\c@rner@U
\def\c@rner@D#1\relax{-#1}
\let\c@rner@DR\c@rner@D
\let\c@rner@DL\c@rner@D
\let\c@rner@L\c@rner@D
\makeatother
\def\DrawHorizontal#1/#2/#3/#4/#5/#6/#7/#8\end{%
  \ar @{#4} '[0,0]+#5 '[0,0]+#5+(0,#1) '[0,#7]+#6+(0,#2)#8 [0,#7]+#6
}
\makeatletter

%%
%% \xybarnode{entry}
%%
%% Following codes
%%   AGR \xytext{\xybarnode{Det}\xybarconnect{1} & \xybarnode{head-noun} & verb} ...
%% will draw following diagram:
%%            ______
%%           |      |
%%           |     \/
%%      AGR Det head-noun verb ...
%%

\newcommand{\xybarnode}[1]{\strut#1\ignorespaces}

%%
%% \yytree[indentation in pc]{matrix}
%% -------
%%
%% A \yytree is an another tree drawing macro of the form "\yytree{...}"
%% It is an xymatrix, with "&" as column separator and "\\" as row
%% separator:
%%

\makeatother
\newcommand{\yytree}[2][2]{%
  \rule{0pt}{0pt}%
  \xymatrix @!0 @C=#1pc @R=1.5pc @W=0pc @*[r] {#2}
}
\makeatletter

%%
%% \yynode[daughter nodes delimited by ,]{entry}
%% -------
%%
%% For example
%%   \yytree{ \yynode[1,4]{top} \\
%%     & \yyode[1,2]{A} \\
%%     & & \yynode{Aa} \\
%%     & & \yynode{Ab} \\
%%     & \yynode[1]{B} \\
%%     & & \yynode{Ba}
%%   }
%% will produce the following tree:
%%   top
%%    |-- A
%%    |   |-Aa
%%    |   |-Ab
%%    |-- B
%%        |-Ba
%% with proper bending line for the last daughter node.
%%

\def\yynode{\@ifnextchar[{\yyn@de}{\yyn@de[]}}%]
\def\yyn@de[#1]#2{{\txt{{\xyn@def@nt #2}}}%
  \@tempcnta=0
  \ifx#1\@empty\else\expandafter\@yylines#1,\relax\fi%
  \ifnum\@tempcnta>0
  \edef\reserved@a{\noexpand\POS[]+D+(1,0);[\the\@tempcnta,0]+(1,0)**\noexpand\dir{-}}%
  \reserved@a\fi
}

\def\@yylines#1,#2\relax{%
  \POS[#1,0]+(1,0);[#1,1]+L **\dir{-}%
  \@tempcnta=#1
  \ifx#2\@empty\else\@yylines#2\relax\fi%
}

%%
%% \xynodefont
%% -----------
%% Default font for node entries in \xytree and \yytree.
%% This is initially set to be empty, which is the same as \rmfamily
%% \xynodefont{\itshape} typeset all node entries in italic shape.
%%

\def\xynodefont#1{\def\xyn@def@nt{#1}}
\xynodefont{}

%%%
%%% Discourse Representation Structure
%%%
%% This is not related with linguistic trees, but because no other
%% packages provide a convenient way to draw DRS diagram without the
%% help of pstricks, these three macros for drawing DRS diagram are
%% added here.  They use also Xy-pic feature to construct boxes.
%%

\newdimen\drs@wd\newdimen\drs@ht\newdimen\drsboxsep
\drsboxsep6pt
\newbox\drs@boxa\newbox\drs@boxb

%%
%% \drsbox[line art]{referent}{predicate}
%% -------
%% [line art] : optional.  specifies the art of lines.  All \dir
%%   directionals of Xy-pic kernel library can be used (not \dir2,
%%   \dir3, \dir_, \dir^ directionals)
%% referent:
%% predicate:
%%

%% ------------
%% | x        |
%% ------------
%% |          |
%% | donkey(x)|
%% | green(x) |
%% |          |
%% ------------

\newcommand{\drsbox}[3][-]{%
  \begin{tabular}{l}\tabcolsep0pt
    \itshape
    \setbox\drs@boxa\hbox{\begin{tabular}{l}#2\end{tabular}}%
    \drs@wd=\wd\drs@boxa
    \drs@ht=\ht\drs@boxa\advance\drs@ht by\dp\drs@boxa
    \setbox\drs@boxb\hbox{\begin{tabular}{l}#3\end{tabular}}%
    \ifdim\drs@wd<\wd\drs@boxb\drs@wd\wd\drs@boxb\fi
    \advance\drs@ht by\ht\drs@boxb \advance\drs@ht by\dp\drs@boxb
    \divide\drs@ht by2
    \advance\drs@ht by1.5\drsboxsep
    \wd\drs@boxa=\drs@wd\wd\drs@boxb=\drs@wd
    \ifdim\ht\drs@boxa<\bigskipamount\ht\drs@boxa=\bigskipamount\fi
    \xy
    <0pt,0pt>*\hbox{\box\drs@boxb}="b";
    <0pt,\drs@ht>*\hbox{\box\drs@boxa}="a";
    "a"+LU+<-\drsboxsep,.5\drsboxsep>;
    "a"+RU+<\drsboxsep,.5\drsboxsep>**\dir{#1};
    "b"+RD+<\drsboxsep,-\drsboxsep>**\dir{#1};
    "b"+LD+<-\drsboxsep,-\drsboxsep>**\dir{#1};
    "a"+LU+<-\drsboxsep,.5\drsboxsep>**\dir{#1};
    "b"+LU+<-\drsboxsep,\drsboxsep>;"b"+RU+<\drsboxsep,\drsboxsep>**\dir{#1};
    \endxy
  \end{tabular}}

%%
%% \drsrectbox[line art]{content}
%% -----------
%%

%% -------------
%% |           |
%% | man(u)    |
%% | unicorn(u)|
%% | seeks(u,v)|
%% |           |
%% -------------

\newcommand{\drsrectbox}[2][-]{%
  \begin{tabular}{l}\tabcolsep0pt
    \itshape
    \xy
    *\hbox{\begin{tabular}{l}#2\end{tabular}}="a";
    "a"+LD+<-\drsboxsep,-\drsboxsep>;
    "a"+LU+<-\drsboxsep,\drsboxsep>**\dir{#1};
    "a"+RU+<\drsboxsep,\drsboxsep>**\dir{#1};
    "a"+RD+<\drsboxsep,-\drsboxsep>**\dir{#1};
    "a"+LD+<-\drsboxsep,-\drsboxsep>**\dir{#1};
    \endxy
  \end{tabular}}

%%
%% \drsdiabox[line art]{conditions}
%% ----------
%%

%%      /\
%%     /  \
%%    /    \
%%   /      \
%%  /        \
%% /  every   \
%% \    x     /
%%  \        /
%%   \      /
%%    \    /
%%     \  /
%%      \/

\newcommand{\drsdiabox}[2][-]{%
  \begin{tabular}{l}\tabcolsep0pt
    \itshape
    \setbox\drs@boxa\hbox{\begin{tabular}{c}#2\end{tabular}}%
    \@tempdima=\wd\drs@boxa
    \@tempdima=.5\wd\drs@boxa
    \advance\@tempdima by.5\ht\drs@boxa
    \advance\@tempdima by.5\drsboxsep
    \xy
    *\hbox{\box\drs@boxa}="a";
    "a"+<-\@tempdima,0pt>;
    "a"+<0pt,\@tempdima>**\dir{#1};
    "a"+<\@tempdima,0pt>**\dir{#1};
    "a"+<0pt,-\@tempdima>**\dir{#1};
    "a"+<-\@tempdima,0pt>**\dir{#1};
    \endxy
  \end{tabular}}

%%%
%%% Autosegmental Representation
%%% ----------------------------
%%%


\def\asrnode#1{\txt{\strut#1}}

\endinput