summaryrefslogtreecommitdiff
path: root/macros/xetex/latex/langsci/langsci-forest-setup.sty
blob: 07af8fc9109a0635af8cab1e1f68f46738b06271 (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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%      File: langsci-forest-setup.sty
%%    Author: Language Science Press (http://langsci-press.org)
%%      Date: 2016-01-16 16:47:43 UTC
%%   Purpose: This file contains optional styles and settings for the
%%            forest package. This version is for 2.0.
%%  Language: LaTeX
%%   Licence:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% texlive 2015 is broken (in comparison to texlive 2013)
% a workaround is to use a different pgf driver.
% See email from Akira Kakuto to Stefan Müller 15.01.2016

% Do we need this for 2017?
% We seem to be better off without it 31.10.2017
%deceive the driver
%\def\pdftexversion{140}
%use the old driver for dvipdfm
%\def\pgfsysdriver{pgfsys-dvipdfm.def}

% http://en.wikibooks.org/wiki/LaTeX/Linguistics#Syntactic_trees
\usepackage{tikz-dependency}

% dangerous, use at own risk
%\usetikzlibrary{tikzmark} % saves positions in tikz pictures, allows to connect arbitrary text


% http://tex.stackexchange.com/questions/256280/drawing-complex-dependency-diagrams-with-tikz-forest
\usetikzlibrary{arrows.meta}

\tikzset{deparrow/.style={-Latex}}%,blue}}


% externalization works with package memoize
\usepackage[linguistics]{forest}
%\usepackage{morewrites}



% specification for all trees, "default preamble" appends to existing specification.
% The version with apostrophe replaces it.
\forestset{default preamble'={
    for tree={align=center,parent anchor=south, child anchor=north,anchor=north,base=bottom},   
% anchor=north was introduced to get alignments of AVMs right.
% used to be anchor=north but this fails on trees in \ea \z examples, St. Mü. 02.05.2020 or may be not
%
% This would align trees to the baseline. We do not want this for TAG
% where several trees have to be aligned with respect to their center.
%    before drawing tree={
%      sort by=y,
%      for min={tree}{baseline}
%    }
  }}


\forestset{
  declare dimen={child anchor yshift}{0pt},
  adjust childrens child anchors/.style={
    if n children>=2{
      before packing={
        tempdima/.max={max_y}{children},
        for children={
          child anchor yshift=tempdima()-max_y()
        },
      }
    }{}
  },
  default preamble={
    for tree={
      edge path'={(!u.parent anchor)--([yshift=\forestoption{child anchor yshift}].child anchor)},
      adjust childrens child anchors
    }
  },
}



% http://www.texample.net/tikz/examples/borrowers-and-lenders/
\usetikzlibrary{arrows} %,positioning}
\tikzset{
    %Define standard arrow tip
    >=stealth'}



%% \forestset{
%% sn edges/.style={for tree={parent anchor=south, child anchor=north,align=center,base=bottom,where n children=0{tier=word}{}}},
%% background tree/.style={for tree={text opacity=0.2,draw opacity=0.2,edge={draw opacity=0.2}}}
%% }



% The following code fixes the size of triangles for examples with translations. The specification
% ",delay=with translation" at the leaves is important to make this work.
% http://tex.stackexchange.com/questions/167978/smaller-roofs-for-forest/205311#205311
\forestset{
  with translation/.style={
    l sep=0,inner xsep=0,
    append translation/.expanded/.wrap pgfmath arg={\gettranslation{##1}}{content},
    content/.expanded/.wrap pgfmath arg={\gettext{##1}}{content},
  },
  append translation/.style={
    if={strequal("#1","")}{}{
      append={[#1,no edge,inner xsep=0,inner ysep=0,outer ysep=0,
               tier=gloss, % does not work for TAG trees since the words are not aligned at the baseline
               l=\baselineskip + 0.2ex,
        ]}
    }
  }
}

\forestset{
  with unaligned translation/.style={
    l sep=0,inner xsep=0,
    append unaligned translation/.expanded/.wrap pgfmath arg={\gettranslation{##1}}{content},
    content/.expanded/.wrap pgfmath arg={\gettext{##1}}{content},
  },
  append unaligned translation/.style={
    if={strequal("#1","")}{}{
      append={[#1,no edge,inner xsep=0,inner ysep=0,outer ysep=0,
               %tier=gloss, % does not work for TAG trees since the words are not aligned at the baseline
               l=\baselineskip + 0.2ex,
        ]}
    }
  }
}


\def\gettext#1{\gettextA#1;;\endget}
\def\gettextA#1;#2;{\removesep#1;}
\def\gettranslation#1{\gettranslationA#1;;\endget}
\def\gettranslationA#1;#2;{\removesep#2;}
\def\removesep#1;#2\endget{#1}

% copy of the above for TAG
\forestset{
  with translation TAG/.style={
    l sep=0,inner xsep=0,
    append translation TAG/.expanded/.wrap pgfmath arg={\gettranslation{##1}}{content},
    content/.expanded/.wrap pgfmath arg={\gettext{##1}}{content},
  },
  append translation TAG/.style={
    if={strequal("#1","")}{}{
      append={[#1,no edge,inner xsep=0,inner ysep=0,outer ysep=0,
               %tier=gloss, % does not work for TAG trees since the words are not aligned at the baseline
               l=\baselineskip + 0.2ex,
        ]}
    }
  }
}


% a specific style that specifies the word tier: all nodes that do not have any children
% to do: make this style default and change all other figures explicitely as is described here:
% http://tex.stackexchange.com/questions/167983/getting-rid-of-a-default-where-specification
\forestset{
sm edges/.style={for tree={parent anchor=south, child anchor=north,base=top}, % changed base=bottom
                                % to top since we have NP and AVM nodes and the NP has to start
                                % right away at the same hight as the AVM St. Mü. 18.10.2017
                 where n children=0{tier=word,delay=with translation}{} %,delay=with translation
                 },
sm edges without translation/.style={for tree={parent anchor=south, child anchor=north,base=bottom},
                 where n children=0{tier=word}{}
                 },
word tier/.style={where n children=0{tier=word}{}
                 },
%
% dependency grammar trees, projective trees only, I guess. St. Mü. 29.09.2014
dg edges/.style={for tree={parent anchor=south, child anchor=north,align=center,base=bottom},
                 where n children=0{tier=word,edge=dotted,calign with current edge,delay=with translation}{}
                },
%
% Adjunct edges as suggested by Engels, 1977
% http://tex.stackexchange.com/questions/204770/defining-a-special-edge-style-for-connecting-nodes-in-forest/204819#204819
dg adjunct/.style={edge path={\noexpand\path[\forestoption{edge}]
(!u.parent anchor)--(.child anchor)-- +(0,#1)\forestoption{edge label};}},
%
dg adjunct/.default=6pt,
%
% for Tesniere-style translations
dg transfer/.style={edge path={\noexpand\path[\forestoption{edge}, rounded corners=3pt]
    % the line downwards
    (!u.parent anchor)-- +($(0,-l)-(0,4pt)$)-- +($(12pt,-l)-(0,4pt)$)
    % the horizontal line
    ($(!p.north west)+(0,l)-(0,20pt)$)--($(.north east)+(0,l)-(0,20pt)$)\forestoption{edge label};},!p.edge'={}},
% for Tesniere-style junctions
dg junction/.style={no edge, tikz+={\draw (!p.east)--(!.west) (.east)--(!n.west);}    },
%
%
%
% http://tex.stackexchange.com/questions/256280/drawing-complex-dependency-diagrams-with-tikz-forest/256300#256300
% in the tree draw the highest nodes of the inheritance network. For lower nodes insert empty nodes
% "[]". These will be filled by the same word and the numbers will be provided with bars according
% to remoteness from the word level.
%
% So cool! Thanks Sašo Živanović
%
% modified to cope with multiple occurances of the same word (the, the1, the2)
%
  wg/.style={
    for tree={
      no edge,
      draw,
      outer ysep=1pt,
    },
    delay={
      copy label,
      for tree={nameN/.option=content},
    },
    for tree={content format={\strut\forestoption{content}}},
    where n children={0}{
      tier=word,
    }{},
  },
  copy label/.style={
    for children={
      if content={}{
        % content/.pgfmath={content("!u")},
        content/.option=!u.content, % faster
        calign with current,
        edge={draw,{-Triangle[open,reversed]}},
        copy label,
        % !u.content/.pgfmath={content},
        !u.content/.option=content, % faster
        !u.content+=',
      }{
        copy label,
      }
    }
  },
  % 1. This is not effective if there's many clashes.
  % 2. Register tempcounta must be free to use.
  nameN/.style={
    if nodewalk valid={name=#1}{
      tempcounta=2,
      @nameN=#1,
    }{
      name=#1,
    },
  },
  @nameN/.style={
    while nodewalk valid={name/.process=Rw1{tempcounta}{#1##1}}{
      tempcounta+=1,
    },
    name/.process=Rw1{tempcounta}{#1##1},
  },
%
%
% TAG
% http://tex.stackexchange.com/questions/207459/getting-old-school-trees-with-forest
% fit -> fit=band causes all nodes to have nothing below them
tag/.style={for tree={parent anchor=south, child anchor=north,align=center,base=top,fit=rectangle},
  where n children=0{delay=with translation TAG}{},
% the following breaks the alignments in sets
  begin draw/.code={\begin{tikzpicture}[baseline=(current bounding box.center)]}
},
%
% like the tag style, but without fit rectangle
no word baseline/.style={for tree={parent anchor=south, child anchor=north,align=center,base=top},
  where n children=0{delay=with translation}{}},
%
% a style that creates an arrow pointing to the substitution node from a tree top node encoded as daughter
substitution/.style={edge={<-,dashed},l+=\baselineskip},
%
%
% auxiliary nodes without node label
%empty nodes/.style={
%  delay={where content={}{shape=coordinate,for parent={for children={anchor=north}}}{}}}
}



% Manual page 52
%% \forestset{
%% nice empty nodes/.style={
%% for tree={calign=fixed edge angles},
%% delay={where content={}{shape=coordinate,for parent={for children={anchor=north}}}{}}
%% }}


% forest 2.0
\forestset{
     empty nodes/.style={
     delay={where content={}{shape=coordinate,for siblings={anchor=north}}{}}
     }
   }

\forestset{
  % Easy extra edges
  edge to'/.style 2 args={
    tikz+={\path[#2](#1.parent anchor)--(.child anchor);}
  },
  edge to/.style={
    edge to'/.expanded={\unexpanded{#1}}{\forestoption{edge}},
  },
  edge from'/.style 2 args={
    tikz+={\path[#2](.parent anchor)--(#1.child anchor);}
  },
  edge from/.style={
    edge from'/.process=_O{#1}{#1.edge},
  },
  % Identify two (adjacent, vertically aligned) nodes
  forget@node@boundary/.code={%
    \forestolet{positive@edge@#1}\relax
    \forestolet{negative@edge@#1}\relax
  },
  identify/.style={
    phantom,
    temptoksa/.option=#1.name,
    before typesetting nodes/.process=Rw1{temptoksa}{
      content/.option=##1.content,
      content format/.option=##1.content format,
      node format/.option=##1.node format,
    },
    for parent/.process=Ow1{n}{
      after packing node={
        for ##1={
          ignore,ignore edge,forget@node@boundary/.option=grow,
        },
      },
    },
    for nodewalk/.process=Rw1Ow1{temptoksa}{name=##1}{!u.name}{edge to={##1}},
  },
  % Uniformly spread any sequence of nodes
  spread/.style n args=4{
    % #1 = coordinate (x,y,s,l)
    % #2 = nodewalk to spread
    % #3 = relative node name of the node with the final coordinate
    % #4 = final coordinate shift
    for nodewalk={
      tempcounta'=-1,
      #2,
      tempdima/.pgfmath={(#1("#3")+#4-#1(""))/tempcounta()}
    }{tempcounta'+=1},
    tempdimb'=0pt,
    for nodewalk={#2}{
      #1'+/.register=tempdimb,
      tempdimb'+/.register=tempdima,
    },
  },
}

% The old type hierarchy style
\forestset{
typehierarchy/.style={for tree={parent anchor=south, child
% old version:
%    anchor=north,align=center,base=top,font=\itshape,fit=rectangle}},
% to get parallel lines:
    anchor=north,align=center,base=top,font=\itshape,calign=fixed angles}},
% if there are instances as leaves in the hiarchy
instance/.style={edge=dotted}
}

% Type hierarchy (Saso 2020)
\forestset{
  type hierarchy/.style={
    for tree={
      anchor=base,
%anchor=north,
      content format={\noexpand\type{\unexpanded{#1}\forestoption{content}}},
    },
  },
  partition/.style={
    content format={\noexpand\textsc{\forestoption{content}}},
    draw,
  },
  instance/.style={
    gray,edge={gray,dashed}
  },
  % if all leaves in the hierarchy are instances, we can declare this as follows:
  % nodes which do not have children (n children=0) are drawn in gray and edges leading to them are dashed 
  instances/.style={
      for tree={
      s sep=0,
      if n children=0{
        gray,
        edge={dashed,gray},
      }{},
  }},
}

% AVM helper
\forestset{
  avm/.style={
    content format={%
      \noexpand\avmoptions{#1}%
      \noexpand\begin{avm}%
        \forestoption{content}%
      \noexpand\end{avm}%
    },
  },
}