#let rtext(content)={ text(fill: red,content) } #let btext(content)={ text(fill: blue,content) } = 自省库(Introspection) 文档各部分之间的交互。 该类别包含 Typst 的自省功能:通过计数器函数,你可以访问和操作页面、章节、图片及公式计数器,也可以创建自定义计数器。同时,查询函数可用于在文档内检索元素,以此生成图片清单或显示当前章节标题的页眉等内容。 大部分函数是依赖上下文的。建议在继续阅读本章内容前,先阅读关于#link()[上下文]的章节。 == 计数器(counter) 可对页面、元素等进行计数。 借助计数器功能,你可以访问并修改页面、标题、图片等对象的计数器。此外,你还可以为其他需要统计的内容自定义计数器。 计数器的值会在文档编写过程中不断变化,其当前值依赖上下文。建议在继续阅读本节前,先阅读关于#link()[上下文]的章节。 === 访问计数器(Accessing a counter) 要获取计数器的原始值,可以使用get函数。该函数返回一个数组:计数器可以拥有多个层级(适用于章节、子章节标题等场景),数组中的每一项对应一个层级。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #set heading(numbering: "1.") = Introduction Raw value of heading counter is #context counter(heading).get() ``` ],[ #set align(horizon) #image("images/8001.png",width: 100%) ] ) === 显示计数器(Displaying a counter) 通常,我们希望以更易于阅读的方式显示计数器的值。为此,我们可以调用计数器的#link()[显示]函数。该函数会获取计数器的当前值,并使用提供的编号方案或自动推断的#link()[编号]方案对其进行格式化。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #set heading(numbering: "1.") = Introduction Some text here. = Background The current value is: #context { counter(heading).display() } Or in roman numerals: #context { counter(heading).display("I") } ``` ],[ #set align(horizon) #image("images/8002.png",width: 100%) ] ) === 修改计数器(Modifying a counter) 修改计数器可使用step和update方法: #set par(hanging-indent: 1.3cm) -- step方法将计数器的值加一。由于计数器可存在多个层级,该方法可选择性接收一个层级参数。若传入该参数,计数器将在指定深度执行递增。 -- update方法支持对计数器进行任意修改。基础用法中,为它传入一个整数(多层级场景则传入数组)。如需更高灵活性,也可以传入一个函数,该函数接收当前值并返回新值。 #set par(hanging-indent: 0cm) 标题计数器会在标题展示前执行递增操作,因此即便第二次更新后计数器值为6,Analysis获取到的数字仍是7。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #set heading(numbering: "1.") = Introduction #counter(heading).step() = Background #counter(heading).update(3) #counter(heading).update(n => n * 2) = Analysis Let's skip 7.1. #counter(heading).step(level: 2) == Analysis Still at #context { counter(heading).display() } ``` ],[ #set align(horizon) #image("images/8003.png",width: 100%) ] ) === 元素计数器(Element counters) 上文展示了多种使用#link()[标题]计数器的示例。标题只是可被计数的一类元素。一般而言,计数器可以对任意#link()[可定位的元素]进行计数。 此外,计数器还可以仅统计匹配特定#link()[选择器]的元素。例如,```typc counter(figure.where(kind: image))``` 会统计包含图片的图元,而忽略其他类型的图元。 === 页面计数器(Page counter) 页面计数器比较特殊。每当发生分页时,它会自动计数。但和其他计数器一样,你也可以手动调整它的计数值。例如,你可以在前言部分使用罗马页码,然后在正文切换为阿拉伯页码,并将页面计数器重置为1。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #set page(numbering: "(i)") = Preface The preface is numbered with roman numerals. #set page(numbering: "1 / 1") #counter(page).update(1) = Main text Here, the counter is reset to one. We also display both the current page and total number of pages in Arabic numbers. ``` ],[ #set align(horizon) #image("images/8004.png",width: 100%) #image("images/8005.png",width: 100%) ] ) === 自定义计数器(Custom counters) 要定义你自己的计数器,请传入一个字符串作为键来调用counter函数。该键用于全局标识这个计数器。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #let mine = counter("mycounter") #context mine.display() \ #mine.step() #context mine.display() \ #mine.update(c => c * 3) #context mine.display() ``` ],[ #set align(horizon) #image("images/8006.png",width: 100%) ] ) === 如何增加一步(How to step) 定义并使用自定义计数器时,通常应先更新计数器,再展示计数器的值。这样一来,计数器的更新行为可以由触发计数的元素决定。举个例子,如果你要为定理编写计数器,那么在定理的定义中,就需要先执行计数器更新操作,之后再显示计数器数值与定理内容。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #let c = counter("theorem") #let theorem(it) = block[ #c.step() *Theorem #context c.display():* #it ] #theorem[$1 = 1$] #theorem[$2 < 3$] ``` ],[ #set align(horizon) #image("images/8007.png",width: 100%) ] ) 原理用标题计数器的例子最容易解释:标题计数器的更新取决于标题的级别。在标题之前执行计数步进,当遇到二级标题时,就能正确从1递进至1.1。如果在标题之后执行步进,就无法确定该递进到哪个数值。 由于计数器必须在其所统计的元素之前完成步进操作,因此计数器初始值永远为0。这样一来,第一次展示时(发生在第一次步进之后),数值就为1。 === 时间旅行(Time travel) 计数器可以穿越时间!你能够在计数器的值生成之前获知它的最终值,甚至可以确定文档中任意位置处该计数器的值。 #table( columns: (1fr,1fr), stroke: none, fill: (rgb(250, 250, 250),luma(95%)), [ ```typst #let mine = counter("mycounter") = Values #context [ Value here: #mine.get() \ At intro: #mine.at() \ Final value: #mine.final() ] #mine.update(n => n + 3) = Introduction #lorem(10) #mine.step() #mine.step() ``` ],[ #set align(horizon) #image("images/8008.png",width: 100%) ] ) === 其他类型的状态(Other kinds of state) 计数器类型与[状态]类型密切相关。阅读其文档,了解更多关于 Typst 中状态管理的信息,以及为什么计数器不直接使用普通变量。 === 构造函数(Constructo) 创建一个由键标识的新计数器。 ```typc counter(key: str label selector location function) → counter ``` - *key*: str label selector location function\ 用于全局标识该计数器的键。 - 如果为字符串,则创建仅受手动更新影响的自定义计数器; - 如果为页面函数,则统计页面数量; - 如果为#link()[选择器],则统计匹配该选择器的元素。例如,你可以 - 提供元素函数来统计该类型的元素, - 提供#link()[where]选择器来统计带有特定字段的某类元素, - 提供#link(