Skip to content

Commit c22bcef

Browse files
committed
Improved Alignment
1 parent e67eaf6 commit c22bcef

File tree

4 files changed

+2017
-2052
lines changed

4 files changed

+2017
-2052
lines changed

Marginalia.pdf

40.3 KB
Binary file not shown.

lib.typ

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
let clearance = _config.get().clearance
171171
let extends = _note_extends_left.final().at(page_num, default: ())
172172
let offsets_down = ()
173-
let cur = 0pt
173+
let cur = _config.get().top
174174
for note in extends {
175175
// 8pt spacing between nodes
176176
if cur <= note.natural {
@@ -193,7 +193,7 @@
193193
max = note.natural + offsets_down.at(index) - clearance
194194
} else if note.fix {
195195
offsets_final_rev.push(0pt)
196-
max = note.natural - clearance
196+
max = calc.min(note.natural - clearance, max)
197197
} else {
198198
offsets_final_rev.push(max - note.natural - note.height)
199199
max = max - note.height - clearance
@@ -208,7 +208,7 @@
208208
let clearance = _config.get().clearance
209209
let extends = _note_extends_right.final().at(page_num, default: ())
210210
let offsets_down = ()
211-
let cur = 0pt
211+
let cur = _config.get().top
212212
for note in extends {
213213
// 8pt spacing between nodes
214214
if cur <= note.natural {
@@ -231,7 +231,7 @@
231231
max = note.natural + offsets_down.at(index) - clearance
232232
} else if note.fix {
233233
offsets_final_rev.push(0pt)
234-
max = note.natural - clearance
234+
max = calc.min(note.natural - clearance, max)
235235
} else {
236236
offsets_final_rev.push(max - note.natural - note.height)
237237
max = max - note.height - clearance
@@ -249,14 +249,15 @@
249249
let anchor = here().position()
250250
let pagewidth = page.width
251251
let page = here().page()
252-
let lineheight = measure(v(par.leading)).height
253-
254-
let natural_position = anchor.y + dy - lineheight
255252

256253
let width = get-left().width
257254
let notebox = box(width: width, body)
258255
let height = measure(notebox).height
259256

257+
let lineheight = measure([X]).height
258+
lineheight = calc.min(lineheight, height)
259+
let natural_position = anchor.y + dy - lineheight
260+
260261
let current = _note_extends_left.get().at(str(page), default: ())
261262
let index = current.len()
262263

@@ -287,14 +288,15 @@
287288
let anchor = here().position()
288289
let pagewidth = page.width
289290
let page = here().page()
290-
let lineheight = measure(v(par.leading)).height
291-
292-
let natural_position = anchor.y + dy - lineheight
293291

294292
let width = get-right().width
295293
let notebox = box(width: width, body)
296294
let height = measure(notebox).height
297295

296+
let lineheight = measure([X]).height
297+
lineheight = calc.min(lineheight, height)
298+
let natural_position = anchor.y + dy - lineheight
299+
298300
let current = _note_extends_right.get().at(str(page), default: ())
299301
let index = current.len()
300302

@@ -326,15 +328,17 @@
326328
/// - dy (length): Vertical offset of the note.
327329
/// - body (content):
328330
#let note(numbered: true, reverse: false, dy: 0pt, body) = {
329-
set text(size: 0.8em, style: "italic", weight: "regular")
330331
if numbered {
331332
notecounter.step()
332333
let body = context if _config.get().flush-numbers {
334+
set par(spacing: 0.55em, leading: 0.4em, hanging-indent: 0pt)
335+
set text(size: 0.8em, weight: "regular")
333336
notecounter.display(_config.get().numbering)
334337
h(1.5pt)
335338
body
336339
} else {
337-
set par(spacing: 0.55em, leading: 0.4em)
340+
set par(spacing: 0.55em, leading: 0.4em, hanging-indent: 0pt)
341+
set text(size: 0.8em, weight: "regular")
338342
box(
339343
width: 0pt,
340344
{
@@ -367,6 +371,11 @@
367371
})
368372
} else {
369373
h(0pt, weak: true)
374+
let body = {
375+
set par(spacing: 0.55em, leading: 0.4em, hanging-indent: 0pt)
376+
set text(size: 0.8em, weight: "regular")
377+
body
378+
}
370379
box(context {
371380
if reverse or (_config.get().book and calc.even(here().page())) {
372381
_note_left(dy: dy, body)
@@ -442,7 +451,7 @@
442451
} else {
443452
get-right().width
444453
}
445-
let height = measure(width: width, content).height + gap.to-absolute()
454+
let height = measure(width: width, content).height + measure(text(size: 0.8em, weight: "regular", v(gap))).height
446455
if numbered {
447456
h(1.5pt, weak: true)
448457
notecounter.display(_config.get().numbering)

main.typ

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#let config = (
44
inner: ( far: 16mm, width: 20mm, sep: 8mm ),
55
outer: ( far: 16mm, width: 40mm, sep: 8mm ),
6-
top: 32mm + 1em, bottom: 16mm,
6+
top: 32mm + 11pt, bottom: 16mm,
77
book: true,
88
// clearance: 30pt,
99
// flush-numbers: false,
@@ -136,13 +136,20 @@ By default, the #link(label("marginalianote()"))[```typst #note[...]```] command
136136
This is a note.
137137

138138
They can contain any content, and will wrap within the note column.
139+
// #note(dy: 11pt)[Sometimes, they can even contain other notes! (But not always, and I don't know what gives.)]
139140
].
140141
By giving the argument ```typc reverse: true```, we obtain a note on the left/inner margin.#note(reverse: true)[Reversed.]
141142
If ```typc config.book = true```, the side will of course be adjusted automatically.
142143

143-
If~#note[Note 1] we~#note[Note 2] place~#note[Note 3] multiple~#note[Note 4] notes~#note[Note 5] in~#note(dy:15pt)[This note was given ```typc 15pt``` dy, but it was shifted more than that to avoid Notes 1--5.] one~#note(reverse: true, dy:15pt)[This note was given ```typc 15pt``` dy.] line,#note(dy:110pt)[This note was given ```typc 110pt``` dy.] they automatically adjust their positions.
144+
If~#note[Note 1] we~#note[Note 2] place~#note[Note 3] multiple~#note[Note 4] notes~#note[Note 5] in~#note(dy:15pt)[This note was given ```typc 15pt``` dy, but it was shifted more than that to avoid Notes 1--5.] one~#note(reverse: true, dy:15pt)[This note was given ```typc 15pt``` dy.] line,#note(dy:10cm)[This note was given ```typc 10cm``` dy.] they automatically adjust their positions.
144145
Additionally, a ```typc dy``` argument can be passed to shift their initial position by that amount vertically. They may still get shifted around.
145146

147+
Notes will shift downwards to avoid previous notes, containing wideblocks, and the top page margin. Notes will shift upwards to avoid later notes and wideblocks, and the bottom page margin. However, if there is not enough space between two wideblocks or between wideblocks and the margins, there will be collisions.
148+
149+
Currently, notes (and wideblocks) are not reordered,
150+
#note[This note lands below the previous one!]
151+
so two ```typ #note```s are placed in the same order vertically as they appear in the markup, even if the first is shifted with a `dy` such that the other would fit above it.
152+
146153
== Markers
147154
The margin notes are decorated with little symbols, which by default hang into the gap. If this is not desired, set the configuration option ```typc flush-numbers: true```.
148155
Setting the argument ```typc numbered: false```, we obtain notes without icon/number.#note(numbered: false)[Like this.]
@@ -167,6 +174,8 @@ or per heading:
167174
// #note[
168175
// Vertical offsets in this document:
169176
// Right:\
177+
// #context marginalia._note_extends_right.get().at("1") \
178+
// #context marginalia._note_extends_right.final().at("1") \
170179
// #context marginalia._note_offset_right("1") \
171180
// #context marginalia._note_offset_right("2") \
172181
// #context marginalia._note_offset_right("3") \
@@ -192,7 +201,8 @@ or per heading:
192201
The command
193202
```typst #wideblock[...]```
194203
can be used to wrap content in a wide block which spans into the margin-note-column.
195-
It is a bit cluttered, but is possible to use notes in wide blocks:#note[Voila.]#note(reverse: true)[Wow!].
204+
It is possible to use notes in a wide block:#note[Voila.]#note(reverse: true)[Wow!].
205+
They will automatically shift downwards to avoid colliding with the wideblock.
196206
]
197207

198208
#wideblock(reverse: true)[
@@ -201,7 +211,7 @@ or per heading:
201211

202212
#wideblock(double: true)[
203213
```typst #wideblock(double: true)[...]```: The `double` option makes it extend both ways.
204-
Note that setting both `reverse: true` and `double: true` will panic.
214+
Note that setting both `reverse: true` and `double: true` is disallowed and will panic.
205215
]
206216

207217
= Figures
@@ -221,6 +231,12 @@ It accepts all arguments `figure` takes (except `placement` and `scope`), plus a
221231

222232
Additionally, the `dy` argument now takes a relative length, where ```typc 100%``` is the height of the figure content + gap, but without the caption.
223233
By default, figures have a `dy` of ```typc 0pt - 100%```, which results in the caption being aligned horizontally to the text.
234+
#marginalia.notefigure(
235+
dy: 0pt,
236+
rect(width: 100%, fill: gradient.linear(..color.map.crest)),
237+
numbered: true,
238+
caption: [Aligned to top of figure with `dy: 0pt`.],
239+
)
224240

225241
A label can be attached to the figure using the `label` argument.// C.f.~@markedfigure.
226242

@@ -243,22 +259,35 @@ For larger figures, use the following set and show rules:
243259
rect(width: 100%, fill: gradient.linear(..color.map.inferno)),
244260
caption: [A figure.],
245261
)
262+
263+
For wide figures, simply place a figure in a wideblock.
264+
The Caption gets placed beneath the figure automatically, courtesy of regular wide-block-avoidance.
265+
#block[
266+
#set text(size: 0.84em)
267+
```typ
268+
#wideblock[#figure(
269+
image(...),
270+
caption: [A figure in a wide block.]
271+
)]
272+
```
273+
]
274+
// #pagebreak(weak: true)
246275
#wideblock[
247276
#figure(
248277
rect(width: 100%, fill: gradient.linear(..color.map.cividis)),
249-
caption: [A figure in a wideblock.],
278+
caption: [A figure in a wide block.],
250279
)
251280
]
252281
#wideblock(reverse: true)[
253282
#figure(
254-
rect(width: 100%, fill: gradient.linear(..color.map.icefire)),
255-
caption: [A figure in a wideblock.],
283+
rect(width: 100%, height: 5em, fill: gradient.linear(..color.map.icefire)),
284+
caption: [A figure in a reversed wide block.],
256285
)
257286
]
258287
#wideblock(double: true)[
259288
#figure(
260289
rect(width: 100%, fill: gradient.linear(..color.map.spectral)),
261-
caption: [A figure in a wideblock.],
290+
caption: [A figure in a double-wide block.],
262291
)
263292
]
264293

0 commit comments

Comments
 (0)