-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathforest-doc.tex
6949 lines (5703 loc) · 322 KB
/
forest-doc.tex
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% forest-doc.tex
%% `forest-doc.tex` is a main documentation source of package `forest`.
%%
%% Copyright (c) 2012-2017 Saso Zivanovic
%% (Sa\v{s}o \v{Z}ivanovi\'{c})
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest versionx of this license is in
%%
%% http://www.latex-project.org/lppl.txt
%%
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work has the LPPL maintenance status `author-maintained'.
%%
%% This file is a part of package `forest'. For the list of files
%% constituting the package see main source file of the package,
%% `forest.dtx', or the derived `forest.sty'.
%%
\documentclass{ltxdoc}
\usepackage[a4paper,
top=2cm,bottom=2.5cm,
reversemarginpar,marginparwidth=2cm,
right=1.5cm,
nohead
]{geometry}
\immediate\write18{makeindex -s forest-doc.ist forest-doc}
\def\indexlettertotoc#1\relax{%
\phantomsection\addcontentsline{toc}{subsection}{#1}%
#1%
}
\makeatletter
\c@IndexColumns=2 \makeatother
\IndexPrologue{Color legend: \textcolor{red}{definition}, \textcolor{darkgreen}{example}, \textcolor{blue}{other}. If an entry belongs to a library, the library name is given in parenthesis. All page numbers are hyperlinks, and definitions in text are hyperlinked to this index.}
\makeindex
\makeatletter
\def\bigbadforlist{}
\def\forest@@doc@@hook@bigbadforlist{\aftergroup\forest@@doc@@hook@bigbadforlist@}
\def\forest@@doc@@hook@bigbadforlist@{%
\forestset{define long step/.append code={%
\ifforest@nodewalkstephandler@makefor
\appto\bigbadforlist{[##1]}%
\fi
}%
}%
}
\def\bigbadprocesslist{}
\makeatother
\usepackage[external]{forest}
\useforestlibrary{edges}
\useforestlibrary{linguistics}
% \tikzexternalize
% forest-tikzexternalize either contains \tikzexternalize or not ... make adjusts this
\IfFileExists{forest-doc.tex-externalize.tex}{%
\input forest-doc.tex-externalize\relax
}{}
\tikzset{
external/prefix={forest.for.dir/},
external/system call={
pdflatex \tikzexternalcheckshellescape -halt-on-error -interaction=nonstopmode -jobname "\image" "\texsource"},
external/up to date check=simple,
}
\usepackage{forest-doc}
% \usepackage{patchpgf}
% \newcommand\pgfprofilenewforcommandcs[2]{%
% \expandafter\pgfprofilenewforcommand\expandafter{\csname #1\endcsname}{#2}}
% \newcommand\pgfprofilenewforpgfkey[1]{%
% \edef\marshal{%
% \noexpand\pgfprofilenewforcommandpattern[#1]{\expandonce{\csname pgfk@#1/.@cmd\endcsname}}{####1\noexpand\pgfeov}{####1\noexpand\pgfeov}
% }\marshal
% }
% \usetikzlibrary{profiler}
% \makeatletter
% \pgfprofilenewforcommand{\forestindex@index}{3}
% \pgfprofilenewforcommand{\pgfmathparse}{1}
% \pgfprofilenewforpgfkey{/forest/split}
% \pgfprofilenewforpgfkey{/forest/nodewalk/reverse}
% \pgfprofilenewforcommand{\forest@Nodewalk}{2}
% \pgfprofilenewforcommand{\pgfkeysalso}{1}
% \makeatother
\usepackage{hyperref}
\hypersetup{unicode=true,colorlinks=true,linkcolor=blue,citecolor=blue,hyperindex=false}
\usepackage{url}
\usepackage[numbers]{natbib}
\usepackage[multiple]{footmisc}
\usepackage{tipa}
\usepackage[inline,shortlabels]{enumitem}
\newlist{compactitem}{itemize}{2}
\setlist[compactitem]{nosep,label=\textbullet}
\newlist{arguments}{enumerate}{1}
\setlist[arguments]{nosep,label=\textbf{\#\arabic*}}
\renewcommand\DescribeMacro[1]{\texttt{\detokenize{#1}}\marginpar{\hfill\small\strut\texttt{\detokenize{#1}}}}
\usepackage{printlen}
\usepackage{tabularx}
\DeleteShortVerb\|
\newcommand\OR{\ensuremath{\,|\,}}%
\usepackage{multicol}
%\usepackage[silent]{trace-pgfkeys} % silent, stack, trace, verbose
%\pgfkeystracelevel{trace}
%%% begin listing region: index_macro_style
\forestset{
detokenize/.style={result=\protect\detokenize{##1}},
tt/.style={result=\protect\texttt{##1}},
macro/.style={detokenize, +result={\char\escapechar}, tt, print in color, hyper},
}
%%% end listing region: index_macro_style
\forestset{
pgfkey/.style={tt,print in color,hyper},
path/.style={+result={/},pgfkey},
handler/.style={+result={.},pgfkey},
option value/.style={
format={tt,result+={ value}},
for first={format=pgfkey},
},
environment/.style={
detokenize,
tt,
print in color,
if stage={index}{}{
if index entry type={definition}{
temptoksa/.register=result,
hyper,
result/.process=ROw2{result}{argument}{%
\texttt{\string\begin\char`\{##1\char`\}}%
##2%
\texttt{\string\end\char`\{##1\char`\}}%
}
}{
hyper
}
}
},
empty/.style={result={{}}},
meta/.style={result=\protect\meta{##1},print in color,hyper},
index library/.style={
if index entry type={definition}{
for first leaf'={
index format+={
result+'={\space
{\protect\scriptsize
(\protect\texttt{\protect\hyperrefnocolor[sec:library-#1]{#1}})%
}%
}
}
},
}{}
},
margin/.style={print format={result=\marginpar{\strut##1}}},
also margin/.style={print format={result=##1\marginpar{\strut##1}}},
}
\let\origmeta\meta
\renewcommand\meta[1]{\origmeta{\rm\ignorespaces#1}}
\renewcommand\rstyle{\color{red}}
\newcommand\hyperrefnocolor[2][]{{\hypersetup{linkcolor=black}\hyperref[#1]{#2}}}
\newcommand\librarysection[1]{%
\subsection{\protect\texttt{#1}}%
\label{sec:library-#1}%
\forestset{every index end/.style={set={index library={#1}}}}%
}
\newcommand\reflibrary[1]{\hyperref[sec:library-#1]{\texttt{#1}}}
\index[not print,not index,set={
index key=unknown,
form={\textbf{unknown!!}},
for first={format={
result/.expanded=\noexpand\textbf{\forestregister{result}??},
print in color}}}
]>{@unknown}
\indexset
[print form={},for first={format=pgfkey}]
>{node key}
\indexset
[for first={format={+result={/tikz/},pgfkey}}]
>{tikz key}
\indexset
[for first={format=pgfkey}]
>{option,readonly option,bracket key,nodewalk key,register,propagator,style,nodewalk style,stage,dynamic tree,step,loop,conditional,anchor,package option}
%%% begin listing region: index_macro_category
\indexset
[for first={format=macro}]
>{macro}
%%% end listing region: index_macro_category
\indexset
[for first={format=environment}]
>{environment}
\indexset
[format=empty]>{empty}
\indexset
[index form={type (of options and registers)},for first={format=meta}]
>{type}
\indexset
[for first={format=handler}]
>{handler}
\indexset
[format={aggregate function},for first={format=handler}]
>{aggregate}
\indexset
[form={\texttt{pgfmath} function},for first={format={detokenize,pgfkey}}]
>{pgfmath}
\indexset
[for first={format=path}]
>{path}
\indexset[
form={},
index key={},
for first={
for first={
key+/.option=!parent.argument,
index format={
result+/.process={Ow1} {!parent.argument} {\protect\meta{##1 option}},
},
print format={
if argument={}{}{
result+/.option=argument
},
},
format={
pgfkey
},
}
}
]>{prefix}
\indexset[
form={},
index key={},
for first={
for first={
+key/.option=!parent.argument,
index format={
+result/.process={Ow1} {!parent.argument} {\protect\meta{##1 option}},
},
print format={
if argument={}{}{
+result/.option=argument
},
},
format={
pgfkey
},
}
}
]>{suffix}
\indexset[
form={},
index key={},
for first={
for first={
key/.process={OOw2} {key} {!parent,parent.argument} {##1 ##2},
index format={
result+/.process={Ow1} {!parent,parent.argument} {\space\protect\meta{##1}},
},
print format={
if argument={}{}{
result+={\space},
result+/.option=argument
}
},
format={
pgfkey
},
}
}
]>{word prefix}
\indexset[
form={},
index key={},
for first={
for first={
key/.process={OOw2} {key} {!parent,parent.argument} {##2 ##1},
index format={
+result/.process={Ow1} {!parent,parent.argument} {\protect\meta{##1}\space},
},
print format={
if argument={}{}{
+result={\space},
+result/.option=argument
}
},
format={
pgfkey
},
}
}
]>{word suffix}
\indexset[%option values
index key format={result/.expanded=\forestoption{argument} value},
format={result/.expanded=\noexpand\texttt{\forestoption{argument}} value},
for first={format=pgfkey}
]>{value of}
\indexset[
index key format={result/.expanded=\forestoption{argument} option},
format={result/.expanded=\noexpand\texttt{\forestoption{argument}} option},
for first={format=pgfkey}
]>{option of}
\indexset
[form={},for first={
print format={if argument={}{}{
result+={:\ },
result+/.option=argument,
}},
format=meta,
}]>{meta}
\indexset[
for first={+key={{cs }},format={pgfkey}}
]>{forest cs}
\indexset[
for first={+key={{ss }},format={pgfkey}}
]>{short step}
\indexset
[for first={format=pgfkey}]
>{named nodewalk}
\indexset[
format={result/.expanded=\noexpand\texttt{process instruction}},
for first={format=pgfkey}
]>{processor}
{\makeatletter % an dirty patch: \lst@nolig can sneak in the name...
\gdef\myexampleindex#1{{\def\lst@nolig{}\lstaspectindex{#1}{}}}
}
\lstset{indexstyle={[1]\myexampleindex}}
%%% end lst-related stuff
\EnableCrossrefs
\setlength\hfuzz{15pt} % dont make so many
\hbadness=7000 % over and under full box warnings
\usetikzlibrary{intersections}
\tikzset{>=latex}
\forestset{
background tree/.style={
for tree={text opacity=0.2,draw opacity=0.2,edge={draw opacity=0.2}}}
}
\title{\FoRest;: a \PGF;/\TikZ;-based package for drawing linguistic trees\\\normalsize\forestversion}
\author{Sa\v so \v Zivanovi\'c\footnote{e-mail:
\href{mailto:[email protected]}{[email protected]};
web:
\href{http://spj.ff.uni-lj.si/zivanovic/}{http://spj.ff.uni-lj.si/zivanovic/}}}
%\usepackage[verbose]{trace-pgfkeys} % silent, stack, trace, verbose
%\pgfkeystracelevel{trace}
\begin{document}
\typeout{)}% hacking auctex's error finding routing
% short verbatim: | (changes spaces into _)
\DeleteShortVerb\|
\def\indexpipe{|}
{\catcode`\_=12 \def\marshal{%
\lstMakeShortInline[basicstyle=\ttfamily,literate={_}{ }1 {__}{_}1]}%
\expandafter}\marshal |
\IfFileExists{forest-doc-test.tex}{%
\input forest-doc-test\relax
}{}
\newbox\treebox
\newbox\codebox
{\settodayfromforestdate\maketitle}
\begin{abstract}
\FoRest; is a \PGF;/\TikZ;-based package for drawing linguistic (and
other kinds of) trees. Its main features are
\begin{enumerate*}[(i)]
\item a packing algorithm which can produce very compact trees;
\item a user-friendly interface consisting of the familiar bracket encoding of trees plus the
key--value interface to option-setting;
\item many tree-formatting options, with control over option values of individual nodes and
mechanisms for their manipulation;
\item a powerful mechanism for traversing the tree;
\item the possibility to decorate the tree using the full power of \PGF;/\TikZ;;
\item an externalization mechanism sensitive to code-changes.
\end{enumerate*}
\end{abstract}
\vspace{1cm}
\begin{forestexample}[basicstyle=\ttfamily\scriptsize,layout=tree on top,no label,v sep=1cm,index={content,pgfmath,conditional>if,repeat,append,before drawing tree,y,alias,for step,step>children,edge,before typesetting nodes,tree,s sep,dimen+,option>l,ancestors,typeset node,nodewalk,readonly option>level,sort by,step>min}]
\pgfmathsetseed{14285}
\begin{forest}
random tree/.style n args={3}{% #1 = max levels, #2 = max children, #3 = max content
content/.pgfmath={random(0,#3)},
if={#1>0}{repeat={random(0,#2)}{append={[,random tree={#1-1}{#2}{#3}]}}}{}},
before typesetting nodes={for tree={draw,s sep=2pt,rotate={int(30*rand)},l+={5*rand},
if={isodd(level())}{fill=green}{fill=yellow}}},
important/.style={draw=red,line width=1.5pt,edge={red,line width=1.5pt}},
before drawing tree={sort by=y, for nodewalk={min=tree,ancestors}{important,typeset node}}
[,random tree={9}{3}{100}]
\end{forest}
\end{forestexample}%
\begin{center}\mbox{}\box\treebox\\\box\codebox\end{center}
\newpage
{%
\parskip 0pt
\begin{multicols}{2}
\tableofcontents
\end{multicols}
}
\newpage
\section{Introduction}
Over several years, I had been a grateful user of various packages
for typesetting linguistic trees. My main experience was with
|qtree| and |synttree|, but as far as I can tell, all of the tools
on the market had the same problem: sometimes, the trees were just
too wide. They looked something like the tree on the left,
while I wanted something like the tree on the right.
\begin{center}
\begin{forest}
baseline,
for tree={parent anchor=south,child anchor=north,l=7ex,s sep=10pt},
for children={fit=rectangle}
[CP
[DP
[D][NP[N][CP[C][TP[T][vP[v][VP[DP][V'[V][DP]]]]]]]]
[TP
[T][vP[v][VP[DP][V'[V][DP]]]]]
]
\end{forest}
\hfill
\begin{forest}
baseline,
for tree={parent anchor=south,child anchor=north,l=7ex,s sep=10pt},
[CP
[DP
[D][NP[N][CP[C][TP[T][vP[v][VP[DP][V'[V][DP]]]]]]]]
[TP
[T][vP[v][VP[DP][V'[V][DP]]]]]
]
\end{forest}
\end{center}
Luckily, it was possible to tweak some parameters by hand to get a
narrower tree, but as I quite dislike constant manual adjustments, I
eventually started to develop \foRest;. It started out as
|xyforest|, but lost the |xy| prefix as I became increasingly fond
of \PGF;/\TikZ;, which offered not only a drawing package but also a
`programming paradigm.' It is due to the awesome power of the
supplementary facilities of \PGF;/\TikZ; that \foRest; is now, I
believe, the most flexible tree typesetting package for \LaTeX\ you can get.
The latest stable version of \FoRest; is \href{http://www.ctan.org/pkg/forest}{available at CTAN}.
Development version(s) can be found \href{https://github.com/sasozivanovic/forest}{at GitHub}.
Comments, criticism, suggestions and code are all very welcome! If you find the package useful, you
can show your appreciation by making a PayPal donation to \url{[email protected]}.
\section{Tutorial}
\label{sec:tutorial}
This short tutorial progresses from basic through useful to obscure \dots fortunately, it is not the
only newcomer's source of information on \foRest;: check out
\href{http://mirrors.ctan.org/info/forest-quickstart/ForestQuickstart.pdf}{Forest Quickstart Guide
for Linguists}. Another very useful source of information (and help!) about \foRest; and \TeX\ in
general is \href{http://tex.stackexchange.com}{\TeX\ StackExchange}. Check out the questions tagged
\href{http://tex.stackexchange.com/questions/tagged/forest}{\texttt{forest}}!
\subsection{Basic usage}
\label{tut:basic-usage}
A tree is input by enclosing its specification in a \index{forest}
environment. The tree is encoded by \emph{the bracket syntax}:
every node is enclosed in square brackets; the children of a
node are given within its brackets, after its content.
{\lstdefinelanguage[my]{TeX}[LaTeX]{TeX}{keywords=forest,
otherkeywords={[,]},keywordstyle=\pstyle,texcsstyle={}}%
\lstset{language={[my]TeX}}%
\begin{forestexample}
\begin{forest}
[VP
[DP]
[V'
[V]
[DP]
]
]
\end{forest}
\end{forestexample}}
Binary trees are nice, but not the only thing this package can draw.
Note that by default, the children are vertically centered with
respect to their parent, i.e.\ the parent is vertically aligned with the midpoint between the
first and the last child.
\begin{forestexample}
\begin{forest}
[VP
[DP[John]]
[V'
[V[sent]]
[DP[Mary]]
[DP[D[a]][NP[letter]]]
]
]
\end{forest}
\end{forestexample}
Spaces around brackets are ignored --- format your code as you
desire!
\begin{forestexample}
\begin{forest}
[VP[DP][V'[V][DP]]]
\end{forest}
\quad
\begin{forest}[VP
[DP ] [ V'[V][ DP]]
]\end{forest}
\end{forestexample}
If you need a square bracket as part of a node's content, use
braces. The same is true for the other characters which have a
special meaning in the \foRest; package, like comma
|,| and equality sign |=|.
\begin{forestexample}
\begin{forest}
[V{P,}
[{[DP]}]
[V'
[V]
[{===DP===}]]]
\end{forest}
\end{forestexample}
Macros in a node specification will be expanded when the node is
drawn --- you can freely use formatting commands inside nodes!
\begin{forestexample}
\begin{forest}
[VP
[{~\textbf~{DP}}]
[V'
[V]
[DP]]]
\end{forest}
\end{forestexample}
All the examples given above produced top-down trees with centered children. The other sections
of this manual explain how various properties of a tree can be changed, making it possible to
typeset radically different-looking trees. However, you don't have to learn everything about this
package to profit from its power. Using styles, you can draw predefined types of trees with ease.
For example, a phonologist can use the \index{GP1} style from library \reflibrary{linguistics} to easily
typeset (Government Phonology) phonological representations. The style is applied simply by
writing its name before the first (opening) bracket of the tree.
\begin{forestexample}[label=ex:gp1-frost,code prefix={\def\usepackage[##1]##2{}}]
\usepackage[~linguistics~]{forest}
% ...
\begin{forest} ~GP1~ [
[O[x[f]][x[r]]]
[R[N[x[o]]][x[s]]]
[O[x[t]]]
[R[N[x]]]
]\end{forest}
\end{forestexample}
Of course, someone needs to develop the style --- you, me, your local \TeX nician \dots\@
Fortunately, designing styles is not very difficult once you get the hang of \foRest;, if you
write one, please contribute! Some macros relating to various fields are collected in
\emph{libraries} that are distributed alongside the main package. This is the case for the
\index{GP1} style used above, which is defined in the |linguistics| library. The simplest way to
load a library is as shown in the example, by loading the package with an optional argument. For
more information on loading libraries, see \S\ref{ref:package-options}.
\subsection{Options}
\label{tut:options}
A node can be given various options, which control various
properties of the node and the tree. For example, at the end of
section~\ref{tut:basic-usage}, we have seen that the \index{GP1} style
vertically aligns the parent with the first
child. This is achieved by setting option \index{calign} (for
\emph{c}hild-\emph{align}ment) to \index{value of=calign>first} (child).
Let's try. Options are given inside the brackets, following the
content, but separated from it by a comma. (If multiple options are
given, they are also separated by commas.) A single option
assignment takes the form \meta{option name}|=|\meta{option value}. (There are
also options which do not require a value or have a default value:
these are given simply as \meta{option name}.)
\begin{forestexample}[label=ex:numerals-simple,index={calign,value of=align>first}]
\begin{forest}
[\LaTeX\ numerals, ~calign=first~
[arabic[1][2][3][4]]
[roman[i][ii][iii][iv]]
[alph[a][b][c][d]]
]
\end{forest}
\end{forestexample}
The experiment has succeeded only partially. The root node's
children are aligned as desired (so \index{calign}|=|\index{value of=calign>first} applied to the
root node), but the value of the \index{calign} option didn't get
automatically assigned to the root's children! \emph{An option given
at some node applies only to that node.} In \foRest;, the options
are passed to the node's relatives via special keys, called
\emph{propagators}. What we need above is the \index{for step=\index{tree}}
propagator. Observe:
\begin{forestexample}[label=ex:numerals-manual]
\begin{forest}
[\LaTeX\ numerals,
~for tree~={calign=first}
[arabic[1][2][3][4]]
[roman[i][ii][iii][iv]]
[alph[a][b][c][d]]
]
\end{forest}
\end{forestexample}
The value of propagator \index{for step=\index{tree}} is a list of keys that we
want to process. This keylist is propagated to all the nodes in
the subtree\footnote{It might be more precise to call \index{for step=\index{tree}}
\texttt{for subtree} \dots\@ but this name at least saves some typing.}
rooted in the current node (i.e.\ the node where \index{for step=\index{tree}} was
given), including the node itself. (Propagator \index{for step=\index{descendants}} is
just like \index{for step=\index{tree}}, only that it excludes the node itself. There
are many other \index{for step=\meta{step}} propagators; for the complete list, see
sections~\ref{ref:spatial-propagators} and \ref{ref:nodewalks}.)
Some other useful options are \index{option>parent anchor}, \index{option>child anchor} and
\index{tier}. The \index{option>parent anchor} and \index{option>child anchor} options tell
where the parent's and child's endpoint of the edge between them should be, respectively: usually,
the value is either empty (meaning a smartly determined border point
\citep[see][\S16.11]{tikzpgf2.10}; this is the default) or a compass direction
\citep[see][\S16.5.1]{tikzpgf2.10}. (Note: the \index{option>parent anchor} determines where the edge
from the child will arrive to this node, not where the node's edge to its parent will start!)
Option \index{tier} is what makes the
skeletal points $\times$ in example (\ref{ex:gp1-frost}) align horizontally although they
occur at different levels in the logical structure of the tree.
Using option \index{tier} is very simple: just set |tier=tier_name| at
all the nodes that you want to align horizontally. Any tier name
will do, as long as the tier names of different tiers are
different \dots\@ (Yes, you can have multiple tiers!)
\begin{forestexample}[point={tier},index={option>parent anchor,option>child anchor,tier},label=ex:tier-manual]
\begin{forest}
[VP, for tree={~parent anchor~=south, ~child anchor~=north}
[DP[John,tier=word]]
[V'
[V[sent,tier=word]]
[DP[Mary,tier=word]]
[DP[D[a,tier=word]][NP[letter,tier=word]]]
]
]
\end{forest}
\end{forestexample}
Before discussing the variety of \foRest;'s options, it is worth
mentioning that \foRest;'s node accepts all options \citep[see
\S16]{tikzpgf2.10} that \TikZ;'s node does --- mostly, it just passes
them on to \TikZ;. For example, you can easily encircle a node like
this:\footnote{If option \texttt{draw} was not given, the shape of the node
would still be circular, but the edge would not be drawn. For
details, see \cite[\S16]{tikzpgf2.10}.}
\begin{forestexample}
\begin{forest}
[VP,~circle~,~draw~
[DP][V'[V][DP]]
]
\end{forest}
\end{forestexample}
Let's have another look at example (\ref{ex:gp1-frost}). You will note that the skeletal
positions were input by typing |x|s, while the result looks like
this: $\times$ (input as |\times| in math mode). Obviously, the
content of the node can be changed. Even more, it can be
manipulated: added to, doubled, boldened, emphasized, etc. We will
demonstrate this by making example (\ref{ex:numerals-manual}) a bit
fancier: we'll write the input in the arabic numbers and have
\LaTeX\ convert it to the other formats. We'll start with the
easiest case of roman numerals: to get them, we can use the (plain)
\TeX\ command |\romannumeral|. To change the content of the node,
we use option \index{content}. When specifying its new value, we can use
|#1| to insert the current content.\footnote{This mechanism is called
\emph{wrapping}. By default, \index{content} is the only \index{autowrapped toks} option,
i.e.\ option where wrapping works implicitely (simply
because I assume that wrapping will be almost exclusively used with this option). To wrap values
of other options, use handler \index{wrap value}; see~\S\ref{ref:handlers}.}
\begin{forestexample}[point={content,delay},index={for step,step>children,content,delay},label=ex:romannumeral]
\begin{forest}
[roman, delay={for children={content=\romannumeral#1}}
[1][2][3][4]
]
\end{forest}
\end{forestexample}
This example introduces another option: \index{delay}. Without it, the example wouldn't work: we
would get arabic numerals. This is so because of the order in which the options are processed.
First, the processing proceeds through the tree in a depth-first, parent-first fashion (first the
parent is processed, and then its children, recursively; but see \index{processing order}). Next,
the option string of a node is processed linearly, in the order they were given. Option
\index{content} is specified implicitely and is always the first. If a propagator is encountered,
the options given as its value are propagated \emph{immediately}. The net effect is that if the
above example contained simply |roman,for_children={content=...}|, the \index{content} option
given there would be processed \emph{before} the implicit content options given to the children
(i.e.\ numbers |1|, |2|, |3| and |4|). Thus, there would be nothing for the |\romannumeral| to
change --- it would actually crash; more generally, the content assigned in such a way would get
overridden by the implicit content. Key \index{delay} is true to its name. It delays the
processing of the keylist given as its argument until the whole tree was processed. In other words, it
introduces cyclical option processing. Whatever is delayed in one cycle, gets processed in the next
one. The number of cycles is not limited --- you can nest \index{delay}s as deep as you need.
Unlike \index{for step=\meta{step}} keys we have met before, \index{delay} is not a
spatial, but a temporal propagator. Several other temporal propagators options exist, see
\S\ref{ref:stages}.
We are now ready to learn about simple conditionals.\footnote{See \S\ref{ref:conditionals} for
further information on conditionals, including the generic \index{if} and \index{where}.} Every
node option has the corresponding \index{if option=\meta{option}} and \index{where
option=\meta{option}} keys. \index{if option=\meta{option}}|=|\meta{value}\meta{true
options}\meta{false options} checks whether the value of \meta{option} equals \meta{value}. If
so, \meta{true options} are processed, otherwise \meta{false options}. The \index{where
option=\meta{option}} keys are the same, but do this for the every node in the subtree; informally
speaking, |where| = |for_tree| + |if|. To see this in action, consider the rewrite of the
\index{tier} example (\ref{ex:tier-manual}) from above. We don't set the tiers manually, but rather
put the terminal nodes (option \index{n children} is a read-only option containing the number of
children) on tier \keyname{word}.\footnote{We could omit the braces around \texttt{0} because it is
a single character. If we were hunting for nodes with 42 children, we'd have to write
\texttt{where n children=\{42\}...}.}
\begin{forestexample}[index={tier,where option,n children}]
\begin{forest}
~where n children~=0{tier=word}{}
[VP
[DP[John]]
[V'
[V[sent]]
[DP[Mary]]
[DP[D[a]][NP[letter]]]
]
]
\end{forest}
\end{forestexample}
\begin{advise}
\item Note that you usually don't want to embed a \index{where option=...} conditional in a \index{for
step=\index{tree}}, as this will lead to a multiple traversal of many nodes, resulting in a
slower execution. If you're inside a \index{for step=\index{tree}}, you probably want to use
\index{if}.
\end{advise}
Finally, let's talk about styles.
(They are not actually defined in the \foRest; package, but rather
inherited from |pgfkeys|.)
At the first approximation, styles are abbreviations: if you
often want to have non-default parent/child anchors, say south/north as in example
(\ref{ex:tier-manual}), you could save some typing by defining a style. Styles are defined using
\PGF;'s handler |.style|, like shown below.\footnote{Style \index{sn edges} is actually already
defined by library \reflibrary{linguistics}. The definition there is a bit more generic.}
\begin{forestexample}[index={tier,option>parent anchor,option>child anchor}]
\begin{forest}
~sn edges~/~.style~={for tree={
parent anchor=south, child anchor=north}},
~sn edges~
[VP,
[DP[John,tier=word]]
[V'
[V[sent,tier=word]]
[DP[Mary,tier=word]]
[DP[D[a,tier=word]][NP[letter,tier=word]]]]]
\end{forest}
\end{forestexample}
If you want to use a style in more than one tree, you have to define it outside the \index{forest}
environment. Use macro \index{forestset} to do this.
\begin{lstlisting}
~\forestset~{
sn edges/.style={for tree={parent anchor=south, child anchor=north}},
background tree/.style={for tree={
text opacity=0.2,draw opacity=0.2,edge={draw opacity=0.2}}}
}
\end{lstlisting}
You might have noticed that in the last two examples, some keys occurred even before the
first opening bracket, contradicting was said at the beginning of this section. This is mainly
just syntactic sugar (it can separate the design and the content): such \emph{preamble}
keys behave as if they were given in the root node, the only difference (which often does not
matter) being that they get processed before all other root node options, even the implicit
\index{content}.
If you find yourself writing the same preamble for every tree in your document,
consider modifying \index{default preamble}, which is implicitely included at the beginning
of every preamble.
\begin{forestexample}[index={default preamble}]
\forestset{
~default preamble~={
font=\Huge,
for tree={circle,draw}
}
}
\begin{forest} [A[B][C]] \end{forest}
\begin{forest} red [D[E][F]] \end{forest}
\begin{forest} for tree={dotted} [G[H][I]] \end{forest}
\end{forestexample}
\subsection{Decorating the tree}
\label{tut:decorating}
The tree can be decorated (think movement arrows) with arbitrary
\TikZ; code.
\begin{forestexample}
\begin{forest}
[XP
[specifier]
[X$'$
[X$^0$]
[complement]
]
]
~\node at (current bounding box.south)
[below=1ex,draw,cloud,aspect=6,cloud puffs=30]
{\emph{Figure 1: The X' template}};~
\end{forest}
\end{forestexample}
However, decorating the tree would make little sense if one could
not refer to the nodes. The simplest way to do so is to give them a
\TikZ; name using the \index{option>name} option, and then use this name in \TikZ;
code as any other (\TikZ;) node name.
\begin{forestexample}[point=name,index={option>phantom,option>name}]
\begin{forest}
[CP
[DP,name=spec CP]
[\dots
[,phantom]
[VP
[DP]
[V'
[V]
[DP,name=object]]]]]
\draw[->,dotted] ~(object)~ to[out=south west,in=south] ~(spec CP)~;
\end{forest}
\end{forestexample}
It gets better than this, however! In the previous examples, we put
the \TikZ; code after the tree specification, i.e.\ after the closing
bracket of the root node. In fact, you can put \TikZ; code after
\emph{any} closing bracket, and \foRest; will know what the current
node is. (Putting the code after a node's bracket is actually just a
special way to provide a value for option \index{tikz} of that node.) To
refer to the current node, simply use an empty node name. This works both with and without
anchors \citep[see][\S16.11]{tikzpgf2.10}: below, |(.south east)| and |()|.
\begin{forestexample}[index={option>phantom,option>name}]
\begin{forest}
[CP
[DP,name=spec CP]
[\dots
[,phantom]
[VP
[DP]
[V'
[V]
[DP,draw] ~{~
\draw[->,dotted] ~()~ to[out=south west,in=south] (spec CP);
\draw[<-,red] ~(.south east)~--++(0em,-4ex)--++(-2em,0pt)
node[anchor=east,align=center]{This guy\\has moved!};
~}~
]]]]
\end{forest}
\end{forestexample}
Important: \emph{the \TikZ; code should usually be enclosed in braces} to hide
it from the bracket parser. You don't want all the bracketed code
(e.g.\ |[->,dotted]|) to become tree nodes, right? (Well, they
probably wouldn't anyway, because \TeX\ would spit out a thousand
errors.)
\bigskip
Finally, the most powerful tool in the node reference toolbox:
\emph{relative nodes}. It is possible to refer to other nodes which stand
in some (most often geometrical) relation to the current node. To
do this, follow the node's name with a \index>{!} and a \emph{nodewalk}
specification.
A nodewalk is a concise\footnote{Actually, \foRest; distinguishes two kinds of steps in node walks:
long-form and short-form steps. This section introduces only short-form steps. See
\S\ref{ref:nodewalks}.} way of expressing node relations. It is simply a string of steps, which
are represented by single characters, where: \index{ss u} stands for the parent node (up); \index{ss
p} for the previous sibling; \index{ss n} for the next sibling; \index{ss s} for \emph{the}
sibling (useful only in binary trees); \index{ss 1}, \index{ss 2}, \index[not print]{ss 3,ss 4,ss
5,ss 6,ss 7,ss 8}\dots\ \index{ss 9} for first, second, \dots\ ninth child; \index{ss l}, for the
last child, etc. For the complete specification, see section~\ref{ref:short-form-steps}.
To see the nodewalk in action, consider the following examples.
In the first example, the agree arrow connects the V node, specified
simply as |()|, since the \TikZ; code follows |[V]|, and the DP node,
which is described as ``a sister of V's parent'': |!us| = up +
sibling.
\begin{forestexample}[index>={!}]
\begin{forest}
[VP
[DP]
[V'
[V] {\draw[<->] ~()~
.. controls +(left:1cm) and +(south west:0.4cm) ..
node[very near start,below,sloped]{\tiny agree}
~(!us)~;}
[DP]
]
]
\end{forest}
\end{forestexample}
\begingroup
\footnotesize
\begin{forestexample}[index={option>phantom,tikz,fit to,tree},layout=export,basicstyle=\footnotesize\ttfamily]
\begin{forest}
[CP
[DP$_1$]
[\dots
[,phantom]
[VP,tikz={\node [draw,red,inner sep=0,~fit to~=tree]{};}
[DP$_2$]
[V'
[V]
[DP$_3$]
]]]]
\end{forest}
\end{forestexample}%
\endgroup
The second example uses \TikZ;'s fitting library (automatically loaded by \foRest;) to compute the
smallest rectangle containing node VP, its first child (DP$_2$) and its last grandchild (DP$_3$).
The example also illustrates that the \TikZ; code
can be specified via the ``normal'' option syntax, i.e.\ as a value
to option \index{tikz}.\footnote{\label{fn:fit-to-tree}Actually, there's a simpler way to do this: use \index{fit to}\keyname{=}\index{tree}!\forestexampleimport}
\begin{forestexample}[point=tikz,index={option>phantom,tikz},index>={!}]
\begin{forest}
[CP
[DP$_1$]
[\dots
[,phantom]
[VP,tikz={\node [draw,red,fit=~()(!1)(!ll)~] {};}
[DP$_2$]
[V'
[V]
[DP$_3$]
]]]]
\end{forest}
\end{forestexample}
\subsection{Node positioning}
\label{tut:node-positioning}
\FoRest; positions the nodes by a recursive bottom-up algorithm which, for every non-terminal node,
computes the positions of the node's children relative to their parent. By default, all the
children will be aligned horizontally some distance down from their parent: the ``normal'' tree
grows down. More generally, however, the direction of growth can change from node to node; this is
controlled by option \index{grow}=\meta{direction}.\footnote{The direction can be specified either in
degrees (following the standard mathematical convention that $0$ degrees is to the right, and that
degrees increase counter-clockwise) or by the compass directions: \texttt{east}, \texttt{north east},
\texttt{north}, etc.} The system computes and stores the positions of children using a
coordinate system dependent on the parent, called an \emph{ls-coordinate system}: the origin is the
parent's anchor; l-axis is in the direction of growth in the parent; s-axis is orthogonal to the
l-axis (positive side in the counter-clockwise direction from $l$-axis); l stands for \emph{l}evel,
s for \emph{s}ibling. The example shows the ls-coordinate system for a node with
|grow=45|.\footnote{The axes are drawn using coordinates given in \texttt{forest cs} coordinate
system; the ``manually'' given polar coordinate equivalent is shown in the comment.}
\begin{forestexample}[point=grow,index={grow}]
\begin{forest} background tree
[parent, grow=45
[child 1][child 2][child 3][child 4][child 5]
]
%\draw[,->](-135:1cm)--(45:3cm) node[below]{$l$};