-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcore.typ
1645 lines (1582 loc) · 58.7 KB
/
core.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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "utils.typ"
#import "utils.typ"
#import "pdfpc.typ"
#import "components.typ"
/// ------------------------------------------------
/// Slides
/// ------------------------------------------------
#let _delayed-wrapper(body) = utils.label-it(
metadata((kind: "touying-delayed-wrapper", body: body)),
"touying-temporary-mark",
)
/// Update configurations for the presentation.
///
/// Example: `#let appendix(body) = touying-set-config((appendix: true), body)` and you can use `#show: appendix` to set the appendix for the presentation.
///
/// - `config` (dict): The new configurations for the presentation.
///
/// - `body` (content): The content of the slide.
#let touying-set-config(config, body) = utils.label-it(
metadata((
kind: "touying-set-config",
config: config,
body: body,
)),
"touying-temporary-mark",
)
/// Appendix for the presentation. The last-slide-counter will be frozen at the last slide before the appendix. It is simple wrapper for `touying-set-config`, just like `#show: touying-set-config.with((appendix: true))`.
///
/// Example: `#show: appendix`
///
/// - `body` (content): The content of the appendix.
#let appendix(body) = touying-set-config(
(appendix: true),
body,
)
/// Recall a slide by its label
///
/// Example: `#touying-recall(<recall>)` or `#touying-recall("recall")`
///
/// - `lbl` (str): The label of the slide to recall
#let touying-recall(lbl) = utils.label-it(
metadata((
kind: "touying-slide-recaller",
label: if type(lbl) == label {
str(lbl)
} else {
lbl
},
)),
"touying-temporary-mark",
)
/// Call touying slide function
#let _call-slide-fn(self, fn, body) = {
let slide-wrapper = fn(body)
assert(
utils.is-kind(slide-wrapper, "touying-slide-wrapper"),
message: "you must use `touying-slide-wrapper` in your slide function",
)
return (slide-wrapper.value.fn)(self)
}
/// Use headings to split a content block into slides
#let split-content-into-slides(self: none, recaller-map: (:), new-start: true, is-first-slide: false, body) = {
// Extract arguments
assert(type(self) == dictionary, message: "`self` must be a dictionary")
assert("slide-level" in self and type(self.slide-level) == int, message: "`self.slide-level` must be an integer")
assert("slide-fn" in self and type(self.slide-fn) == function, message: "`self.slide-fn` must be a function")
let slide-level = self.slide-level
let slide-fn = self.slide-fn
let new-section-slide-fn = self.at("new-section-slide-fn", default: none)
let new-subsection-slide-fn = self.at("new-subsection-slide-fn", default: none)
let new-subsubsection-slide-fn = self.at("new-subsubsection-slide-fn", default: none)
let new-subsubsubsection-slide-fn = self.at("new-subsubsubsection-slide-fn", default: none)
let horizontal-line-to-pagebreak = self.at("horizontal-line-to-pagebreak", default: true)
let children = if utils.is-sequence(body) {
body.children
} else {
(body,)
}
// convert all sequence to array recursively, and then flatten the array
let sequence-to-array(it) = {
if utils.is-sequence(it) {
it.children.map(sequence-to-array)
} else {
it
}
}
children = children.map(sequence-to-array).flatten()
let get-last-heading-depth(current-headings) = {
if current-headings != () {
current-headings.at(-1).depth
} else {
0
}
}
let get-last-heading-label(current-headings) = {
if current-headings != () {
if current-headings.at(-1).has("label") {
str(current-headings.at(-1).label)
}
}
}
let call-slide-fn-and-reset(self, already-slide-wrapper: false, slide-fn, current-slide-cont, recaller-map) = {
let cont = if already-slide-wrapper {
slide-fn(self)
} else {
_call-slide-fn(self, slide-fn, current-slide-cont)
}
let last-heading-label = get-last-heading-label(self.headings)
if last-heading-label != none {
recaller-map.insert(last-heading-label, cont)
}
(cont, recaller-map, (), (), true, false)
}
// The empty content list
let empty-contents = ([], [ ], parbreak(), linebreak())
// The headings that we currently have
let current-headings = ()
// Recaller map
let recaller-map = recaller-map
// The current slide we are building
let current-slide = ()
// The current slide content
let cont = none
// is new start
let is-new-start = new-start
// start part
let start-part = ()
// result
let result = ()
// Is we have a horizontal line
let horizontal-line = false
// Iterate over the children
for child in children {
// Handle horizontal-line
// split content when we have a horizontal line
if horizontal-line-to-pagebreak and horizontal-line and child not in ([—], [–], [-]) {
current-slide = utils.trim(current-slide)
(cont, recaller-map, current-headings, current-slide, new-start, is-first-slide) = call-slide-fn-and-reset(
self + (headings: current-headings, is-first-slide: is-first-slide),
slide-fn,
current-slide.sum(default: none),
recaller-map,
)
result.push(cont)
horizontal-line = false
}
// Main logic
if utils.is-kind(child, "touying-slide-wrapper") {
current-slide = utils.trim(current-slide)
if current-slide != () {
(cont, recaller-map, current-headings, current-slide, new-start, is-first-slide) = call-slide-fn-and-reset(
self + (headings: current-headings, is-first-slide: is-first-slide),
slide-fn,
current-slide.sum(default: none),
recaller-map,
)
result.push(cont)
}
(cont, recaller-map, current-headings, current-slide, new-start, is-first-slide) = call-slide-fn-and-reset(
self + (headings: current-headings, is-first-slide: is-first-slide),
already-slide-wrapper: true,
child.value.fn,
none,
recaller-map,
)
if child.has("label") and child.label != <touying-temporary-mark> {
recaller-map.insert(str(child.label), cont)
}
result.push(cont)
} else if utils.is-kind(child, "touying-slide-recaller") {
current-slide = utils.trim(current-slide)
if current-slide != () or current-headings != () {
(cont, recaller-map, current-headings, current-slide, new-start, is-first-slide) = call-slide-fn-and-reset(
self + (headings: current-headings, is-first-slide: is-first-slide),
slide-fn,
current-slide.sum(default: none),
recaller-map,
)
result.push(cont)
}
let lbl = child.value.label
assert(lbl in recaller-map, message: "label not found in the recaller map for slides")
// recall the slide
result.push(recaller-map.at(lbl))
} else if child == pagebreak() {
// split content when we have a pagebreak
current-slide = utils.trim(current-slide)
(cont, recaller-map, current-headings, current-slide, new-start, is-first-slide) = call-slide-fn-and-reset(
self + (headings: current-headings, is-first-slide: is-first-slide),
slide-fn,
current-slide.sum(default: none),
recaller-map,
)
result.push(cont)
} else if horizontal-line-to-pagebreak and child == [—] {
horizontal-line = true
continue
} else if horizontal-line-to-pagebreak and horizontal-line and child in ([–], [-]) {
continue
} else if utils.is-heading(child, depth: slide-level) {
let last-heading-depth = get-last-heading-depth(current-headings)
current-slide = utils.trim(current-slide)
if child.depth <= last-heading-depth or current-slide != () or (
child.depth == 1 and new-section-slide-fn != none
) or (child.depth == 2 and new-subsection-slide-fn != none) or (
child.depth == 3 and new-subsubsection-slide-fn != none
) or (child.depth == 4 and new-subsubsubsection-slide-fn != none) {
current-slide = utils.trim(current-slide)
if current-slide != () or current-headings != () {
(cont, recaller-map, current-headings, current-slide, new-start, is-first-slide) = call-slide-fn-and-reset(
self + (headings: current-headings, is-first-slide: is-first-slide),
slide-fn,
current-slide.sum(default: none),
recaller-map,
)
result.push(cont)
}
}
current-headings.push(child)
new-start = true
if not child.has("label") or str(child.label) != "touying:hidden" {
if child.depth == 1 and new-section-slide-fn != none {
(cont, recaller-map, current-headings, current-slide, new-start, is-first-slide) = call-slide-fn-and-reset(
self + (headings: current-headings, is-first-slide: is-first-slide),
new-section-slide-fn,
child.body,
recaller-map,
)
result.push(cont)
} else if child.depth == 2 and new-subsection-slide-fn != none {
(cont, recaller-map, current-headings, current-slide, new-start, is-first-slide) = call-slide-fn-and-reset(
self + (headings: current-headings, is-first-slide: is-first-slide),
new-subsection-slide-fn,
child.body,
recaller-map,
)
result.push(cont)
} else if child.depth == 3 and new-subsubsection-slide-fn != none {
(cont, recaller-map, current-headings, current-slide, new-start, is-first-slide) = call-slide-fn-and-reset(
self + (headings: current-headings, is-first-slide: is-first-slide),
new-subsubsection-slide-fn,
child.body,
recaller-map,
)
result.push(cont)
} else if child.depth == 4 and new-subsubsubsection-slide-fn != none {
(cont, recaller-map, current-headings, current-slide, new-start, is-first-slide) = call-slide-fn-and-reset(
self + (headings: current-headings, is-first-slide: is-first-slide),
new-subsubsubsection-slide-fn,
child.body,
recaller-map,
)
result.push(cont)
}
}
} else if self.at("auto-offset-for-heading", default: true) and utils.is-heading(child) {
let fields = child.fields()
let lbl = fields.remove("label", default: none)
let _ = fields.remove("body", default: none)
fields.offset = 0
let new-heading = if lbl != none {
utils.label-it(heading(..fields, child.body), child.label)
} else {
heading(..fields, child.body)
}
if new-start {
current-slide.push(new-heading)
} else {
start-part.push(new-heading)
}
} else if utils.is-kind(child, "touying-set-config") {
current-slide = utils.trim(current-slide)
if current-slide != () or current-headings != () {
(cont, recaller-map, current-headings, current-slide, new-start, is-first-slide) = call-slide-fn-and-reset(
self + (headings: current-headings, is-first-slide: is-first-slide),
slide-fn,
current-slide.sum(default: none),
recaller-map,
)
result.push(cont)
}
// Appendix content
result.push(
split-content-into-slides(
self: utils.merge-dicts(self, child.value.config),
recaller-map: recaller-map,
new-start: true,
child.value.body,
),
)
} else if is-first-slide and utils.is-styled(child) {
current-slide = utils.trim(current-slide)
if current-slide != () or current-headings != () {
(cont, recaller-map, current-headings, current-slide, new-start, is-first-slide) = call-slide-fn-and-reset(
self + (headings: current-headings, is-first-slide: is-first-slide),
slide-fn,
current-slide.sum(default: none),
recaller-map,
)
result.push(cont)
}
result.push(
utils.reconstruct-styled(
child,
split-content-into-slides(
self: self,
recaller-map: recaller-map,
new-start: true,
is-first-slide: is-first-slide,
child.child,
),
),
)
} else {
let child = if utils.is-styled(child) {
// Split the content into slides recursively for styled content
let (start-part, cont) = split-content-into-slides(
self: self,
recaller-map: recaller-map,
new-start: false,
child.child,
)
if start-part != none {
utils.reconstruct-styled(child, start-part)
}
_delayed-wrapper(utils.reconstruct-styled(child, cont))
} else {
child
}
if new-start {
// Add the child to the current slide
current-slide.push(child)
} else {
start-part.push(child)
}
}
}
// Handle the last slide
current-slide = utils.trim(current-slide)
if current-slide != () or current-headings != () {
(cont, recaller-map, current-headings, current-slide, new-start, is-first-slide) = call-slide-fn-and-reset(
self + (headings: current-headings, is-first-slide: is-first-slide),
slide-fn,
current-slide.sum(default: none),
recaller-map,
)
result.push(cont)
}
if is-new-start {
return result.sum(default: none)
} else {
return (start-part.sum(default: none), result.sum(default: none))
}
}
/// ------------------------------------------------
/// Slide
/// ------------------------------------------------
/// Wrapper for a function to make it can receive `self` as an argument.
/// It is useful when you want to use `self` to get current subslide index, like `uncover` and `only` functions.
///
/// Example: `#let alternatives = touying-fn-wrapper.with(utils.alternatives)`
///
/// - `fn` ((self: none, ..args) => { .. }): The function that will be called.
///
/// - `with-visible-subslides` (bool): Whether the first argument of the function is the visible subslides.
///
/// It is useful for functions like `uncover` and `only`.
///
/// Touying will automatically update the max repetitions for the slide if the function is called with visible subslides.
#let touying-fn-wrapper(fn, with-visible-subslides: false, ..args) = utils.label-it(
metadata((
kind: "touying-fn-wrapper",
fn: fn,
args: args,
with-visible-subslides: with-visible-subslides,
)),
"touying-temporary-mark",
)
/// Wrapper for a slide function to make it can receive `self` as an argument.
///
/// Notice: This function is necessary for the slide function to work in Touying.
///
/// Example:
///
/// ```typst
/// #let slide(..args) = touying-slide-wrapper(self => {
/// touying-slide(self: self, ..args)
/// })
/// ```
///
/// - `fn` (self => { .. }): The function that will be called with an argument `self`.
#let touying-slide-wrapper(fn) = utils.label-it(
metadata((
kind: "touying-slide-wrapper",
fn: fn,
)),
"touying-temporary-mark",
)
/// Uncover content after the `#pause` mark in next subslide.
#let pause = [#metadata((kind: "touying-pause"))<touying-temporary-mark>]
/// Display content after the `#meanwhile` mark meanwhile.
#let meanwhile = [#metadata((kind: "touying-meanwhile"))<touying-temporary-mark>]
/// Uncover content in some subslides. Reserved space when hidden (like `#hide()`).
///
/// Example: `uncover("2-")[abc]` will display `[abc]` if the current slide is 2 or later
///
/// - `visible-subslides` is a single integer, an array of integers,
/// or a string that specifies the visible subslides
///
/// Read [polylux book](https://polylux.dev/book/dynamic/complex.html)
///
/// The simplest extension is to use an array, such as `(1, 2, 4)` indicating that
/// slides 1, 2, and 4 are visible. This is equivalent to the string `"1, 2, 4"`.
///
/// You can also use more convenient and complex strings to specify visible slides.
///
/// For example, "-2, 4, 6-8, 10-" means slides 1, 2, 4, 6, 7, 8, 10, and slides after 10 are visible.
///
/// - `uncover-cont` is the content to display when the content is visible in the subslide.
#let uncover(visible-subslides, uncover-cont) = {
touying-fn-wrapper(utils.uncover, with-visible-subslides: true, visible-subslides, uncover-cont)
}
/// Display content in some subslides only.
/// Don't reserve space when hidden, content is completely not existing there.
///
/// - `visible-subslides` is a single integer, an array of integers,
/// or a string that specifies the visible subslides
///
/// Read [polylux book](https://polylux.dev/book/dynamic/complex.html)
///
/// The simplest extension is to use an array, such as `(1, 2, 4)` indicating that
/// slides 1, 2, and 4 are visible. This is equivalent to the string `"1, 2, 4"`.
///
/// You can also use more convenient and complex strings to specify visible slides.
///
/// For example, "-2, 4, 6-8, 10-" means slides 1, 2, 4, 6, 7, 8, 10, and slides after 10 are visible.
///
/// - `only-cont` is the content to display when the content is visible in the subslide.
#let only(visible-subslides, only-cont) = {
touying-fn-wrapper(utils.only, with-visible-subslides: true, visible-subslides, only-cont)
}
/// `#alternatives` has a couple of "cousins" that might be more convenient in some situations. The first one is `#alternatives-match` that has a name inspired by match-statements in many functional programming languages. The idea is that you give it a dictionary mapping from subslides to content:
///
/// #example(```
/// #alternatives-match((
/// "1, 3-5": [this text has the majority],
/// "2, 6": [this is shown less often]
/// ))
/// ```)
///
/// - `subslides-contents` is a dictionary mapping from subslides to content.
///
/// - `position` is the position of the content. Default is `bottom + left`.
#let alternatives-match(subslides-contents, position: bottom + left) = {
touying-fn-wrapper(utils.alternatives-match, subslides-contents, position: position)
}
/// `#alternatives` is able to show contents sequentially in subslides.
///
/// Example: `#alternatives[Ann][Bob][Christopher]` will show "Ann" in the first subslide, "Bob" in the second subslide, and "Christopher" in the third subslide.
///
/// - `start` is the starting subslide number. Default is `1`.
///
/// - `repeat-last` is a boolean indicating whether the last subslide should be repeated. Default is `true`.
#let alternatives(
start: 1,
repeat-last: true,
..args,
) = {
touying-fn-wrapper(utils.alternatives, start: start, repeat-last: repeat-last, ..args)
}
/// You can have very fine-grained control over the content depending on the current subslide by using #alternatives-fn. It accepts a function (hence the name) that maps the current subslide index to some content.
///
/// Example: `#alternatives-fn(start: 2, count: 7, subslide => { numbering("(i)", subslide) })`
///
/// - `start` is the starting subslide number. Default is `1`.
///
/// - `end` is the ending subslide number. Default is `none`.
///
/// - `count` is the number of subslides. Default is `none`.
#let alternatives-fn(
start: 1,
end: none,
count: none,
..kwargs,
fn,
) = {
touying-fn-wrapper(utils.alternatives-fn, start: start, end: end, count: count, ..kwargs, fn)
}
/// You can use this function if you want to have one piece of content that changes only slightly depending of what "case" of subslides you are in.
///
/// #example(```
/// #alternatives-cases(("1, 3", "2"), case => [
/// #set text(fill: teal) if case == 1
/// Some text
/// ])
/// ```)
///
/// - `cases` is an array of strings that specify the subslides for each case.
///
/// - `fn` is a function that maps the case to content. The argument `case` is the index of the cases array you input.
#let alternatives-cases(cases, fn, ..kwargs) = {
touying-fn-wrapper(utils.alternatives-cases, cases, fn, ..kwargs)
}
/// Speaker notes are a way to add additional information to your slides that is not visible to the audience. This can be useful for providing additional context or reminders to yourself.
///
/// Example: `#speaker-note[This is a speaker note]`
///
/// - `self` is the current context.
///
/// - `mode` is the mode of the markup text, either `typ` or `md`. Default is `typ`.
///
/// - `setting` is a function that takes the note as input and returns a processed note.
///
/// - `note` is the content of the speaker note.
#let speaker-note(mode: "typ", setting: it => it, note) = {
touying-fn-wrapper(utils.speaker-note, mode: mode, setting: setting, note)
}
/// Alert is a way to display a message to the audience. It can be used to draw attention to important information or to provide instructions.
#let alert(body) = touying-fn-wrapper(utils.alert, body)
/// Touying also provides a unique and highly useful feature—math equation animations, allowing you to conveniently use pause and meanwhile within math equations.
///
/// #example(```
/// #touying-equation(`
/// f(x) &= pause x^2 + 2x + 1 \
/// &= pause (x + 1)^2 \
/// `)
/// ```)
///
/// - `block` is a boolean indicating whether the equation is a block. Default is `true`.
///
/// - `numbering` is the numbering of the equation. Default is `none`.
///
/// - `supplement` is the supplement of the equation. Default is `auto`.
///
/// - `scope` is the scope when we use `eval()` function to evaluate the equation.
///
/// - `body` is the content of the equation. It should be a string, a raw text or a function that receives `self` as an argument and returns a string.
#let touying-equation(block: true, numbering: none, supplement: auto, scope: (:), body) = utils.label-it(
metadata((
kind: "touying-equation",
block: block,
numbering: numbering,
supplement: supplement,
scope: scope,
body: {
if type(body) == function {
body
} else if type(body) == str {
body
} else if type(body) == content and body.has("text") {
body.text
} else {
panic("Unsupported type: " + str(type(body)))
}
},
)),
"touying-temporary-mark",
)
/// Touying can integrate with `mitex` to display math equations.
/// You can use `#touying-mitex` to display math equations with pause and meanwhile.
///
/// #example(```
/// #touying-mitex(mitex, `
/// f(x) &= \pause x^2 + 2x + 1 \\
/// &= \pause (x + 1)^2 \\
/// `)
/// ```)
///
/// - `mitex` is the mitex function. You can import it by code like `#import "@preview/mitex:0.2.3": mitex`
///
/// - `block` is a boolean indicating whether the equation is a block. Default is `true`.
///
/// - `numbering` is the numbering of the equation. Default is `none`.
///
/// - `supplement` is the supplement of the equation. Default is `auto`.
///
/// - `body` is the content of the equation. It should be a string, a raw text or a function that receives `self` as an argument and returns a string.
#let touying-mitex(block: true, numbering: none, supplement: auto, mitex, body) = utils.label(
metadata((
kind: "touying-mitex",
block: block,
numbering: numbering,
supplement: supplement,
mitex: mitex,
body: {
if type(body) == function {
body
} else if type(body) == str {
body
} else if type(body) == content and body.has("text") {
body.text
} else {
panic("Unsupported type: " + str(type(body)))
}
},
)),
"touying-temporary-mark",
)
/// Touying reducer is a powerful tool to provide more powerful animation effects for other packages or functions.
///
/// For example, you can adds `pause` and `meanwhile` animations to cetz and fletcher packages.
///
/// Cetz: `#let cetz-canvas = touying-reducer.with(reduce: cetz.canvas, cover: cetz.draw.hide.with(bounds: true))`
///
/// Fletcher: `#let fletcher-diagram = touying-reducer.with(reduce: fletcher.diagram, cover: fletcher.hide)`
///
/// - `reduce` is the reduce function that will be called. It is usually a function that receives an array of content and returns a content it painted. Just like the `cetz.canvas` or `fletcher.diagram` function.
///
/// - `cover` is the cover function that will be called when some content is hidden. It is usually a function that receives an the argument of the content that will be hidden. Just like the `cetz.draw.hide` or `fletcher.hide` function.
///
/// - `..args` is the arguments of the reducer function.
#let touying-reducer(reduce: arr => arr.sum(), cover: arr => none, ..args) = utils.label-it(
metadata((
kind: "touying-reducer",
reduce: reduce,
cover: cover,
kwargs: args.named(),
args: args.pos(),
)),
"touying-temporary-mark",
)
// parse touying equation, and get the repetitions
#let _parse-touying-equation(self: none, need-cover: true, base: 1, index: 1, eqt-metadata) = {
let eqt = eqt-metadata.value
let result-arr = ()
// repetitions
let repetitions = base
let max-repetitions = repetitions
// get cover function from self
let cover = self.methods.cover.with(self: self)
// get eqt body
let it = eqt.body
// if it is a function, then call it with self
if type(it) == function {
it = it(self)
}
assert(type(it) == str, message: "Unsupported type: " + str(type(it)))
// parse the content
let result = ()
let cover-arr = ()
let children = it
.split(regex("(#meanwhile;?)|(meanwhile)"))
.intersperse("touying-meanwhile")
.map(s => s.split(regex("(#pause;?)|(pause)")).intersperse("touying-pause"))
.flatten()
.map(s => s.split(regex("(\\\\\\s)|(\\\\\\n)")).intersperse("\\\n"))
.flatten()
.map(s => s.split(regex("&")).intersperse("&"))
.flatten()
for child in children {
if child == "touying-pause" {
repetitions += 1
} else if child == "touying-meanwhile" {
// clear the cover-arr when encounter #meanwhile
if cover-arr.len() != 0 {
result.push("cover(" + cover-arr.sum() + ")")
cover-arr = ()
}
// then reset the repetitions
max-repetitions = calc.max(max-repetitions, repetitions)
repetitions = 1
} else if child == "\\\n" or child == "&" {
// clear the cover-arr when encounter linebreak or parbreak
if cover-arr.len() != 0 {
result.push("cover(" + cover-arr.sum() + ")")
cover-arr = ()
}
result.push(child)
} else {
if repetitions <= index or not need-cover {
result.push(child)
} else {
cover-arr.push(child)
}
}
}
// clear the cover-arr when end
if cover-arr.len() != 0 {
result.push("cover(" + cover-arr.sum() + ")")
cover-arr = ()
}
let equation = math.equation(
block: eqt.block,
numbering: eqt.numbering,
supplement: eqt.supplement,
eval(
"$" + result.sum(default: "") + "$",
scope: eqt.scope + (
cover: (..args) => {
let cover = eqt.scope.at("cover", default: cover)
if args.pos().len() != 0 {
cover(args.pos().first())
}
},
),
),
)
if eqt-metadata.has("label") and eqt-metadata.label != <touying-temporary-mark> {
equation = utils.label-it(equation, eqt-metadata.label)
}
result-arr.push(equation)
max-repetitions = calc.max(max-repetitions, repetitions)
return (result-arr, max-repetitions)
}
// parse touying mitex, and get the repetitions
#let _parse-touying-mitex(self: none, need-cover: true, base: 1, index: 1, eqt-metadata) = {
let eqt = eqt-metadata.value
let result-arr = ()
// repetitions
let repetitions = base
let max-repetitions = repetitions
// get eqt body
let it = eqt.body
// if it is a function, then call it with self
if type(it) == function {
it = it(self)
}
assert(type(it) == str, message: "Unsupported type: " + str(type(it)))
// parse the content
let result = ()
let cover-arr = ()
let children = it
.split(regex("\\\\meanwhile"))
.intersperse("touying-meanwhile")
.map(s => s.split(regex("\\\\pause")).intersperse("touying-pause"))
.flatten()
.map(s => s.split(regex("(\\\\\\\\\s)|(\\\\\\\\\n)")).intersperse("\\\\\n"))
.flatten()
.map(s => s.split(regex("&")).intersperse("&"))
.flatten()
for child in children {
if child == "touying-pause" {
repetitions += 1
} else if child == "touying-meanwhile" {
// clear the cover-arr when encounter #meanwhile
if cover-arr.len() != 0 {
result.push("\\phantom{" + cover-arr.sum() + "}")
cover-arr = ()
}
// then reset the repetitions
max-repetitions = calc.max(max-repetitions, repetitions)
repetitions = 1
} else if child == "\\\n" or child == "&" {
// clear the cover-arr when encounter linebreak or parbreak
if cover-arr.len() != 0 {
result.push("\\phantom{" + cover-arr.sum() + "}")
cover-arr = ()
}
result.push(child)
} else {
if repetitions <= index or not need-cover {
result.push(child)
} else {
cover-arr.push(child)
}
}
}
// clear the cover-arr when end
if cover-arr.len() != 0 {
result.push("\\phantom{" + cover-arr.sum() + "}")
cover-arr = ()
}
let equation = (eqt.mitex)(
block: eqt.block,
numbering: eqt.numbering,
supplement: eqt.supplement,
result.sum(default: ""),
)
if eqt-metadata.has("label") and eqt-metadata.label != <touying-temporary-mark> {
equation = utils.label-it(equation, eqt-metadata.label)
}
result-arr.push(equation)
max-repetitions = calc.max(max-repetitions, repetitions)
return (result-arr, max-repetitions)
}
// parse touying reducer, and get the repetitions
#let _parse-touying-reducer(self: none, base: 1, index: 1, reducer) = {
let result-arr = ()
// repetitions
let repetitions = base
let max-repetitions = repetitions
// get cover function from self
let cover = reducer.cover
// parse the content
let result = ()
let cover-arr = ()
for child in reducer.args.flatten() {
if type(child) == content and child.func() == metadata and type(child.value) == dictionary {
let kind = child.value.at("kind", default: none)
if kind == "touying-pause" {
repetitions += 1
} else if kind == "touying-meanwhile" {
// clear the cover-arr when encounter #meanwhile
if cover-arr.len() != 0 {
result.push(cover(cover-arr.sum()))
cover-arr = ()
}
// then reset the repetitions
max-repetitions = calc.max(max-repetitions, repetitions)
repetitions = 1
} else {
if repetitions <= index {
result.push(child)
} else {
cover-arr.push(child)
}
}
} else {
if repetitions <= index {
result.push(child)
} else {
cover-arr.push(child)
}
}
}
// clear the cover-arr when end
if cover-arr.len() != 0 {
let r = cover(cover-arr)
if type(r) == array {
result += r
} else {
result.push(r)
}
cover-arr = ()
}
result-arr.push(
(reducer.reduce)(
..reducer.kwargs,
result,
),
)
max-repetitions = calc.max(max-repetitions, repetitions)
return (result-arr, max-repetitions)
}
// parse content into results and repetitions
#let _parse-content-into-results-and-repetitions(
self: none,
need-cover: true,
base: 1,
index: 1,
show-delayed-wrapper: false,
..bodies,
) = {
let labeled(func) = {
return not (
"repeat" in self and "subslide" in self and "label-only-on-last-subslide" in self and func in self.label-only-on-last-subslide and self.subslide != self.repeat
)
}
let bodies = bodies.pos()
let result-arr = ()
// repetitions
let repetitions = base
let max-repetitions = repetitions
// get cover function from self
let cover = self.methods.cover.with(self: self)
for it in bodies {
// a hack for code like #table([A], pause, [B])
if type(it) == content and it.func() in (table.cell, grid.cell) {
if type(it.body) == content and it.body.func() == metadata and type(it.body.value) == dictionary {
let kind = it.body.value.at("kind", default: none)
if kind == "touying-pause" {
repetitions += 1
} else if kind == "touying-meanwhile" {
// reset the repetitions
max-repetitions = calc.max(max-repetitions, repetitions)
repetitions = 1
}
continue
}
}
// if it is a function, then call it with self
if type(it) == function {
// subslide index
it = it(self)
}
// parse the content
let result = ()
let cover-arr = ()
let children = if utils.is-sequence(it) {
it.children
} else {
(it,)
}
for child in children {
if type(child) == content and child.func() == metadata and type(child.value) == dictionary {
let kind = child.value.at("kind", default: none)
if kind == "touying-pause" {
repetitions += 1
} else if kind == "touying-meanwhile" {
// clear the cover-arr when encounter #meanwhile
if cover-arr.len() != 0 {
result.push(cover(cover-arr.sum()))
cover-arr = ()
}
// then reset the repetitions
max-repetitions = calc.max(max-repetitions, repetitions)
repetitions = 1
} else if kind == "touying-equation" {
// handle touying-equation
let (conts, nextrepetitions) = _parse-touying-equation(
self: self,
need-cover: repetitions <= index,
base: repetitions,
index: index,
child,
)
let cont = conts.first()
if repetitions <= index or not need-cover {
result.push(cont)
} else {
cover-arr.push(cont)
}
repetitions = nextrepetitions
} else if kind == "touying-mitex" {
// handle touying-mitex
let (conts, nextrepetitions) = _parse-touying-mitex(
self: self,
need-cover: repetitions <= index,
base: repetitions,
index: index,
child,
)
let cont = conts.first()
if repetitions <= index or not need-cover {
result.push(cont)
} else {
cover-arr.push(cont)
}
repetitions = nextrepetitions
} else if kind == "touying-reducer" {
// handle touying-reducer
let (conts, nextrepetitions) = _parse-touying-reducer(
self: self,
base: repetitions,
index: index,
child.value,
)
let cont = conts.first()
if repetitions <= index or not need-cover {
result.push(cont)
} else {
cover-arr.push(cont)
}
repetitions = nextrepetitions
} else if kind == "touying-fn-wrapper" {
// handle touying-fn-wrapper
if repetitions <= index or not need-cover {
result.push((child.value.fn)(self: self, ..child.value.args))
} else {
cover-arr.push((child.value.fn)(self: self, ..child.value.args))