| 1 |
/* |
| 2 |
https://github.com/tudborg/notes.typ/raw/main/notes.typ |
| 3 |
Keep track of a running note counter, and associated notes. |
| 4 |
*/ |
| 5 |
|
| 6 |
#let note_state_prefix = "notes-" |
| 7 |
#let note_default_group = "default" |
| 8 |
|
| 9 |
#let note_default_display_fn(note) = { |
| 10 |
h(0pt, weak: true) |
| 11 |
super([[#note.index]]) |
| 12 |
} |
| 13 |
|
| 14 |
#let add_note( |
| 15 |
// The location of the note. This is used to derive |
| 16 |
// what the note counter should be for this note. |
| 17 |
loc, |
| 18 |
// The note itself. |
| 19 |
text, |
| 20 |
// The offset which will be added to the 0-based counter index |
| 21 |
// when the index stored in the state. |
| 22 |
offset: 1, |
| 23 |
// the display function that creates the returned content from put. |
| 24 |
// Put can't return a pure index number because the counter |
| 25 |
// and state updates need to output content. |
| 26 |
display: note_default_display_fn, |
| 27 |
// The state group to track notes in. |
| 28 |
// A group acts independent (both counter and set of notes) |
| 29 |
// from other groups. |
| 30 |
group: note_default_group |
| 31 |
) = { |
| 32 |
let s = state(note_state_prefix + group, ()) |
| 33 |
let c = counter(note_state_prefix + group) |
| 34 |
// find any existing note that hasn't been printed yet, |
| 35 |
// containing the exact same text: |
| 36 |
let existing = s.at(loc).filter((n) => n.text == text) |
| 37 |
// If we found an existing note use that, |
| 38 |
// otherwise the note is the current location's counter + offset |
| 39 |
// and the given text (the counter is 0-based, we want 1-based indices) |
| 40 |
let note = if existing.len() > 0 { |
| 41 |
existing.first() |
| 42 |
} else { |
| 43 |
(text: text, index: c.at(loc).first() + offset, page: loc.page()) |
| 44 |
} |
| 45 |
|
| 46 |
// If we didn't find an existing index, increment the counter |
| 47 |
// and add the note to the "notes" state. |
| 48 |
if existing.len() == 0 { |
| 49 |
c.step() |
| 50 |
s.update(notes => notes + (note,)) |
| 51 |
} |
| 52 |
|
| 53 |
// Output the note marker |
| 54 |
display(note) |
| 55 |
} |
| 56 |
|
| 57 |
// get notes at specific location |
| 58 |
#let get_notes(loc, group: note_default_group) = { |
| 59 |
state(note_state_prefix + group, ()).at(loc) |
| 60 |
} |
| 61 |
|
| 62 |
// Reset the note group to empty. |
| 63 |
// Note: The counter does not reset by default. |
| 64 |
#let reset_notes(group: note_default_group, reset_counter: false) = { |
| 65 |
if reset_counter { |
| 66 |
counter(note_state_prefix + group).update(0) |
| 67 |
} |
| 68 |
state(note_state_prefix + group, ()).update(()) |
| 69 |
} |
| 70 |
|
| 71 |
// |
| 72 |
// Helpers for nicer in-document ergonomics |
| 73 |
// |
| 74 |
|
| 75 |
#let render_notes(fn, group: note_default_group, reset: true, reset_counter: false) = { |
| 76 |
locate(loc => fn(get_notes(loc, group: group))) |
| 77 |
if reset { |
| 78 |
reset_notes(group: group, reset_counter: reset_counter) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// Create a new note at the current location |
| 83 |
#let note(note, ..args) = { |
| 84 |
locate((loc) => add_note(loc, note, ..args)) |
| 85 |
} |
| 86 |
|
| 87 |
// The quick-start option that outputs something useful by default. |
| 88 |
// This is a sane-defaults call to `render_notes`. |
| 89 |
#let notes( |
| 90 |
size: 8pt, |
| 91 |
font: "Roboto", |
| 92 |
line: line(length: 100%, stroke: 1pt + gray), |
| 93 |
padding: (top: 3mm), |
| 94 |
alignment: bottom, |
| 95 |
numberings: "1", |
| 96 |
group: note_default_group, |
| 97 |
reset: true, |
| 98 |
reset_counter: false |
| 99 |
) = { |
| 100 |
let render(notes) = { |
| 101 |
if notes.len() > 0 { |
| 102 |
set align(alignment) |
| 103 |
block(breakable: false, pad(..padding, { |
| 104 |
if line != none { line } |
| 105 |
set text(size: size, font: font) |
| 106 |
for note in notes { |
| 107 |
[/ #text(font: "Roboto Mono")[[#numbering(numberings, note.index)]]: #note.text] |
| 108 |
} |
| 109 |
})) |
| 110 |
} |
| 111 |
} |
| 112 |
render_notes(group: group, reset: reset, reset_counter: reset_counter, render) |
| 113 |
} |
| 114 |
|
| 115 |
// |
| 116 |
// Examples |
| 117 |
// |
| 118 |
|
| 119 |
#set page(height: 5cm, width: 15cm) |
| 120 |
|
| 121 |
= Example |
| 122 |
|
| 123 |
- Having a flexible note-keeping system is important #note[Citation needed] |
| 124 |
- It's pretty easy to implement with Typst #note[Fact] |
| 125 |
- Everyone really likes the name "Typst" #note[Citation needed] |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
// Print notes since last print: |
| 130 |
#notes() |
| 131 |
#pagebreak() |
| 132 |
|
| 133 |
These notes won't be printed on this page, so they accumulate onto next page. |
| 134 |
|
| 135 |
- Hello World #note[Your first program] |
| 136 |
- Foo Bar #note[Does not serve beverages] |
| 137 |
#pagebreak() |
| 138 |
|
| 139 |
- Orange #note[A Color] |
| 140 |
- Blue #note[A Color] |
| 141 |
// Notice that the "Citation needed" gets a new index |
| 142 |
// because we've re-used it since we printed the initial "Citation needed" |
| 143 |
- All colors are great #note[Citation needed] |
| 144 |
#notes() |
| 145 |
#pagebreak() |
| 146 |
|
| 147 |
#set page( |
| 148 |
height: 8cm, |
| 149 |
footer: notes(padding: (bottom: 5mm)), |
| 150 |
margin: (rest: 5mm, bottom: 3cm) |
| 151 |
) |
| 152 |
|
| 153 |
= Footnotes |
| 154 |
|
| 155 |
It is of course also possible to place the notes _in_ the page footer. |
| 156 |
This is the way to implement footnotes. |
| 157 |
|
| 158 |
- Black#note[Debateably a color] |
| 159 |
- White#note[Debateably a color] |
| 160 |
#pagebreak() |
| 161 |
|
| 162 |
- Orange#note[A Color] |
| 163 |
- Blue#note[A Color] |
| 164 |
- Purple#note[Also a color] |
| 165 |
|
| 166 |
#pagebreak() |
| 167 |
|
| 168 |
= Note groups |
| 169 |
|
| 170 |
This page still has footnotes (using the default note group) |
| 171 |
but we can also name a new group for custom notes. |
| 172 |
|
| 173 |
#let mynote = note.with(group: "custom") |
| 174 |
#let mynotes = notes.with( |
| 175 |
group: "custom", |
| 176 |
font: "Comic Neue", |
| 177 |
size: 12pt, |
| 178 |
numberings: "a.", |
| 179 |
line: none, |
| 180 |
alignment: right + top |
| 181 |
) |
| 182 |
|
| 183 |
- This is in it's own group#mynote[Custom] |
| 184 |
- Regular footnote here#note[Regular footnote] |
| 185 |
- Same custom note#mynote[Custom] |
| 186 |
|
| 187 |
#mynotes() |