-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathlib.typ
More file actions
425 lines (386 loc) · 12.8 KB
/
lib.typ
File metadata and controls
425 lines (386 loc) · 12.8 KB
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
// Workaround for the lack of an `std` scope.
#let std-bibliography = bibliography
#let std-smallcaps = smallcaps
#let std-upper = upper
// Overwrite the default `smallcaps` and `upper` functions with increased spacing between
// characters. Default tracking is 0pt.
#let smallcaps(body) = std-smallcaps(text(tracking: 0.6pt, body))
#let upper(body) = std-upper(text(tracking: 0.6pt, body))
// Colors used across the template.
#let stroke-color = luma(200)
#let fill-color = luma(250)
// This function gets your whole document as its `body`.
#let ilm(
// The title for your work.
title: [Your Title],
// Author's name.
// TODO: Deprecated. Use `authors` instead. Will be removed in a future version.
author: "Author",
// Author(s) of your work. Can be a string or an array of strings.
// If an array is provided, authors will be displayed on separate lines on the cover page.
authors: none,
// Cover page customization.
// Set to "use-ilm-default" to use Ilm's default cover page,
// set to `none` to skip the cover page entirely,
// or provide custom content to create your own cover page.
cover-page: "use-ilm-default",
// The paper size to use.
paper-size: "a4",
// Date that will be displayed on cover page.
// The value needs to be of the 'datetime' type.
// More info: https://typst.app/docs/reference/foundations/datetime/
// Example: datetime(year: 2024, month: 03, day: 17)
date: none,
// Format in which the date will be displayed on cover page.
// More info: https://typst.app/docs/reference/foundations/datetime/#format
// The default format will display date as: MMMM DD, YYYY
date-format: "[month repr:long] [day padding:zero], [year repr:full]",
// An abstract for your work. Can be omitted if you don't have one.
abstract: none,
// The contents for the preface page. This will be displayed after the cover page. Can
// be omitted if you don't have one.
preface: none,
// The result of a call to the `outline` function or `none`.
// Set this to `none`, if you want to disable the table of contents.
// More info: https://typst.app/docs/reference/model/outline/
table-of-contents: outline(),
// Display an appendix after the body but before the bibliography.
appendix: (
enabled: false,
title: "",
heading-numbering-format: "",
body: none,
),
// The result of a call to the `bibliography` function or `none`.
// Example: bibliography("refs.bib")
// More info: https://typst.app/docs/reference/model/bibliography/
bibliography: none,
// Whether to start a chapter on a new page.
chapter-pagebreak: true,
// Whether to display a maroon circle next to external links.
external-link-circle: true,
// Footer style for page numbering.
// Set to `none` to disable footer entirely.
// Available styles:
// - "page-number-alternate-with-chapter": alternating sides with chapter name (default)
// - "page-number-left-with-chapter": left-aligned with chapter name
// - "page-number-right-with-chapter": right-aligned with chapter name
// - "page-number-center": centered page number only
// - "page-number-left": left-aligned page number only
// - "page-number-right": right-aligned page number only
footer: "page-number-alternate-with-chapter",
// Raw text customization.
// Set to "use-typst-default" to use Typst's default raw text styling,
// or provide a dictionary to customize font and size.
raw-text: (
font: ("Iosevka", "Fira Mono"),
size: 9pt,
),
// Display an index of figures (images).
figure-index: (
enabled: false,
title: "",
),
// Display an index of tables
table-index: (
enabled: false,
title: "",
),
// Display an index of listings (code blocks).
listing-index: (
enabled: false,
title: "",
),
// The content of your work.
body,
) = {
// Determine the final authors to use (new `authors` parameter takes precedence over
// deprecated `author`). Normalize to array for simpler processing.
let final-authors = if authors != none {
if type(authors) == str {
(authors,)
} else {
authors
}
} else {
(author,)
}
// Set the document's metadata.
set document(title: title, author: final-authors.join(", "))
// Set the body font.
set text(size: 12pt) // default is 11pt
// Customize raw text formatting.
show raw: it => {
// TODO: Remove backwards compatibility for `use-typst-defaults` in future version
let use-defaults = (
(type(raw-text) == str and raw-text == "use-typst-default") or
(type(raw-text) == dictionary and raw-text.at("use-typst-defaults", default: false))
)
if use-defaults {
it
} else if type(raw-text) == dictionary {
set text(
// Reference: Typst's default is Fira Mono at 8.8pt
// TODO: Remove backwards compatibility for `custom-font` and `custom-size` in future version
font: raw-text.at("font", default: raw-text.at("custom-font", default: ("Iosevka", "Fira Mono"))),
size: raw-text.at("size", default: raw-text.at("custom-size", default: 9pt)),
)
it
} else {
it
}
}
// Configure page size and margins.
set page(
paper: paper-size,
margin: (bottom: 1.75cm, top: 2.25cm),
)
// Cover page.
if cover-page == none {
// Skip cover page
} else if type(cover-page) == content {
// Custom cover page content
page(cover-page)
} else if type(cover-page) == str and cover-page == "use-ilm-default" {
// Default Ilm cover page
page(
align(
left + horizon,
block(width: 90%)[
#let v-space = v(2em, weak: true)
#text(3em)[*#title*]
#v-space
// Display author(s)
#let author-count = final-authors.len()
#let author-size = if author-count == 1 {
1.6em
} else if author-count == 2 {
1.4em
} else if author-count == 3 {
1.2em
} else {
1.1em
}
#for (i, auth) in final-authors.enumerate() {
text(author-size, auth)
if i < author-count - 1 {
linebreak()
}
}
#if abstract != none {
v-space
block(width: 80%)[
// Default leading is 0.65em.
#par(leading: 0.78em, justify: true, linebreaks: "optimized", abstract)
]
}
#if date != none {
v-space
text(date.display(date-format))
}
],
),
)
}
// Configure paragraph properties.
// Default leading is 0.65em.
// Default spacing is 1.2em.
set par(leading: 0.7em, spacing: 1.35em, justify: true, linebreaks: "optimized")
// Add vertical space after headings.
show heading: it => {
it
v(2%, weak: true)
}
// Do not hyphenate headings.
show heading: set text(hyphenate: false)
// Show a small maroon circle next to external links.
show link: it => {
it
// Workaround for ctheorems package so that its labels keep the default link styling.
if external-link-circle and type(it.dest) != label {
sym.wj
h(1.6pt)
sym.wj
super(box(height: 3.8pt, circle(radius: 1.2pt, stroke: 0.7pt + rgb("#993333"))))
}
}
// Display preface as the second page.
if preface != none {
page(preface)
}
// Display table of contents.
if table-of-contents != none {
table-of-contents
}
// Configure page numbering and footer.
set page(
footer: if footer != none {
context {
// Get current page number.
let i = counter(page).at(here()).first()
// Only get chapter info if needed
let chapter = none
let on-chapter-page = false
if footer.ends-with("with-chapter") {
// Are we on a page that starts a chapter?
let target = heading.where(level: 1)
on-chapter-page = query(target).any(it => it.location().page() == i)
// Find the chapter of the section we are currently in.
if not on-chapter-page {
let before = query(target.before(here()))
if before.len() > 0 {
let current = before.last()
if current.numbering != none {
chapter = upper(text(size: 0.68em, current.body))
}
}
}
}
let gap = 1.75em
// Apply footer style
if footer == "page-number-alternate-with-chapter" {
let is-odd = calc.odd(i)
let aln = if is-odd { right } else { left }
if chapter != none {
if is-odd {
align(aln)[#chapter #h(gap) #i]
} else {
align(aln)[#i #h(gap) #chapter]
}
} else {
align(aln)[#i]
}
} else if footer == "page-number-left-with-chapter" {
if chapter != none {
align(left)[#i #h(gap) #chapter]
} else {
align(left)[#i]
}
} else if footer == "page-number-right-with-chapter" {
if chapter != none {
align(right)[#chapter #h(gap) #i]
} else {
align(right)[#i]
}
} else if footer == "page-number-center" {
align(center)[#i]
} else if footer == "page-number-left" {
align(left)[#i]
} else if footer == "page-number-right" {
align(right)[#i]
} else {
// Fallback to default behavior
let is-odd = calc.odd(i)
let aln = if is-odd { right } else { left }
align(aln)[#i]
}
}
},
)
// Configure equation numbering.
set math.equation(numbering: "(1)")
// Display inline code in a small box that retains the correct baseline.
show raw.where(block: false): box.with(
fill: fill-color.darken(2%),
inset: (x: 3pt, y: 0pt),
outset: (y: 3pt),
radius: 2pt,
)
// Display block code with padding.
show raw.where(block: true): block.with(inset: (x: 5pt))
// Break large tables across pages.
show figure.where(kind: table): set block(breakable: true)
set table(
// Increase the table cell's padding
inset: 7pt, // default is 5pt
stroke: (0.5pt + stroke-color),
)
// Use smallcaps for table header row.
show table.cell.where(y: 0): smallcaps
// Wrap `body` in curly braces so that it has its own context. This way show/set rules
// will only apply to body.
{
// Configure heading numbering.
set heading(numbering: "1.")
// Start chapters on a new page.
show heading.where(level: 1): it => {
if chapter-pagebreak {
pagebreak(weak: true)
}
it
}
body
}
// Display appendix before the bibliography.
if appendix.enabled {
pagebreak()
heading(level: 1)[#appendix.at("title", default: "Appendix")]
// For heading prefixes in the appendix, the standard convention is A.1.1.
let num-fmt = appendix.at("heading-numbering-format", default: "A.1.1.")
counter(heading).update(0)
set heading(
outlined: false,
numbering: (..nums) => {
let vals = nums.pos()
if vals.len() > 0 {
let v = vals.slice(0)
return numbering(num-fmt, ..v)
}
},
)
appendix.body
}
// Display bibliography.
if bibliography != none {
pagebreak()
show std-bibliography: set text(0.85em)
// Use default paragraph properties for bibliography.
show std-bibliography: set par(leading: 0.65em, justify: false, linebreaks: auto)
bibliography
}
// Display indices of figures, tables, and listings.
let fig-t(kind) = figure.where(kind: kind)
let has-fig(kind) = counter(fig-t(kind)).get().at(0) > 0
if figure-index.enabled or table-index.enabled or listing-index.enabled {
show outline: set heading(outlined: true)
context {
let imgs = figure-index.enabled and has-fig(image)
let tbls = table-index.enabled and has-fig(table)
let lsts = listing-index.enabled and has-fig(raw)
if imgs or tbls or lsts {
// Note that we pagebreak only once instead of each each individual index. This is
// because for documents that only have a couple of figures, starting each index
// on new page would result in superfluous whitespace.
pagebreak()
}
if imgs {
outline(
title: figure-index.at("title", default: "Index of Figures"),
target: fig-t(image),
)
}
if tbls {
outline(
title: table-index.at("title", default: "Index of Tables"),
target: fig-t(table),
)
}
if lsts {
outline(
title: listing-index.at("title", default: "Index of Listings"),
target: fig-t(raw),
)
}
}
}
}
// This function formats its `body` (content) into a blockquote.
#let blockquote(body) = {
block(
width: 100%,
fill: fill-color,
inset: 2em,
stroke: (y: 0.5pt + stroke-color),
body,
)
}