Skip to content

Commit 53352ea

Browse files
committed
feat: add table of contents
1 parent 4e02710 commit 53352ea

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
## 尚未实现
4646

47-
- [ ] 目录
47+
- [x] 目录
4848
- [x] 一级序号和标题居中;二级序号和标题顶格;三级及以下标题“首行缩进” 2 字符
4949
- [x] 图片
5050
- [x] 表格

appendix.typ

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
body
77
) = {
8-
set heading(numbering: none, outlined: false)
98
show heading: it => locate(loc => {
109
set par(first-line-indent: 0em)
1110

main.typ

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ Typst 还支持书写行内的代码,就像 Markdown 一样,比如 `return n
8787

8888
#pagebreak()
8989
= 附#h(2em)录
90+
#set heading(outlined: false)
9091

9192
== 附录 1
9293

93-
这是一个附录内容,学校规定附录的二级标题得是附录二字后接阿拉伯数字
94+
这是一个附录内容,学校规定附录的二级标题得是“附录”二字后接阿拉伯数字
9495

9596
但是 Typst 的中文与英文和数字之间的空格并没有像 LaTeX 那样自动空出,所以就需要自己手打了。
9697

template.typ

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,31 +89,67 @@
8989
pagebreak()
9090

9191
// 目录
92-
set page(numbering: "I")
93-
counter(page).update(1)
94-
align(center)[
95-
#outline(
96-
title: text(font: FONTSET.at("Hei"), weight: "bold", tracking: 2em, size: 16pt, [目录\ \ ]),
97-
depth: 3,
98-
)
99-
]
100-
101-
// 章节标题配置
10292
let chineseNumMap(num) = {
10393
let chineseNum = (
10494
"", "", "", "", "", "", "", "", "", "",
10595
"十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十")
10696
chineseNum.at(num - 1)
10797
}
108-
98+
99+
set page(numbering: "I")
100+
counter(page).update(1)
101+
102+
show outline: it => locate(loc => {
103+
set par(first-line-indent: 0em)
104+
105+
align(center)[
106+
#text(font: FONTSET.at("Hei"), weight: "bold", tracking: 2em, size: 16pt, [目录\ \ ])
107+
]
108+
109+
let chapterCounter = 1
110+
let sectionCounter = 1
111+
let subsectionCounter = 1
112+
113+
let headingList = query(heading, after: loc)
114+
for i in headingList {
115+
if i.outlined == false {
116+
break
117+
}
118+
119+
if i.level == 1 {
120+
set text(font: (FONTSET.at("English"), FONTSET.at("Hei")), size: 12pt, weight: "bold")
121+
122+
if i.body != [参考文献] and i.body != [致#h(2em)谢] and i.body != [附#h(2em)录] {
123+
[第#chineseNumMap(chapterCounter)章#h(1em)]
124+
}
125+
[#i.body#box(width: 1fr, repeat[.])#calc.abs(i.location().page() - loc.page())\ ]
126+
127+
chapterCounter = chapterCounter + 1
128+
sectionCounter = 1
129+
} else if i.level == 2 {
130+
[#h(1em)#calc.abs(chapterCounter - 1)\.#sectionCounter#h(1em)#i.body#box(width: 1fr, repeat[.])#calc.abs(i.location().page() - loc.page())\ ]
131+
132+
sectionCounter += 1
133+
subsectionCounter = 1
134+
} else if i.level == 3 {
135+
[#h(2em)#calc.abs(chapterCounter - 1)\.#calc.abs(sectionCounter - 1)\.#subsectionCounter#h(1em)#i.body#box(width: 1fr, repeat[.])#calc.abs(i.location().page() - loc.page())\ ]
136+
137+
subsectionCounter += 1
138+
}
139+
}
140+
})
141+
142+
outline(title: none, depth: 3, indent: true)
143+
144+
// 章节标题配置
109145
set heading(numbering: "1.1")
110146
show heading: it => locate(loc => {
111147
let levels = counter(heading).at(loc)
112148

113149
// 重置段首空格
114150
set par(first-line-indent: 0em)
115151
set text(font: FONTSET.at("Hei"), weight: "bold")
116-
152+
117153
if it.level == 1 {
118154
// 重置计数器
119155
tableCounter.update(1)
@@ -180,10 +216,10 @@
180216
[图 #chapterLevel\-#figureCounter.display() #caption]
181217
)
182218
]
183-
219+
184220
figureCounter.step()
185221
})
186-
222+
187223
figure(
188224
image(file, width: width)
189225
)
@@ -192,7 +228,7 @@
192228
#let Table(caption, columnsSet, alignSet, body) = {
193229
show table: it => locate(loc => {
194230
let chapterLevel = counter(heading).at(loc).at(0)
195-
231+
196232
align(center)[
197233
#text(
198234
font: (FONTSET.at("English"), FONTSET.at("Kai")),

0 commit comments

Comments
 (0)