-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.typ
713 lines (665 loc) · 21.6 KB
/
lib.typ
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
// clean-math-presentation theme
// Authors: Coekjan, QuadnucYard, OrangeX4, JoshuaLampert
// Inspired by https://github.com/Coekjan/touying-buaa and
// https://github.com/QuadnucYard/touying-theme-seu and
// https://github.com/typst/packages/blob/main/packages/preview/touying/0.5.3/themes/stargazer.typ
#import "@preview/touying:0.5.5": *
#import "@preview/great-theorems:0.1.1": *
// Custom colors
#let primary-color = rgb("#005bac")
#let secondary-color = rgb("#004078")
#let tertiary-color = rgb("#972828")
// align
#let _typst-builtin-align = align
// Theorems configuration by great-theorems (only for proofs) and custom block
#let proof = proofblock()
#let _tblock(self: none, blocktitle: none, title: none, it) = {
title = {
if title != none and blocktitle != none {
blocktitle + " (" + title + ")"
} else if blocktitle != none {
blocktitle
} else {
title
}
}
grid(
columns: 1,
row-gutter: 0pt,
block(
fill: self.colors.primary,
width: 100%,
radius: (top: 6pt),
inset: (top: 0.4em, bottom: 0.3em, left: 0.5em, right: 0.5em),
text(fill: self.colors.neutral-lightest, weight: "bold", title),
),
rect(
fill: gradient.linear(self.colors.primary, self.colors.primary.lighten(90%), angle: 90deg),
width: 100%,
height: 4pt,
),
block(
fill: self.colors.primary.lighten(90%),
width: 100%,
radius: (bottom: 6pt),
inset: (top: 0.4em, bottom: 0.5em, left: 0.5em, right: 0.5em),
it,
),
)
}
/// General theorem block for the presentation.
///
/// - `title` is the title of the theorem. Default is `none`.
///
/// - `blocktitle` is the title of the block (e.g. "Definition", "Theorem" etc...) Default is `none`.
///
/// - `it` is the content of the block.
#let tblock(title: none, blocktitle: none, it) = touying-fn-wrapper(_tblock.with(title: title, blocktitle: blocktitle, it))
/// Definition block for the presentation.
///
/// - `title` is the title of the definition. Default is `none`.
///
/// - `it` is the content of the definition.
#let definition = tblock.with(blocktitle: "Definition")
/// Theorem block for the presentation.
///
/// - `title` is the title of the theorem. Default is `none`.
///
/// - `it` is the content of the theorem.
#let theorem = tblock.with(blocktitle: "Theorem")
/// Lemma block for the presentation.
///
/// - `title` is the title of the lemma. Default is `none`.
///
/// - `it` is the content of the lemma.
#let lemma = tblock.with(blocktitle: "Lemma")
/// Corollary block for the presentation.
///
/// - `title` is the title of the corollary. Default is `none`.
///
/// - `it` is the content of the corollary.
#let corollary = tblock.with(blocktitle: "Corollary")
/// Example block for the presentation.
///
/// - `title` is the title of the example. Default is `none`.
///
/// - `it` is the content of the example.
#let example = tblock.with(blocktitle: "Example")
/// Mathgrid to align multiple terms in a multi-line block equation.
///
/// - `align` is the alignment of the content. This can either be an alignment or an array of alignments for each column. Default is `center`.
///
/// - `gutter` is the space between the columns. Default is `1em`.
///
/// - `eq` is the content of the mathgrid.
///
/// See https://forum.typst.app/t/how-can-i-align-multiple-terms-in-a-multi-line-block-equation/1608/4
#let mgrid(align: center, gutter: 1em, eq) = context {
if eq.func() != [].func() {
// Body is just a single element, so leave it as is.
return eq
}
// Split body at linebreaks and alignment points.
let lines = eq.children.split(linebreak()).map(line => line.split($&$.body).map(array.join))
// Calculate width of each column.
let widths = ()
for line in lines {
for (i, part) in line.enumerate() {
let width = measure(math.equation(block: true, numbering: none, part)).width
if i >= widths.len() {
widths.push(width)
} else {
widths.at(i) = calc.max(widths.at(i), width)
}
}
}
// Resolve alignment for each column.
let aligns = range(widths.len()).map(i => {
if type(align) == alignment { align }
else if type(align) == array { align.at(calc.rem(i, align.len())) }
else { panic("expected alignment or array as 'align'") }
})
// Try to flatten sequence elements (to allow access to an underlying align
// element for overriding the alignment of single parts).
let flatten(seq) = {
if type(seq) != content or seq.func() != [].func() {
return seq
}
let children = seq.children.filter(c => c != [ ])
if children.len() == 1 { children.first() } else { seq }
}
// Display parts centered in each column and add gutter.
let layout-line(line) = {
if line.len() < widths.len() {
line += (none,) * (widths.len() - line.len())
}
line.zip(widths, aligns).map(((part, width, align)) => {
let part = flatten(part)
let part-width = measure(math.equation(block: true, numbering: none, part)).width
let delta = width - part-width
// Check if alignment is overridden.
if type(part) == content and part.func() == std.align {
align = part.alignment.x
}
let (start, end) = if align == center {( h(delta/2), h(delta/2) )}
else if align == left {( none, h(delta) )}
else if align == right {( h(delta), none )}
start + part + end
}).join(h(gutter))
}
lines.map(layout-line).join(linebreak())
}
/// Default slide function for the presentation.
///
/// - `title` is the title of the slide. Default is `auto`.
///
/// - `config` is the configuration of the slide. You can use `config-xxx` to set the configuration of the slide. For more several configurations, you can use `utils.merge-dicts` to merge them.
///
/// - `repeat` is the number of subslides. Default is `auto`,which means touying will automatically calculate the number of subslides.
///
/// The `repeat` argument is necessary when you use `#slide(repeat: 3, self => [ .. ])` style code to create a slide. The callback-style `uncover` and `only` cannot be detected by touying automatically.
///
/// - `setting` is the setting of the slide. You can use it to add some set/show rules for the slide.
///
/// - `composer` is the composer of the slide. You can use it to set the layout of the slide.
///
/// For example, `#slide(composer: (1fr, 2fr, 1fr))[A][B][C]` to split the slide into three parts. The first and the last parts will take 1/4 of the slide, and the second part will take 1/2 of the slide.
///
/// If you pass a non-function value like `(1fr, 2fr, 1fr)`, it will be assumed to be the first argument of the `components.side-by-side` function.
///
/// The `components.side-by-side` function is a simple wrapper of the `grid` function. It means you can use the `grid.cell(colspan: 2, ..)` to make the cell take 2 columns.
///
/// For example, `#slide(composer: 2)[A][B][#grid.cell(colspan: 2)[Footer]]` will make the `Footer` cell take 2 columns.
///
/// If you want to customize the composer, you can pass a function to the `composer` argument. The function should receive the contents of the slide and return the content of the slide, like `#slide(composer: grid.with(columns: 2))[A][B]`.
///
/// - `..bodies` is the contents of the slide. You can call the `slide` function with syntax like `#slide[A][B][C]` to create a slide.
#let slide(
title: auto,
header: auto,
footer: auto,
align: auto,
config: (:),
repeat: auto,
setting: body => body,
composer: auto,
..bodies,
) = touying-slide-wrapper(self => {
if align != auto {
self.store.align = align
}
// restore typst builtin align function
let align = _typst-builtin-align
if title != auto {
self.store.title = title
}
if header != auto {
self.store.header = header
}
if footer != auto {
self.store.footer = footer
}
let self = utils.merge-dicts(
self,
config-page(fill: self.colors.neutral-lightest),
)
let new-setting = body => {
show: align.with(self.store.align)
set text(fill: self.colors.neutral-darkest)
show: setting
body
}
touying-slide(self: self, config: config, repeat: repeat, setting: new-setting, composer: composer, ..bodies)
})
// To also handle content (e.g. something like $dagger$) as affiliation-id,
// cf. https://github.com/typst/typst/issues/2196#issuecomment-1728135476
#let to-string(content) = {
if type(content) in (int, float, decimal, version, bytes, label, type, str) {
str(content)
} else {
if content.has("text") {
content.text
} else if content.has("children") {
content.children.map(to-string).join("")
} else if content.has("body") {
to-string(content.body)
} else if content == [ ] {
" "
}
}
}
/// Title slide for the presentation. You should update the information in the `config-info` function. You can also pass the information directly to the `title-slide` function.
///
/// - `logo1` is the first logo of the slide. Can be an image. Default is `none`.
///
/// - `logo2` is the second logo of the slide. Can be an image. Default is `none`.
///
/// - `background` is the background image of the slide. Can be an image. Default is `none`.
///
/// - `margin` is the margin of the slide. Default is `(top: 1.0em, bottom: 0.0em)`.
///
/// Example:
///
/// ```typst
/// #show: clean-math-presentation-theme.with(
/// config-info(
/// title: [Title],
/// ),
/// )
///
/// #title-slide(subtitle: [Subtitle])
/// ```
#let title-slide(
logo1: none,
logo2: none,
background: none,
margin: (top: 1.0em, bottom: 0.0em),
..args) = touying-slide-wrapper(self => {
set page(background: background)
self.store.title = none
let info = self.info + args.named()
info.authors = {
let authors = if "authors" in info {
info.authors
} else {
info.author
}
let authors = if type(authors) == array {
authors
} else {
(authors,)
}
authors.map(author => if author.name == info.author {
if "affiliation-id" in author {
(name: underline(author.name), affiliation-id: author.affiliation-id)
} else {
(name: underline(author.name),)
}
} else {
author
})
}
let body = {
show: align.with(center + horizon)
// title
block(
fill: self.colors.primary,
inset: 1.4em,
radius: 0.5em,
breakable: false,
{
text(size: 1.5em, fill: self.colors.neutral-lightest, weight: "bold", info.title)
if info.subtitle != none {
parbreak()
text(size: 1.0em, fill: self.colors.neutral-lightest, weight: "bold", info.subtitle)
}
},
)
// authors
info.authors.map(author => if "affiliation-id" in author {
text(author.name + super(to-string(author.affiliation-id)))
} else {
author.name
}).join(", ")
// institutions
if info.affiliations != none {
parbreak()
for affiliation in info.affiliations {
text(size: 0.8em, super(to-string(affiliation.id)) + affiliation.name)
linebreak()
}
}
// date
if info.date != none {
parbreak()
text(size: 1.0em, utils.display-info-date(self))
}
if logo1 != none {
logo1
}
if logo2 != none {
logo2
}
}
self = utils.merge-dicts(
self,
config-page(
header: none,
footer: none,
margin: margin,
),
)
touying-slide(self: self, body)
})
/// Outline slide for the presentation.
///
/// - `title` is the title of the outline. Default is `utils.i18n-outline-title`.
///
/// - `level` is the level of the outline. Default is `none`.
///
/// - `numbered` is whether the outline is numbered. Default is `true`.
#let outline-slide(
title: utils.i18n-outline-title,
numbered: true,
level: none,
..args,
) = touying-slide-wrapper(self => {
self.store.title = title
self = utils.merge-dicts(
self,
config-page(fill: self.colors.neutral-lightest),
)
touying-slide(
self: self,
align(
self.store.align,
components.adaptive-columns(
text(
fill: self.colors.primary,
weight: "bold",
components.custom-progressive-outline(
level: level,
alpha: self.store.alpha,
indent: (0em, 1em),
vspace: (.4em,),
numbered: (numbered,),
depth: 1,
..args.named(),
),
),
) + args.pos().sum(default: none),
),
)
})
/// New section slide for the presentation. You can update it by updating the `new-section-slide-fn` argument for `config-common` function.
///
/// Example: `config-common(new-section-slide-fn: new-section-slide.with(numbered: false))`
///
/// - `title` is the title of the section. The default is `utils.i18n-outline-title`.
///
/// - `level` is the level of the heading. The default is `1`.
///
/// - `numbered` is whether the heading is numbered. The default is `true`.
///
/// - `body` is the body of the section. It will be pass by touying automatically.
#let new-section-slide(
title: utils.i18n-outline-title,
level: 1,
numbered: true,
..args,
body,
) = outline-slide(title: title, level: level, numbered: numbered, ..args, body)
/// Focus on some content.
///
/// Example: `#focus-slide[Wake up!]`
///
/// - `align` is the alignment of the content. Default is `horizon + center`.
#let focus-slide(align: horizon + center, body) = touying-slide-wrapper(self => {
self = utils.merge-dicts(
self,
config-common(freeze-slide-counter: true),
config-page(
fill: self.colors.primary,
margin: 2em,
header: none,
footer: none,
),
)
set text(fill: self.colors.neutral-lightest, weight: "bold", size: 1.5em)
touying-slide(self: self, _typst-builtin-align(align, body))
})
/// End slide for the presentation.
///
/// - `title` is the title of the slide. Default is `none`.
///
/// - `body` is the content of the slide.
#let ending-slide(title: none, body) = touying-slide-wrapper(self => {
let content = {
set align(center + horizon)
if title != none {
block(
fill: self.colors.tertiary,
inset: (top: 0.7em, bottom: 0.7em, left: 3em, right: 3em),
radius: 0.5em,
text(size: 1.5em, fill: self.colors.neutral-lightest, title),
)
}
body
}
touying-slide(self: self, content)
})
/// Touying clean-math-presentation theme.
///
/// Example:
///
/// ```typst
/// #show: clean-math-presentation-theme.with(aspect-ratio: "16-9", config-colors(primary: blue))`
/// ```
///
/// Consider using:
///
/// ```typst
/// #set text(font: "Fira Sans", weight: "light", size: 20pt)`
/// #show math.equation: set text(font: "Fira Math")
/// #set strong(delta: 100)
/// #set par(justify: true)
/// ```
///
/// - `aspect-ratio` is the aspect ratio of the slides. Default is `16-9`.
///
/// - `align` is the alignment of the content. Default is `top`.
///
/// - `title` is the title in header of the slide. Default is `self => utils.display-current-heading(depth: self.slide-level)`.
///
/// - `header-right` is the right part of the header. Default is `self => self.info.logo`.
///
/// - `footer` is the footer of the slide. Default is `none`.
///
/// - `footer-right` is the right part of the footer. Default is `context utils.slide-counter.display() + " / " + utils.last-slide-number`.
///
/// - `progress-bar` is whether to show the progress bar in the footer. Default is `false`.
///
/// - `footer-columns` is the columns of the footer. Default is `(25%, 50%, 15%, 10%)`.
///
/// - `footer-a` is the left part of the footer. Default is `self => self.info.author`.
///
/// - `footer-b` is the second left part of the footer. Default is `self => if self.info.short-title == auto { self.info.title } else { self.info.short-title }`.
///
/// - `footer-c` is the second right part of the footer. Default is `self => utils.display-info-date(self)`.
///
/// - `footer-d` is the right part of the footer. Default is `context utils.slide-counter.display() + " / " + utils.last-slide-number`.
///
/// ----------------------------------------
///
/// The default colors:
///
/// ```typ
/// config-colors(
/// primary: rgb("#005bac"),
/// secondary: rgb("#004078"),
/// tertiary: rgb("#972828"),
/// neutral-lightest: rgb("#ffffff"),
/// neutral-darkest: rgb("#000000"),
/// )
/// ```
#let clean-math-presentation-theme(
aspect-ratio: "16-9",
align: top + left,
alpha: 20%,
title: self => utils.display-current-heading(depth: self.slide-level),
header-right: self => self.info.logo,
progress-bar: false,
footer-columns: (25%, 50%, 15%, 10%),
footer-a: self => self.info.author,
footer-b: self => if self.info.short-title == auto {
self.info.title
} else {
self.info.short-title
},
footer-c: self => utils.display-info-date(self),
footer-d: context utils.slide-counter.display() + " / " + utils.last-slide-number,
..args,
body,
) = {
let header(self) = {
set _typst-builtin-align(top)
grid(
rows: (auto, auto),
utils.call-or-display(self, self.store.navigation),
utils.call-or-display(self, self.store.header),
)
}
let footer(self) = {
set text(size: 0.6em)
set _typst-builtin-align(center + bottom)
grid(
rows: (auto, auto),
utils.call-or-display(self, self.store.footer),
if self.store.progress-bar {
utils.call-or-display(
self,
components.progress-bar(height: 2pt, self.colors.primary, self.colors.neutral-lightest),
)
},
)
}
show: touying-slides.with(
config-page(
paper: "presentation-" + aspect-ratio,
header: header,
footer: footer,
header-ascent: 0em,
footer-descent: 0em,
margin: (top: 3.5em, bottom: 1.0em, x: 2.5em),
),
config-info(
affiliations: none,
),
config-common(
slide-fn: slide,
datetime-format: "[month repr:long] [day], [year]",
),
config-methods(
init: (self: none, body) => {
set document(title: self.info.title)
set text(size: 20pt)
set list(marker: (text([‣], fill: self.colors.primary),
text([--], fill: self.colors.primary)))
show figure.caption: set text(size: 0.8em)
show footnote.entry: set text(size: 0.6em)
show heading: set text(fill: self.colors.primary)
show link: it => if type(it.dest) == str {
set text(fill: self.colors.primary)
it
} else {
it
}
show figure.where(kind: table): set figure.caption(position: top)
show figure.caption: c => {
text(fill: self.colors.primary, weight: "bold")[
#{
c.supplement
}]
c.separator
c.body
}
show: great-theorems-init
show math.equation: box // no line breaks in inline math
// Only number equations if they have a label
show math.equation:it => {
if it.has("label"){
math.equation(block:true, numbering: "(1)", it)
} else {
it
}
}
show ref: it => {
let el = it.element
if el != none and el.func() == math.equation {
link(el.location(), numbering("(1)",
counter(math.equation).at(el.location()).at(0) + 1
))
} else {
it
}
}
body
},
alert: utils.alert-with-primary-color,
tblock: _tblock,
),
config-colors(
primary: primary-color,
secondary: secondary-color,
tertiary: tertiary-color,
neutral-lightest: rgb("#ffffff"),
neutral-darkest: rgb("#000000"),
),
// save the variables for later use
config-store(
align: align,
alpha: alpha,
title: title,
header-right: header-right,
progress-bar: progress-bar,
footer-columns: footer-columns,
footer-a: footer-a,
footer-b: footer-b,
footer-c: footer-c,
footer-d: footer-d,
navigation: self => {
context {
let current-heading = utils.current-heading(level: 1)
let current-heading-name = if current-heading != none {
current-heading.body
} else {
""
}
grid(
columns: (1fr, 1fr),
block(
width: 100%,
height: 0.8em,
fill: self.colors.secondary,
place(right + horizon, text(current-heading-name, fill: self.colors.neutral-lightest, size: 0.7em), dx: -0.3em),
),
block(
width: 100%,
height: 0.8em,
fill: self.colors.primary,
)
)
}
},
header: self => if self.store.title != none {
block(
width: 100%,
height: 2.2em,
fill: gradient.linear(self.colors.primary, self.colors.secondary),
place(left + horizon, text(fill: self.colors.neutral-lightest, weight: "bold", size: 1.2em, utils.call-or-display(self, self.store.title)), dx: 1.0em),
)
},
footer: self => {
let cell(fill: none, it) = rect(
width: 100%,
height: 100%,
inset: 1mm,
outset: 0mm,
fill: fill,
stroke: none,
_typst-builtin-align(horizon, text(fill: self.colors.neutral-lightest, it)),
)
grid(
columns: self.store.footer-columns,
rows: (1.5em, auto),
cell(fill: self.colors.primary, utils.call-or-display(self, self.store.footer-a)),
cell(fill: self.colors.primary, utils.call-or-display(self, self.store.footer-b)),
cell(fill: self.colors.primary, utils.call-or-display(self, self.store.footer-c)),
cell(fill: self.colors.primary, utils.call-or-display(self, self.store.footer-d)),
)
}
),
..args,
)
body
}