-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.typ
More file actions
839 lines (787 loc) · 34.4 KB
/
Copy pathlib.typ
File metadata and controls
839 lines (787 loc) · 34.4 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
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
// lib.typ — 琉璃 LiuLi · 中英双语教材主题 / A Bilingual Textbook Theme for Typst
// 实色标头如琉璃瓦,六色同窑;暗色代码块如釉面。Tested with Typst 0.14.
// 用法见 example.typ 与 README.md。
// 拼音排序表:索引与术语表在 lang: "zh" 下默认按拼音排序,pinyin-key 亦一并导出
#import "pinyin.typ": *
// ================= 调色板(可整体修改) =================
#let tb-colors = (
ink: rgb("#23252e"),
accent: rgb("#3d5aa8"),
definition: rgb("#3d5aa8"),
theorem: rgb("#6b4fa8"),
lemma: rgb("#6b4fa8"),
corollary: rgb("#6b4fa8"),
proposition: rgb("#6b4fa8"),
example: rgb("#2e7d74"),
exercise: rgb("#b07118"),
note: rgb("#3d7a99"),
warning: rgb("#b04438"),
algorithm: rgb("#4a5568"),
overview: rgb("#3d5aa8"),
intuition: rgb("#2e7d74"),
pitfall: rgb("#b04438"),
)
// 字体回退链:装了哪个用哪个(安装建议见 README)
#let tb-fonts = (
serif: ("Libertinus Serif", "Source Han Serif SC", "Noto Serif CJK SC", "Noto Serif SC", "SimSun"),
sans: ("Libertinus Sans", "Source Han Sans SC", "Noto Sans CJK SC", "Noto Sans SC", "SimHei"),
kai: ("LXGW WenKai", "KaiTi", "STKaiti", "Kaiti SC"),
mono: ("Hasklig", "DejaVu Sans Mono", "Noto Sans Mono CJK SC"),
)
// ================= 多语言 =================
#let tb-strings = (
zh: (
definition: "定义", theorem: "定理", lemma: "引理", corollary: "推论",
proposition: "命题",
proof: "证明", example: "例", exercise: "习题", note: "注", warning: "警告",
algorithm: "算法", listing: "代码", figure: "图", table: "表",
section: "节", equation: "式", contents: "目录", preface: "前言",
bibliography: "参考文献", index: "索引", answers: "习题答案",
exercises: "习题", input: "输入:", output: "输出:",
chapter: "章", appendix: "附录", part: "篇",
overview: "本章概要", intuition: "直观", pitfall: "常见错误",
glossary: "术语表", notation: "符号表", acronyms: "缩略语",
see: "见", see-also: "另见",
alg-if: "若", alg-then: "则", alg-else: "否则", alg-for: "对",
alg-while: "当", alg-do: "执行", alg-return: "返回",
),
en: (
definition: "Definition", theorem: "Theorem", lemma: "Lemma", corollary: "Corollary",
proposition: "Proposition",
proof: "Proof", example: "Example", exercise: "Exercise", note: "Note", warning: "Warning",
algorithm: "Algorithm", listing: "Listing", figure: "Figure", table: "Table",
section: "Section", equation: "Eq.", contents: "Contents", preface: "Preface",
bibliography: "Bibliography", index: "Index", answers: "Answers to Exercises",
exercises: "Exercises", input: "Input: ", output: "Output: ",
chapter: "Chapter", appendix: "Appendix", part: "Part",
overview: "Chapter overview", intuition: "Intuition", pitfall: "Common mistake",
glossary: "Glossary", notation: "Notation", acronyms: "Acronyms",
see: "see", see-also: "see also",
alg-if: "if", alg-then: "then", alg-else: "else", alg-for: "for",
alg-while: "while", alg-do: "do", alg-return: "return",
),
)
// ================= 全局状态 =================
#let tb-lang = state("tb-lang", "zh")
#let tb-title = state("tb-title", "")
#let tb-appendix = state("tb-appendix", false)
#let tb-ch-en = state("tb-ch-en", none)
#let tb-solutions = state("tb-solutions", ())
#let tb-plain-raw = state("tb-plain-raw", false)
#let tb-part-ctr = counter("tb-part")
#let tb-listing-ctr = counter("tb-listing")
// 编号环境一律以 figure 实现(kind 形如 "tb:theorem"),因此可被 @label 原生引用。
// lemma / corollary / proposition 与 theorem 共用一个计数器族。
#let tb-env-group(kind) = {
if kind in ("lemma", "corollary", "proposition") { "theorem" } else { kind }
}
#let tb-env-kind(kind) = "tb:" + tb-env-group(kind)
// 需要在每章开头归零的环境族
#let tb-env-groups = ("definition", "theorem", "example", "algorithm", "exercise")
// 以下工具需在 context 中调用
#let tb-str(key) = tb-strings.at(tb-lang.get()).at(key)
// 章号:在给定位置求值(引用必须用被引对象的位置,否则会显示引用处的章号)
#let tb-ch-num-at(loc) = {
let nums = counter(heading).at(loc)
let n = if nums.len() > 0 { nums.first() } else { 0 }
if tb-appendix.at(loc) { numbering("A", n) } else { str(n) }
}
#let tb-ch-num() = tb-ch-num-at(here())
// 环境编号「章.序号」。tb-env-num-str 须在 context 中调用并返回字符串
// (存入 state 前必须先求值,否则会在书末重新求值成别的编号)
#let tb-env-num-str(kind) = {
let n = counter(figure.where(kind: tb-env-kind(kind))).get().first()
tb-ch-num() + "." + str(n)
}
#let tb-env-num(kind) = context tb-env-num-str(kind)
#let tb-folio() = {
let fmt = here().page-numbering()
let n = counter(page).get().first()
if fmt == none { str(n) } else { numbering(fmt, n) }
}
// ================= 页眉 / 页脚 =================
#let tb-page-header() = context {
let phys = here().page()
if not query(heading.where(level: 1)).any(h => h.location().page() == phys) {
set text(font: tb-fonts.sans, size: 8pt, tracking: 0.6pt, fill: luma(30%))
let row = if calc.even(counter(page).get().first()) {
block(width: 100%, { text(weight: 700, size: 9pt, tb-folio()); h(14pt); tb-title.get() })
} else {
let secs = query(heading.where(level: 2).before(here()))
align(right, {
if secs.len() > 0 {
let s = secs.last()
if s.numbering != none {
numbering(s.numbering, ..counter(heading).at(s.location()).slice(0, 2))
h(0.6em)
}
s.body
h(14pt)
}
text(weight: 700, size: 9pt, tb-folio())
})
}
stack(spacing: 6pt, row, line(length: 100%, stroke: 0.4pt + luma(75%)))
}
}
#let tb-page-footer() = context {
let phys = here().page()
let ch = query(heading.where(level: 1)).filter(h => h.location().page() == phys)
if ch.len() > 0 and ch.first().supplement != [tb-part] {
align(center, text(font: tb-fonts.sans, size: 9pt, weight: 600, fill: luma(25%), tb-folio()))
}
}
// ================= 结构:篇 / 章 / 前后件 =================
// label: 直接落在 heading 元素上。写 `#chapter[标题] <ch:x>` 会把标签挂到
// 整个 sequence(含 pagebreak)上,其位置早于章计数器自增,引用会取到错误章号。
#let part(title, en: none, label: none) = {
pagebreak(to: "odd", weak: true)
tb-ch-en.update(en)
// 篇计数器必须在 heading 之前自增:若放在 heading 的 show 规则里,其位置晚于
// heading 自身,按 heading 位置取值(页眉、引用)会拿到自增前的旧值。
tb-part-ctr.step()
[#heading(level: 1, numbering: none, outlined: true, supplement: [tb-part], title)#{
if label != none { std.label(label) }
}]
}
#let chapter(title, en: none, label: none) = {
pagebreak(to: "odd", weak: true)
tb-ch-en.update(en)
[#heading(level: 1, supplement: context {
if tb-appendix.get() { tb-str("appendix") } else { tb-str("chapter") }
}, title)#{ if label != none { std.label(label) } }]
}
#let tb-special-heading-body(body) = {
pagebreak(to: "odd", weak: true)
tb-ch-en.update(none)
heading(level: 1, numbering: none, outlined: true, body)
}
#let tb-special-heading(key) = tb-special-heading-body(context tb-str(key))
#let preface(body) = { tb-special-heading("preface"); body }
#let table-of-contents(depth: 2) = {
tb-special-heading("contents")
outline(title: none, depth: depth)
}
#let front-matter(body) = {
pagebreak(to: "odd", weak: true)
set page(numbering: "i")
counter(page).update(1)
body
}
#let main-matter(body) = {
pagebreak(to: "odd", weak: true)
set page(numbering: "1")
counter(page).update(1)
body
}
#let appendix(body) = {
counter(heading).update(0)
tb-appendix.update(true)
set heading(numbering: "A.1")
body
}
// ================= 定理类环境 =================
// 1c 风格:实色标头 + 同色细框;标头右侧为英文环境名(en 模式下省略)
#let tb-env-block(kind, body, title: none, numbered: true) = {
let c = tb-colors.at(kind)
let inner = block(width: 100%, breakable: true, above: 1.1em, below: 1.1em, stroke: 0.7pt + c, {
set par(first-line-indent: 0em)
set align(left) // figure 默认居中,环境体一律左对齐
block(width: 100%, above: 0em, below: 0em, fill: c, inset: (x: 10pt, y: 6.5pt), sticky: true,
grid(columns: (1fr, auto), align: (left + horizon, right + horizon), column-gutter: 8pt,
context {
set text(font: tb-fonts.sans, size: 0.92em, fill: white, weight: 700)
[#tb-str(kind)#if numbered [ #tb-env-num-str(kind)]]
if title != none [ · #title]
},
context {
if tb-lang.get() == "zh" {
text(font: tb-fonts.sans, size: 0.72em, tracking: 1.8pt, weight: 700,
fill: white.transparentize(28%), upper(tb-strings.en.at(kind)))
}
}))
block(width: 100%, above: 0em, below: 0em, inset: (x: 11pt, top: 9pt, bottom: 10pt), {
set text(font: tb-fonts.serif)
body
})
})
// 编号环境包一层 figure:Typst 由此原生编号并支持 @label 引用
if numbered {
figure(kind: tb-env-kind(kind), supplement: context tb-str(kind),
caption: none, placement: none, outlined: false, numbering: n => str(n), inner)
} else { inner }
}
#let definition = tb-env-block.with("definition")
#let theorem = tb-env-block.with("theorem")
#let lemma = tb-env-block.with("lemma")
#let corollary = tb-env-block.with("corollary")
#let proposition = tb-env-block.with("proposition")
#let example = tb-env-block.with("example")
#let note = tb-env-block.with("note", numbered: false)
#let warning = tb-env-block.with("warning", numbered: false)
// ================= 教学提示块(不编号) =================
#let tb-lead(key, color) = context {
text(font: tb-fonts.sans, weight: 700, fill: color, tb-str(key))
}
// 章首概要:整章的问题、内容与学习目标
#let overview(body) = block(width: 100%, above: 0.4em, below: 1.6em,
fill: luma(96%), stroke: (left: 2.5pt + tb-colors.overview), inset: (x: 12pt, y: 10pt), {
set par(first-line-indent: 0em)
tb-lead("overview", tb-colors.ink)
h(0.6em)
body
})
// 直观:正式陈述之前的非正式解释
#let intuition(body) = block(width: 100%, above: 1em, below: 1em, {
set par(first-line-indent: 0em)
tb-lead("intuition", tb-colors.intuition)
h(0.6em)
emph(body)
})
// 常见错误:先给出误解,再纠正
#let pitfall(body) = block(width: 100%, above: 1em, below: 1em,
stroke: (left: 2.5pt + tb-colors.pitfall), inset: (left: 10pt, y: 2pt), {
set par(first-line-indent: 0em)
tb-lead("pitfall", tb-colors.pitfall)
h(0.6em)
body
})
// 单个不编号的行间公式(模板默认给所有行间公式编号)
#let unnumbered(body) = {
set math.equation(numbering: none)
body
}
#let proof(body, name: none) = block(width: 100%, above: 0.9em, below: 0.9em, {
set par(first-line-indent: 0em)
context {
text(font: tb-fonts.sans, weight: 700)[#tb-str("proof")]
if name != none { text(font: tb-fonts.sans, weight: 600, fill: luma(35%))[(#name)] }
}
h(0.65em)
body
h(1fr)
[∎]
})
#let exercise(body, solution: none) = figure(
kind: tb-env-kind("exercise"), supplement: context tb-str("exercise"),
caption: none, placement: none, outlined: false, numbering: n => str(n),
block(width: 100%, above: 1em, below: 1em, {
set par(first-line-indent: 0em)
set align(left)
context {
let num = tb-env-num-str("exercise")
text(font: tb-fonts.sans, weight: 700, fill: tb-colors.exercise)[#tb-str("exercise") #num]
if solution != none { tb-solutions.update(arr => arr + ((num: num, sol: solution),)) }
}
h(0.6em)
body
}),
)
#let exercises-heading() = heading(level: 2, numbering: none, outlined: false, context tb-str("exercises"))
// 书末:自动汇总所有 exercise 的 solution
#let answers() = {
tb-special-heading("answers")
context {
set par(first-line-indent: 0em)
for e in tb-solutions.final() {
block(width: 100%, above: 0.9em, below: 0em, {
text(font: tb-fonts.sans, weight: 700, fill: tb-colors.exercise)[#tb-str("exercise") #e.num]
h(0.6em)
text(font: tb-fonts.kai, e.sol)
})
}
}
}
// ================= 算法环境(booktabs 风格) =================
#let algorithm(body, title: none, input: none, output: none) = figure(
kind: tb-env-kind("algorithm"), supplement: context tb-str("algorithm"),
caption: none, placement: none, outlined: false, numbering: n => str(n),
block(width: 100%, breakable: true, above: 1.2em, below: 1.2em,
stroke: (top: 1.4pt + tb-colors.ink, bottom: 1.4pt + tb-colors.ink),
inset: (x: 4pt, y: 0pt), {
set par(first-line-indent: 0em)
set align(left)
block(width: 100%, inset: (y: 7pt), stroke: (bottom: 0.5pt + luma(72%)), context {
set text(font: tb-fonts.sans, size: 0.95em)
text(weight: 700, fill: tb-colors.algorithm)[#tb-str("algorithm") #tb-env-num-str("algorithm")]
if title != none { text(weight: 700)[ #title] }
})
v(0.5em, weak: true)
if input != none { block(context { text(font: tb-fonts.sans, weight: 700, size: 0.92em, tb-str("input")) } + input) }
if output != none { block(context { text(font: tb-fonts.sans, weight: 700, size: 0.92em, tb-str("output")) } + output) }
block(inset: (bottom: 8pt), { set enum(numbering: "1.", tight: false, spacing: 0.7em); body })
}),
)
// 伪代码控制流:关键字随 lang 切换,嵌套结构交给内层 enum 缩进。
// + #alg-if($n <= 1$)[ + #alg-return[$n$] ]
// + #alg-for($i <- 2, ..., n$)[ + ... ]
// 每个 alg-* 渲染「关键字 条件 关键字」一行,body 作为下一层 enum 缩进其后。
#let tb-alg-kw(key) = context text(weight: 700, tb-str(key))
#let alg-if(cond, body, else-body: none) = {
[#tb-alg-kw("alg-if") #cond #tb-alg-kw("alg-then")]
body
if else-body != none { tb-alg-kw("alg-else"); else-body }
}
#let alg-for(cond, body) = { [#tb-alg-kw("alg-for") #cond #tb-alg-kw("alg-do")]; body }
#let alg-while(cond, body) = { [#tb-alg-kw("alg-while") #cond #tb-alg-kw("alg-do")]; body }
#let alg-return(body) = [#tb-alg-kw("alg-return") #body]
// ================= 代码块(1c 暗色主题) =================
#let tb-lang-badge(filename) = {
let parts = str(filename).split(".")
if parts.len() < 2 { none } else {
let m = (py: "PYTHON", c: "C", h: "C", cpp: "C++", hpp: "C++", js: "JAVASCRIPT",
ts: "TYPESCRIPT", rs: "RUST", go: "GO", java: "JAVA", sh: "SHELL", typ: "TYPST",
json: "JSON", html: "HTML", css: "CSS", sql: "SQL")
if parts.last() in m { m.at(parts.last()) } else { upper(parts.last()) }
}
}
// code:文件名标签页(顶部青色提示线)+ 暗色代码体 + 「代码 3-1」图注居中置底
#let code(body, filename: none, caption: none) = {
tb-listing-ctr.step()
block(width: 100%, breakable: true, above: 1.2em, below: 1.2em, {
set par(first-line-indent: 0em)
let rows = ()
if filename != none {
rows.push(block(width: 100%, fill: rgb("#16181d"), inset: (x: 11pt, y: 0pt),
grid(columns: (auto, 1fr), align: (left + bottom, right + horizon),
box(stroke: (top: 1.6pt + rgb("#7fd0c0")), inset: (top: 6pt, bottom: 7pt),
text(font: tb-fonts.mono, size: 8.5pt, fill: rgb("#d5d7dc"), filename)),
{
let b = tb-lang-badge(filename)
if b != none {
text(font: tb-fonts.sans, size: 7pt, tracking: 1.5pt, weight: 700, fill: rgb("#6d727c"), b)
}
})))
}
rows.push(body)
stack(..rows)
v(0.55em, weak: true)
align(center, context {
set text(font: tb-fonts.sans, size: 0.88em)
text(weight: 700, fill: tb-colors.accent)[#tb-str("listing") #tb-ch-num()-#tb-listing-ctr.get().first()]
if caption != none [ #text(weight: 600, fill: luma(30%))[#caption]]
})
})
}
#let console(body) = {
tb-plain-raw.update(true)
block(width: 100%, breakable: true, above: 1em, below: 1em,
fill: rgb("#22252e"), inset: (x: 11pt, y: 9pt), {
set text(fill: rgb("#e8e6df"), size: 8.8pt)
set par(justify: false, first-line-indent: 0em)
body
})
tb-plain-raw.update(false)
}
// 某个位置所在页码,按该处的页码格式渲染(前件为 i、ii……,正文为 1、2……)
#let tb-page-str(loc) = {
let fmt = loc.page-numbering()
let nums = counter(page).at(loc)
if fmt == none { str(nums.first()) } else { numbering(fmt, ..nums) }
}
// ================= 索引 =================
// index("矩阵") —— 一级条目
// index("矩阵!转置") —— 二级条目(用 ! 分隔,父条目无须单独标记)
// index("矩阵", sort: "juzhen") —— 排序键:中文按拼音排序即靠它给出
// index("行列式", see: "矩阵") —— 只写「见 矩阵」,不列页码
// index("矩阵", see-also: "线性映射") —— 页码之后追加「另见 …」
#let index(term, sort: auto, see: none, see-also: none) = {
[#metadata((term: term, sort: sort, see: see, see-also: see-also))#label("tb-idx")]
}
// 把同一条目的页码合并:去重、按页序排列,并把连续 range-min 页以上折成区间。
// 页码格式不同的段(前件 i/ii 与正文 1/2)分开处理,不会跨格式折叠。
#let tb-page-runs(entries, range-min, dash) = {
let out = ()
let fks = ()
for e in entries { if not fks.contains(e.fk) { fks.push(e.fk) } }
for fk in fks {
let ns = ()
let smap = (:)
for e in entries {
if e.fk != fk or ns.contains(e.n) { continue }
ns.push(e.n)
smap.insert(str(e.n), e.s)
}
ns = ns.sorted()
let i = 0
while i < ns.len() {
let j = i
while j + 1 < ns.len() and ns.at(j + 1) == ns.at(j) + 1 { j = j + 1 }
if j - i + 1 >= range-min {
out.push(smap.at(str(ns.at(i))) + dash + smap.at(str(ns.at(j))))
} else {
for k in range(i, j + 1) { out.push(smap.at(str(ns.at(k)))) }
}
i = j + 1
}
}
out
}
// 排序键函数:auto 表示中文按拼音(pinyin-key)、其余语言按词条本身;
// 传 none 强制按词条本身(Unicode 码点);也可传自己的 str => str 函数。
#let tb-sort-fn(sort-key) = {
if sort-key == none { s => s } else if sort-key != auto { sort-key } else if tb-lang.get() == "zh" {
pinyin-key
} else { s => s }
}
// range-min: 连续多少页起折成区间(默认 3,即 1,2 照列,1,2,3 折成 1–3)
#let make-index(n-cols: 2, range-min: 3, sort-key: auto) = {
tb-special-heading("index")
context {
set par(first-line-indent: 0em, justify: false)
let zh = tb-lang.get() == "zh"
let sep = if zh { "," } else { ", " }
// 收集:一级条目 -> (term, sort, pages, see, see-also, subs)
let mains = (:)
let new-node(term, sort) = (term: term, sort: sort, pages: (), see: none, see-also: none, subs: (:))
for e in query(label("tb-idx")) {
let v = e.value
let parts = if type(v.term) == str { v.term.split("!") } else { (v.term,) }
let skey = if v.sort != auto { str(v.sort) } else if type(v.term) == str { v.term } else { "" }
let sparts = skey.split("!")
let mk = if type(parts.first()) == str { parts.first() } else { repr(parts.first()) }
let msort = if sparts.len() > 0 { sparts.first() } else { mk }
if mk not in mains { mains.insert(mk, new-node(parts.first(), msort)) }
let node = mains.at(mk)
let loc = e.location()
let fmt = loc.page-numbering()
let nums = counter(page).at(loc)
let pg = (n: nums.first(), fk: repr(fmt),
s: if fmt == none { str(nums.first()) } else { numbering(fmt, ..nums) })
if parts.len() > 1 {
// 二级条目:页码与交叉引用挂在子条目上
let sk = parts.at(1)
let ssort = if sparts.len() > 1 { sparts.at(1) } else { sk }
if sk not in node.subs { node.subs.insert(sk, new-node(sk, ssort)) }
let sub = node.subs.at(sk)
if v.see != none { sub.see = v.see } else if v.see-also != none {
sub.see-also = v.see-also
} else { sub.pages.push(pg) }
node.subs.insert(sk, sub)
} else {
if v.see != none { node.see = v.see } else if v.see-also != none {
node.see-also = v.see-also
} else { node.pages.push(pg) }
}
mains.insert(mk, node)
}
let dash = "\u{2013}" // en dash
let line(node, indent) = block(above: 0.55em, below: 0em, inset: (left: indent), {
node.term
if node.see != none {
text(size: 9pt)[#sep#emph(tb-str("see")) #node.see]
} else {
let runs = tb-page-runs(node.pages, range-min, dash)
if runs.len() > 0 {
box(width: 1fr, inset: (x: 3pt), repeat(text(fill: luma(60%))[.], gap: 3pt))
text(font: tb-fonts.sans, size: 9pt, runs.join(sep))
}
if node.see-also != none {
text(size: 9pt)[#sep#emph(tb-str("see-also")) #node.see-also]
}
}
})
let keyf = tb-sort-fn(sort-key)
let by-sort(d) = d.keys().sorted(key: k => keyf(d.at(k).sort))
columns(n-cols, {
for k in by-sort(mains) {
let node = mains.at(k)
line(node, 0pt)
for sk in by-sort(node.subs) { line(node.subs.at(sk), 1.2em) }
}
})
}
}
// ================= 术语表 / 符号表 / 缩略语 =================
// gls() 与 index() 一样是不可见标记:术语本身照常写在正文里,这里只登记。
// 释义(def)只需在任意一处给出(通常是首次出现),重复标记不必再写。
// group 决定归入哪一份清单,默认三种:
// "glossary" 术语表 | "notation" 符号表 | "acronyms" 缩略语
// 也可用自定义 group,此时 make-glossary(group: …, title: …) 需自行给出标题。
// term 为数学等 content 时无法比较大小,需用 sort 显式给出排序键(中文亦可
// 借此给出拼音,见 make-index 的说明)。
#let gls(term, def: none, group: "glossary", sort: auto) = {
let key = if sort != auto { sort } else if type(term) == str { term } else { "" }
[#metadata((term: term, def: def, group: group, sort: key))#label("tb-gls")]
}
#let tb-gls-groups = ("glossary", "notation", "acronyms")
// 生成某一组的清单。pages: 是否附页码。
#let make-glossary(group: "glossary", title: auto, n-cols: 1, pages: true, sort-key: auto) = {
tb-special-heading-body(context {
if title != auto { title } else if group in tb-gls-groups { tb-str(group) } else { group }
})
context {
set par(first-line-indent: 0em, justify: false)
let sep = if tb-lang.get() == "zh" { "," } else { ", " }
// key -> (term, def, sort, pages);释义取第一个非 none 者
let d = (:)
let order = ()
for e in query(label("tb-gls")) {
let v = e.value
if v.group != group { continue }
let k = if v.sort != "" { v.sort } else { repr(v.term) }
let pg = tb-page-str(e.location())
if k in d {
let cur = d.at(k)
if cur.def == none { cur.def = v.def }
if not cur.pages.contains(pg) { cur.pages.push(pg) }
d.insert(k, cur)
} else {
order.push(k)
d.insert(k, (term: v.term, def: v.def, sort: v.sort, pages: (pg,)))
}
}
// 有排序键的按键排序;没有键的(content 术语且未给 sort)保持出现顺序,列在其后
let keyf = tb-sort-fn(sort-key)
let keyed = order.filter(k => d.at(k).sort != "").sorted(key: k => keyf(d.at(k).sort))
let rest = order.filter(k => d.at(k).sort == "")
columns(n-cols, {
for k in keyed + rest {
let e = d.at(k)
block(above: 0.55em, below: 0em, {
text(font: tb-fonts.sans, weight: 700, e.term)
if e.def != none [ #e.def]
if pages and e.pages.len() > 0 {
text(font: tb-fonts.sans, size: 9pt, fill: luma(45%))[ #e.pages.join(sep)]
}
})
}
})
}
}
// ================= 参考文献 =================
// source 接受两种形式:
// references(read("refs.bib", encoding: none)) // bytes(推荐)
// references(bibliography("refs.bib", title: none)) // 自行构造的 bibliography
// 不能传路径字符串:Typst 按「调用所在文件」解析相对路径,而这里的调用位于
// 包内的 lib.typ,因此任何用户侧路径都会解析失败。
#let references(source, style: auto, full: false) = {
tb-special-heading("bibliography")
context {
let s = if style == auto {
if tb-lang.get() == "zh" { "gb-7714-2015-numeric" } else { "ieee" }
} else { style }
if type(source) == bytes {
bibliography(source, title: none, style: s, full: full)
} else if type(source) == content {
source
} else {
panic(
"liuli: references() cannot resolve a path from inside the package "
+ "(Typst resolves it relative to lib.typ). Pass bytes instead: "
+ "references(read(\"refs.bib\", encoding: none)), or pass a "
+ "prebuilt bibliography(\"refs.bib\", title: none).",
)
}
}
}
// ================= 封面 =================
#let tb-cover(title, title-en, subtitle, author, series, publisher) = page(
margin: (x: 1.1in, top: 1.1in, bottom: 1in), header: none, footer: none, numbering: none, {
set par(first-line-indent: 0em)
set text(font: tb-fonts.sans)
if series != none {
text(size: 10.5pt, tracking: 1.5pt, fill: luma(35%), weight: 600, series)
v(6pt); line(length: 100%, stroke: 0.6pt + luma(60%))
}
v(1.5in)
stack(dir: ltr, spacing: 8pt,
..("definition", "theorem", "example", "exercise", "note", "warning")
.map(k => rect(width: 25pt, height: 25pt, fill: tb-colors.at(k))))
v(0.45in)
text(size: 33pt, weight: 900, tracking: 1.5pt, title)
if title-en != none { v(0.22in); text(size: 14pt, weight: 600, tracking: 1.2pt, fill: luma(28%), upper(title-en)) }
if subtitle != none { v(0.18in); text(font: tb-fonts.serif, size: 12.5pt, fill: luma(32%), subtitle) }
v(1fr)
text(size: 14.5pt, weight: 700, author)
if publisher != none {
v(0.3in); line(length: 100%, stroke: 0.6pt + luma(60%)); v(9pt)
text(size: 10.5pt, fill: luma(35%), publisher)
}
})
// ================= 模板主函数 =================
#let textbook(
title: "教材标题",
title-en: none,
subtitle: none,
author: "",
series: none,
publisher: none,
lang: "zh", // "zh" | "en":切换所有环境标签、章节字样与文献样式
paper: "us-letter",
cover: auto, // auto 内置封面 | none 不要封面 | 任意 content 自定义
body,
) = {
let zh = lang == "zh"
let S = tb-strings.at(lang)
set document(title: title)
tb-lang.update(lang)
tb-title.update(title)
set text(font: tb-fonts.serif, size: 10.5pt, lang: lang, region: if zh { "cn" }, fill: tb-colors.ink)
set par(justify: true, leading: 0.82em, spacing: 0.82em,
first-line-indent: (amount: 2em, all: zh))
set page(paper: paper, binding: left,
margin: (inside: 1in, outside: 0.8in, top: 0.95in, bottom: 0.95in),
header: tb-page-header(), footer: tb-page-footer())
set heading(numbering: "1.1", supplement: S.section)
set math.equation(supplement: S.equation, numbering: n => context {
let s = tb-ch-num() + "." + str(n)
if zh { "(" + s + ")" } else { "(" + s + ")" }
})
set figure(numbering: n => context { tb-ch-num() + "-" + str(n) })
show figure.where(kind: image): set figure(supplement: S.figure)
show figure.where(kind: table): set figure(supplement: S.table)
show figure.where(kind: raw): set figure(supplement: S.listing)
show figure.caption: set text(font: tb-fonts.sans, size: 9.5pt)
// 定理类环境以 figure 承载,需允许跨页断开
show figure.where(kind: "tb:definition"): set block(breakable: true)
show figure.where(kind: "tb:theorem"): set block(breakable: true)
show figure.where(kind: "tb:example"): set block(breakable: true)
show figure.where(kind: "tb:algorithm"): set block(breakable: true)
show figure.where(kind: "tb:exercise"): set block(breakable: true)
// ---- 交叉引用:编号一律在「被引对象所在位置」求值 ----
// 图/表/式的 numbering 是带 context 的闭包,而 ref 会在「引用处」渲染它,
// 于是章号会取成引用处的章号(第 1 章的图从第 2 章引用会显示成 2-1)。
show ref: it => {
let el = it.element
if el == none { return it } // 文献引用(@bibkey)走原路径
let loc = el.location()
if el.func() == heading {
if el.level == 1 {
if el.supplement == [tb-part] {
let n = tb-part-ctr.at(loc).first()
link(loc, if zh [第#numbering("一", n)#S.part] else [#S.part #numbering("I", n)])
} else if el.numbering == none {
link(loc, el.body) // 前言 / 目录 / 索引 等无编号章
} else {
let n = counter(heading).at(loc).first()
let apx = tb-appendix.at(loc)
link(loc, if zh {
if apx { [#S.appendix #numbering("A", n)] } else { [第#numbering("一", n)#S.chapter] }
} else {
if apx { [#S.appendix #numbering("A", n)] } else { [#S.chapter #n] }
})
}
} else {
let s = numbering(el.numbering, ..counter(heading).at(loc))
link(loc, if zh [第#s#S.section] else [#S.section #s])
}
} else if el.func() == figure {
let k = el.kind
let n = counter(figure.where(kind: k)).at(loc).first()
// 环境用「章.序号」,图表代码用「章-序号」
let env = type(k) == str and k.starts-with("tb:")
let num = tb-ch-num-at(loc) + (if env { "." } else { "-" }) + str(n)
let sup = if k == image { S.figure } else if k == table { S.table } else if k == raw {
S.listing
} else { el.supplement }
link(loc, [#sup #num])
} else if el.func() == math.equation {
let n = counter(math.equation).at(loc).first()
let s = tb-ch-num-at(loc) + "." + str(n)
link(loc, [#S.equation #{ if zh { "(" + s + ")" } else { "(" + s + ")" } }])
} else { it }
}
set table(stroke: 0.5pt + luma(65%), inset: 7pt)
set enum(indent: 1em)
set list(indent: 1em, marker: ([▪], [–], [·]))
set quote(block: true)
show quote: set text(font: tb-fonts.kai)
show emph: it => context { if tb-lang.get() == "zh" { text(font: tb-fonts.kai, it.body) } else { it } }
show link: set text(fill: tb-colors.accent)
// ---- 代码:暗色块 + 行号 + 内联样式(高亮配色见 dark.tmTheme) ----
set raw(tab-size: 4, theme: "dark.tmTheme")
show raw: set text(font: tb-fonts.mono)
show raw.where(block: false): set text(size: 0.92em, fill: tb-colors.ink)
show raw.where(block: false): it => box(fill: rgb("#f1efe8"), stroke: 0.5pt + rgb("#e2dfd4"),
inset: (x: 3.5pt, y: 0pt), outset: (y: 2.6pt), radius: 2pt, it)
show raw.where(block: true): it => context {
if tb-plain-raw.get() { it } else {
block(width: 100%, breakable: true, fill: rgb("#22252e"), inset: (x: 11pt, y: 9pt), {
set text(size: 8.8pt, fill: rgb("#e8e6df"))
set par(justify: false, first-line-indent: 0em, leading: 0.62em)
grid(columns: (auto, 1fr), column-gutter: 1em, row-gutter: 0.62em,
..it.lines.map(l => (align(right, text(fill: rgb("#5c626e"), size: 0.8em, str(l.number))), l)).flatten())
})
}
}
// ---- 标题 ----
show heading: set text(font: tb-fonts.sans, fill: tb-colors.ink)
show heading.where(level: 1): it => {
set par(first-line-indent: 0em)
if it.supplement == [tb-part] {
v(2.4in)
context {
let n = tb-part-ctr.get().first()
text(font: tb-fonts.sans, tracking: 2.5pt, size: 11pt, fill: tb-colors.accent, weight: 700)[PART #numbering("I", n)]
v(0.9em, weak: true)
if tb-lang.get() == "zh" {
text(font: tb-fonts.sans, size: 27pt, weight: 900)[第#numbering("一", n)篇 #it.body]
} else {
text(font: tb-fonts.sans, size: 27pt, weight: 900, it.body)
}
let en = tb-ch-en.get()
if en != none { v(0.9em, weak: true); text(font: tb-fonts.serif, style: "italic", size: 13pt, fill: luma(40%), en) }
}
} else {
for g in tb-env-groups { counter(figure.where(kind: "tb:" + g)).update(0) }
tb-listing-ctr.update(0)
counter(math.equation).update(0)
counter(figure.where(kind: image)).update(0)
counter(figure.where(kind: table)).update(0)
counter(figure.where(kind: raw)).update(0)
v(0.7in)
if it.numbering != none {
context {
let n = counter(heading).get().first()
let apx = tb-appendix.get()
let zh-label = if apx [附录 #numbering("A", n)] else [第#numbering("一", n)章]
let en-label = if apx [APPENDIX #numbering("A", n)] else [CHAPTER #n]
box(fill: tb-colors.ink, inset: (x: 9pt, y: 5.5pt),
text(fill: white, font: tb-fonts.sans, weight: 700, size: 11.5pt,
if tb-lang.get() == "zh" { zh-label } else { en-label }))
if tb-lang.get() == "zh" {
h(11pt)
text(font: tb-fonts.sans, size: 9.5pt, weight: 600, fill: luma(45%), tracking: 1.8pt, en-label)
}
}
v(1.4em, weak: true)
}
text(font: tb-fonts.sans, size: 23pt, weight: 900, it.body)
context {
let en = tb-ch-en.get()
if en != none { v(0.7em, weak: true); text(font: tb-fonts.serif, style: "italic", size: 12.5pt, fill: luma(40%), en) }
}
v(1em, weak: true)
block(width: 100%, {
line(length: 100%, stroke: 0.6pt + luma(75%))
place(top + left, dy: -1.7pt, line(length: 38pt, stroke: 2.5pt + tb-colors.accent))
})
v(1.6em, weak: true)
}
}
show heading.where(level: 2): it => block(above: 1.7em, below: 0.9em, {
set text(font: tb-fonts.sans, size: 15pt, weight: 800)
if it.numbering != none { context text(fill: tb-colors.accent)[#counter(heading).display(it.numbering)]; h(0.6em) }
it.body
})
show heading.where(level: 3): it => block(above: 1.4em, below: 0.7em, {
set text(font: tb-fonts.sans, size: 12pt, weight: 700)
if it.numbering != none { context text(fill: tb-colors.accent)[#counter(heading).display(it.numbering)]; h(0.55em) }
it.body
})
// ---- 目录条目 ----
show outline.entry.where(level: 1): it => {
if it.element.supplement == [tb-part] {
v(1.1em, weak: true)
block(link(it.element.location(),
text(font: tb-fonts.sans, weight: 900, size: 10.5pt, fill: tb-colors.accent, it.element.body)))
} else {
v(0.7em, weak: true)
block(text(font: tb-fonts.sans, weight: 700, it))
}
}
if cover == auto { tb-cover(title, title-en, subtitle, author, series, publisher) }
else if cover != none { cover }
body
}