Skip to content

Commit b26445b

Browse files
KenyCKeny C
andauthored
lingotree:1.0.0 (#3474)
Co-authored-by: Keny C <[email protected]>
1 parent 18bd1ac commit b26445b

File tree

14 files changed

+1362
-0
lines changed

14 files changed

+1362
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright © 2025 Keny Chatain
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
# Lingotree
2+
3+
## Installation
4+
5+
See [here](https://github.com/typst/packages?tab=readme-ov-file#local-packages) how to install this package locally. Essentially:
6+
7+
- download this repository
8+
- rename it as: `{data-dir}/typst/packages/local/lingotree/1.0.0` where `{data-dir}` is:
9+
- Linux: `$XDG_DATA_HOME` or, if not set, `~/.local/share`
10+
- Mac: `~/Library/Application Support`
11+
- Windows: `%APPDATA%`
12+
13+
14+
15+
## Usage
16+
17+
Below are a couple of illustrations of the package. Cf [manual](manual.pdf) for a full reference of the functions.
18+
19+
### Basic
20+
21+
22+
```typst
23+
#import "@preview/lingotree:1.0.0": *
24+
25+
#render(
26+
tree(
27+
tag: [VP],
28+
tree(
29+
tag: [DP],
30+
[every],
31+
[farmer],
32+
),
33+
[smiled]
34+
)
35+
)
36+
```
37+
38+
![Syntactic tree of 'every farmer smiled'](gallery/every_farmer_smiled.svg)
39+
40+
#### Non-binary trees
41+
42+
```typst
43+
#import "@preview/lingotree:1.0.0": *
44+
45+
#render(
46+
tree(
47+
tag: [VP],
48+
tree(
49+
tag: [VP],
50+
[Millie],
51+
[left],
52+
),
53+
[and],
54+
tree(
55+
tag: [VP],
56+
[Bill],
57+
[left],
58+
),
59+
)
60+
)
61+
```
62+
63+
![Syntactic tree of 'Millie left and Bill left'](gallery/non-binary-tree.svg)
64+
65+
### Adjusting spacing and color
66+
67+
There are several parameters that tweak the appearance of the tree:
68+
69+
- `layer-spacing`: distance between root of a subtree and its children.
70+
- `child-spacing`: distance between two siblings.
71+
- `branch-stroke`: stoke used to draw the branch.
72+
- `color`: sets the color of both branches and nodes (is overriden by any color you set in `branch-stroke`).
73+
- `align`: alignment of the tafs and leaf nodes.
74+
75+
These parameters may be passed:
76+
77+
- as optional arguments of `render`: in this case, they affect all the tree.
78+
- as optional arguments of `tree`: in this case they affect only the root of the subtree to which they're applied and its immediate children
79+
- as an optional dictionary argument `defaults` to `tree`: in this case, they apply recursively to a tree and all its descendants
80+
81+
82+
```typst
83+
#import "@preview/lingotree:1.0.0": *
84+
85+
86+
#render(
87+
tree(
88+
tree(
89+
[every],
90+
tree(
91+
[Russian],
92+
[novel],
93+
)
94+
),
95+
tree(
96+
[I],
97+
tree(
98+
[read],
99+
tree(
100+
[every],
101+
tree(
102+
[Russian],
103+
[novel],
104+
),
105+
// Here we set default parameters for "every Russian novel"
106+
defaults: (
107+
// Note: the color parameter affects the color of branches and also the color of nodes
108+
color: gray,
109+
branch-stroke: stroke(dash: "dotted", thickness: 0.5pt)
110+
)
111+
),
112+
)
113+
),
114+
layer-spacing: 1em,
115+
)
116+
)
117+
```
118+
![Multi-dominant tree of 'I read every Russian novel'](gallery/adjust-params.svg)
119+
120+
121+
### Movement arrow
122+
123+
In the spirit of modularity, `lingotree` intentionally does not provide movement arrows and other tree annotations. This is something you can implement, using `cetz` (draw tools) and `mannot` (to record positions in a document).
124+
125+
126+
```typst
127+
#import "@preview/lingotree:1.0.0": *
128+
#import "@preview/mannot:0.3.0": mark, annot-cetz
129+
#import "@preview/cetz:0.4.2"
130+
131+
132+
// Renders a tree structure with specific marks indicating the start and end points of movement
133+
134+
135+
#render(
136+
defaults: (align: center),
137+
tree(
138+
tag: [VP],
139+
tree(
140+
defaults: (color: blue),
141+
tag: [D],
142+
[D\ #mark(tag: <endqr>)[an]],
143+
[N\ egg],
144+
),
145+
tree(
146+
tag: [VP],
147+
[DP\ Jane],
148+
tree(
149+
tag: [VP],
150+
[V\ found],
151+
mark(tag: <begqr>, text(blue)[$t$])
152+
),
153+
),
154+
),
155+
)
156+
157+
// Integrate mannot and cetz functionalities
158+
// Creates a canvas to track the positions of the marks in the tree and draws a Bezier arrow between them
159+
160+
#annot-cetz(
161+
(<begqr>, <endqr>),
162+
cetz,
163+
{
164+
cetz.draw.bezier-through(
165+
"begqr.south",
166+
(rel: (x: -1, y: -1)),
167+
168+
// Adjusts the arrow's endpoint position to fall between "an" and "egg"
169+
(rel: (x: 0.3), to: "endqr.south"),
170+
171+
stroke: blue,
172+
mark: (end: "straight"),
173+
)
174+
},
175+
)
176+
```
177+
178+
If you rarely need to be more fancy than an arrow, it may be useful to encapsulate the arrow drawing in a single command. Here is how I do it:
179+
180+
```typst
181+
#let arrow(beg, end, control: (x: -1, y: -.5), stroke: black) = annot-cetz(
182+
(beg, end),
183+
cetz,
184+
{
185+
cetz.draw.bezier-through(
186+
str(beg) + ".south",
187+
(rel: control),
188+
str(end) + ".south",
189+
stroke: stroke,
190+
mark: (end: "straight"),
191+
)
192+
},
193+
)
194+
```
195+
196+
![Tree with movement](gallery/movement.svg)
197+
198+
199+
## See also
200+
201+
- [typst-syntree](https://github.com/lynn/typst-syntree) : a package to typeset tree using a bracket syntax ; more convenient, less powerful

0 commit comments

Comments
 (0)