Skip to content

如何实现第零章

如何实现第零章

“第零章”的原本设计

“第零章”表示首章尚未出现,会用于首章以前的小节、图表。

typst
#set heading(numbering: "1.1")
== 第零章的小节
= 第一章
Typst compiled image

不过若无这种内容,首章按“第零章”也无妨。

代码是死的,人是活的。虽然 counter 不能设置为负数,但我们可以直接指定整个编号。

typst
#set heading(numbering: "第一章")
#heading(numbering: (..) => "第零章")[这对吗]
#counter(heading).update((a, ..) => a - 1)
= 你说得对
= 但是原神
#heading(numbering: (..) => "第π章")[对的兄弟]
Typst compiled image

或者显示编号的时候减去 1

typst
#set heading(
  numbering: n => numbering("第一章", calc.max(n - 1, 0)),
)
= 这对吗
= 你说得对
= 但是原神
Typst compiled image
为何需要calc.max(…, 0)

在排版收敛之前,n可能为零。此时n - 1是负值,会让numbering报错。为避免报错,让排版继续迭代,套上calc.max(…, 0)以令之非负。

参考 Why the heading number can be zero in v0.14.0-rc.2? - Questions - Typst Forum

What do you think?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
Comments
  • Latest
  • Oldest
  • Hottest
Powered by Waline v3.5.1

基于 MIT 许可发布