Download chp08.typ from m-rui001/typst_reference: direct link, hf CLI and curl.
- Browser
- Download file 35.6 kB
-
https://huggingface.co/datasets/m-rui001/typst_reference/resolve/main/chp08.typ
- Command line
-
hf download hf://datasets/m-rui001/typst_reference/chp08.typ
-
curl -L -o chp08.typ https://huggingface.co/datasets/m-rui001/typst_reference/resolve/main/chp08.typ
35.6 kB
| #let rtext(content)={ | |
| text(fill: red,content) | |
| } | |
| #let btext(content)={ | |
| text(fill: blue,content) | |
| } | |
| = 自省库(Introspection)<introspection> | |
| 文档各部分之间的交互。 | |
| 该类别包含 Typst 的自省功能:通过计数器函数,你可以访问和操作页面、章节、图片及公式计数器,也可以创建自定义计数器。同时,查询函数可用于在文档内检索元素,以此生成图片清单或显示当前章节标题的页眉等内容。 | |
| 大部分函数是依赖上下文的。建议在继续阅读本章内容前,先阅读关于#link(<Context>)[上下文]的章节。 | |
| == 计数器(counter)<counter> | |
| 可对页面、元素等进行计数。 | |
| 借助计数器功能,你可以访问并修改页面、标题、图片等对象的计数器。此外,你还可以为其他需要统计的内容自定义计数器。 | |
| 计数器的值会在文档编写过程中不断变化,其当前值依赖上下文。建议在继续阅读本节前,先阅读关于#link(<Context>)[上下文]的章节。 | |
| === 访问计数器(Accessing a counter)<counter_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)<counter_Displaying-a-counter> | |
| 通常,我们希望以更易于阅读的方式显示计数器的值。为此,我们可以调用计数器的#link(<counter_display>)[显示]函数。该函数会获取计数器的当前值,并使用提供的编号方案或自动推断的#link(<numbering>)[编号]方案对其进行格式化。 | |
| #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)<counter_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)<counter_Element-counters> | |
| 上文展示了多种使用#link(<heading>)[标题]计数器的示例。标题只是可被计数的一类元素。一般而言,计数器可以对任意#link(<location_Locatable-elements>)[可定位的元素]进行计数。 | |
| 此外,计数器还可以仅统计匹配特定#link(<selector>)[选择器]的元素。例如,```typc counter(figure.where(kind: image))``` 会统计包含图片的图元,而忽略其他类型的图元。 | |
| === 页面计数器(Page counter)<counter_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_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)<counter_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(<intro>) \ | |
| Final value: #mine.final() | |
| ] | |
| #mine.update(n => n + 3) | |
| = Introduction <intro> | |
| #lorem(10) | |
| #mine.step() | |
| #mine.step() | |
| ``` | |
| ],[ | |
| #set align(horizon) | |
| #image("images/8008.png",width: 100%) | |
| ] | |
| ) | |
| === 其他类型的状态(Other kinds of state)<counter_Other-kinds-of-state> | |
| 计数器类型与[状态]类型密切相关。阅读其文档,了解更多关于 Typst 中状态管理的信息,以及为什么计数器不直接使用普通变量。 | |
| === 构造函数(Constructo) | |
| 创建一个由键标识的新计数器。 | |
| ```typc | |
| counter(key: str label selector location function) → counter | |
| ``` | |
| - *key*: str label selector location function\ 用于全局标识该计数器的键。 | |
| - 如果为字符串,则创建仅受手动更新影响的自定义计数器; | |
| - 如果为页面函数,则统计页面数量; | |
| - 如果为#link(<selector>)[选择器],则统计匹配该选择器的元素。例如,你可以 | |
| - 提供元素函数来统计该类型的元素, | |
| - 提供#link(<Function_where>)[where]选择器来统计带有特定字段的某类元素, | |
| - 提供#link(<label>)[\<label\>]来统计带有该标签的元素。 | |
| 元素选择器与#link(<Function_where>)[where]选择器形式的计数器键仅支持#[可定位元素]。 | |
| === 定义(Definitions) | |
| 本节元素除了step、update都是上下文相关的, | |
| - *get*<counter_get> | |
| 获取当前位置计数器的值。始终返回整数数组,即便计数器仅包含一个数字。 | |
| 这等价于 counter.at(here())。 | |
| ```typc | |
| self.get()→ int array | |
| ``` | |
| - *display*<counter_display> | |
| 显示计数器的值。 | |
| 你可以同时指定自定义编号与自定义位置。二者默认均为自动模式,会选用合理的默认值(分别为被计数元素的编号和当前位置)。 | |
| 返回格式化后的输出内容。 | |
| ```typc | |
| self.display( | |
| numbering: auto str function, | |
| at: auto label selector location function, | |
| both: bool, | |
| ) → any | |
| ``` | |
| -- *numbering*: auto str function | |
| #link(<numbering>)[编号模式或函数],用于指定计数器的显示方式。如果传入一个函数,该函数会将计数器的每个数字作为独立参数接收。若数字数量不固定(例如用于标题参数),可以使用#link(<Argument_Sinks>)[参数收集器]。默认值:```typc auto```。 | |
| 如果省略此项或设置为#rtext[auto],则使用被计数元素对应的编号样式显示计数器;若无对应样式,则采用“1.1”格式。 | |
| -- *at*: auto label selector location function | |
| 计数器需要显示的位置。默认值:```typc auto```。 | |
| 如果使用选择器,它必须精确匹配文档中的一个元素。对此最实用的选择器类型是#link(<label>)[标签]和#link(<location>)[位置]。 | |
| 如果省略该参数或将其设为#rtext[auto],计数器将在当前位置显示。这等价于使用here()。 | |
| 编号将在一个上下文环境中执行,在该环境中here()解析为指定位置,从而使涉及更多计数器的编号能够正确解析。 | |
| -- *both*: bool | |
| 如果启用,将同时显示当前顶级计数与最终顶级计数。二者可通过同一套编号样式设置。页码属性会使用该功能,在设置类似“1 / 1”的格式时,展示当前页码与总页数。默认值:```typc false```。 | |
| - *at*<counter_at> | |
| 获取指定位置的计数器值。始终返回整数数组,即便该计数器仅有一个数值。 | |
| 选择器必须与文档内恰好一个元素匹配。最适用的选择器类型为标签与位置。 | |
| ```typc | |
| self.at(selector: label selector location function) → int array | |
| ``` | |
| -- *selector*: label selector location function | |
| 应获取计数器值的位置。 | |
| - *final*<counter_final> | |
| 检索文档末尾的计数器值。始终返回整数数组,即使计数器仅包含一个数字。 | |
| ```typc | |
| self.final() → int array | |
| ``` | |
| - *step*<counter_step> | |
| 将计数器的值加一。 | |
| 该更新会在返回内容插入文档的位置生效。如果你没有将输出放入文档中,则不会产生任何效果!例如,当你编写 `let _ = counter(page).step()` 时就会出现这种情况。计数器的更新始终按照排版顺序执行,在这种情况下,Typst 将不知道何时执行计数器步进操作。 | |
| ```typc | |
| self.step(level: int) → content | |
| ``` | |
| -- *level*: int | |
| 计数器的步进深度。默认值:```typc 1```。 | |
| - *update*<counter_update> | |
| 更新计数器的值。 | |
| 与步长的情况一样,只有将生成的内容放入文档中,更新才会生效。 | |
| ```typc | |
| self.update(update: int array function) → content | |
| ``` | |
| -- *update*: int array function | |
| 如果传入整数或整数数组,则将计数器设置为该值。如果传入函数,该函数会接收计数器的上一个值(每个数字作为独立参数),并且必须返回新值(整数或数组)。 | |
| == 此处(here)<here> | |
| 返回文档中的当前位置。 | |
| 你可以将其视为一个底层基础组件,用于直接从活动#link(<Context>)[上下文]提取当前位置。其他部分函数会在内部使用它:例如,```typc counter.get()``` 等价于```typc counter.at(here())```。 | |
| 在#link(<location_Locatable-elements>)[可定位元素]的显示规则中,```typc here()``` 将匹配被显示元素的位置。 | |
| 如果你想要展示当前页码,请参考#link(<counter>)[计数器]类型的文档。虽然 here 可用于获取物理页码,但通常你需要的是逻辑页码,比如在序言之后被重新计数的页码。 | |
| === 示例(Examples) | |
| 结合位置方法确定文档中的当前#link(<location_position>)[位置]: | |
| #table( | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| fill: (rgb(250, 250, 250),luma(95%)), | |
| [ | |
| ```typst | |
| #context [ | |
| I am located at | |
| #here().position() | |
| ] | |
| ``` | |
| ],[ | |
| #set align(horizon) | |
| #image("images/8009.png",width: 100%) | |
| ] | |
| ) | |
| #link(<query>)[查询]当前位置之前的元素: | |
| #table( | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| fill: (rgb(250, 250, 250),luma(95%)), | |
| [ | |
| ```typst | |
| = Introduction | |
| = Background | |
| There are | |
| #context query( | |
| selector(heading).before(here()) | |
| ).len() | |
| headings before me. | |
| = Conclusion | |
| ``` | |
| ],[ | |
| #set align(horizon) | |
| #image("images/8010.png",width: 100%) | |
| ] | |
| ) | |
| 有关before/after选择器的更多详情,请参阅#link(<selector>)[选择器]类型。 | |
| === 参数(Parameters) | |
| ```typc | |
| here() → location | |
| ``` | |
| == 定位(locate)<locate> | |
| 确定元素在文档中的位置。 | |
| 接收一个选择器,该选择器必须仅匹配一个元素,并返回该元素的#link(<location>)[位置]。该位置尤其可用于获取该元素实际所在的#link(<location_page>)[页]码与#link(<location_position>)[坐标](页面、横坐标、纵坐标)。 | |
| === 示例(Example) | |
| 定位特定元素: | |
| #table( | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| fill: (rgb(250, 250, 250),luma(95%)), | |
| [ | |
| ```typst | |
| #context [ | |
| Introduction is at: \ | |
| #locate(<intro>).position() | |
| ] | |
| = Introduction <intro> | |
| ``` | |
| ],[ | |
| #set align(horizon) | |
| #image("images/8011.png",width: 100%) | |
| ] | |
| ) | |
| === 参数(Parameters) | |
| ```typc | |
| locate(selector: label selector location function) → location | |
| ``` | |
| -- *selector*: label selector location function | |
| 应当仅匹配一个元素的选择器。系统会定位该元素。 | |
| 与以下场景搭配使用时尤为实用: | |
| #set par(hanging-indent: 1.3cm) | |
| -- 用于#link(<here>)[定位]当前上下文, | |
| -- 通过内容上的#link(<content_location>)[location()]方法,从某个查询元素获取的#link(<location>)[位置]。 | |
| #set par(hanging-indent: 0cm) | |
| == 位置(location)<location> | |
| 用于标识文档中的元素。 | |
| 位置可唯一标识文档内的元素,并支持获取该元素在页面上的绝对位置。你可以通过#link(<here>)[here]函数获取当前位置,也可以通过内容对象上的#link(<content_location>)[location()]方法获取查询或展示元素的位置。 | |
| === 可定位元素(Locatable elements)<location_Locatable-elements> | |
| 自动分配位置的元素称为可定位元素,可通过#link(<query>)[查询]找到: | |
| #set par(hanging-indent: 1.3cm) | |
| -- 在[模型类别]中,以下元素为可定位元素:#link(<assert>)[资源]、#link(<bibliography>)[参考文献]、#link(<cite>)[引用]、#link(<document>)[文档]、#link(<Emphasis>)[强调]、#link(<enum>)[编号列表]、#link(<figure>)[图形]、#link(<figure_Caption_customization>)[图形标题]、#link(<footnote>)[脚注]、#link(<footnote_entry>)[脚注条目]、#link(<heading>)[标题]、#link(<link>)[链接]、#link(<list>)[列表]、#link(<outline>)[目录]、#link(<outline_entry>)[目录条目]、#link(<Paragraph>)[段落]、#link(<quote>)[引文]、#link(<ref>)[引用]、#link(<strong>)[加粗]、#link(<table>)[表格]、#link(<terms>)[术语]和#link(<title>)[题名]。模型类别中的大部分元素都是可定位元素,因为标题、图形这类语义元素常配合自省功能使用。 | |
| -- 在#link(<text_type>)[文本类别]中,#link(<raw>)[原代码]元素以及#link(<underline>)[下划线]、#link(<overline>)[上划线]、#link(<strike>)[删除线]、#link(<highlight>)[高亮]这类装饰元素为可定位元素,它们本身也具备较强语义特性。 | |
| -- 在#link(<introspection>)[自省类别]中,#link(<metadata>)[元数据元素]是可定位元素,查询元数据是它的主要用途。 | |
| -- 在其他类别中,大多数元素不可定位。例外项为#link(<equation>)[数学公式]、#link(<image>)[图像]以及#link(<PDF_attach>)[PDF附件]。 | |
| #set par(hanging-indent: 0cm) | |
| 想要确定某个特定元素是否为可定位元素,你可以尝试对其执行查询。 | |
| 请注意,你仍然可以通过其他方式观察到查询中无法定位的元素,例如当这些元素附带标签时。 | |
| === 定义(Definitions) | |
| - *page*<location_page> | |
| 返回该位置对应的页码。 | |
| 请注意,该方法返回的并非此位置处#link(<counter_Page-counter>)[页面计数器]的值,而是真实页码(从1开始计数)。 | |
| 如果你想获取页面计数器的值,请改用```typc counter(page).at(loc)```。 | |
| 可与 #link(<here>)[here] 搭配使用,获取当前上下文的实际页面位置: | |
| #table( | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| fill: (rgb(250, 250, 250),luma(95%)), | |
| [ | |
| ```typst | |
| #context [ | |
| I am located on | |
| page #here().page() | |
| ] | |
| ``` | |
| ],[ | |
| #set align(horizon) | |
| #image("images/8012.png",width: 100%) | |
| ] | |
| ) | |
| ```typc | |
| self.page() → int | |
| ``` | |
| - *position*<location_position> | |
| 返回一个字典,包含该位置对应的页码以及x、y坐标。页码从1开始计数,坐标以页面左上角为原点进行计量。 | |
| 如果仅需要页码,请改用 page(),这样 Typst 可以跳过不必要的运算。 | |
| ```typc | |
| self.position() → dictionary | |
| ``` | |
| - *page-numbering*<location_page_numbering> | |
| 返回该位置页面的页码编号样式。可在展示页面计数器时使用,以获取本地编号。在创建自定义索引或目录时该功能十分有用。 | |
| 若该位置的页码编号设置为无,此函数将返回无。 | |
| ```typc | |
| self.page-numbering() → none str function | |
| ``` | |
| == 元数据(metadata)<metadata> | |
| 将一个值暴露给查询系统,且不会生成可见内容。 | |
| 可以通过查询函数获取该元素,也可以在命令行使用 typst query 进行获取。它的作用是向自省系统暴露任意值。若要在众多元数据值中定位某一个,你可以给它附加标签,再针对该标签执行查询。 | |
| 元数据元素对于命令行查询尤为实用,因为它能够向外部暴露任意值。 | |
| #table( | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| fill: (rgb(250, 250, 250),luma(95%)), | |
| [ | |
| ```typst | |
| // Put metadata somewhere. | |
| #metadata("This is a note") <note> | |
| // And find it from anywhere else. | |
| #context { | |
| query(<note>).first().value | |
| } | |
| ``` | |
| ],[ | |
| #set align(horizon) | |
| // Put metadata somewhere. | |
| #metadata("This is a note") <note> | |
| // And find it from anywhere else. | |
| #context { | |
| query(<note>).first().value | |
| } | |
| ] | |
| ) | |
| === 参数(Parameters) | |
| ```typc | |
| metadata(value: any) → content | |
| ``` | |
| -- *value*: any | |
| 要嵌入文档的值。 | |
| == 查询(query)<query> | |
| 查找文档中的元素。 | |
| 查询函数可在文档内搜索特定类型或带有特定标签的元素。使用该函数前,你需要先确保#link(<Context>)[上下文]可用。 | |
| === 查找元素(Finding elements)<query_Finding-elements> | |
| 在下面的示例中,我们手动创建目录,而不使用#link(<outline>)[outline]函数。 | |
| 为此,我们首先查询文档中所有一级且大纲属性为真的标题。仅查询一级标题可以保证,在本示例中,子标题不会被纳入目录。大纲字段用于排除“目录”这个标题本身。 | |
| 请注意,我们需要打开一个上下文,才能使用查询函数。 | |
| #table( | |
| columns: 1fr, | |
| align: left, | |
| stroke: none, | |
| fill: (x,y)=> | |
| if (y==0) {white} | |
| else if (y==1) {luma(240)} | |
| , | |
| [ | |
| ```typst | |
| #set page(numbering: "1") | |
| #heading(outlined: false)[ | |
| Table of Contents | |
| ] | |
| #context { | |
| let chapters = query( | |
| heading.where( | |
| level: 1, | |
| outlined: true, | |
| ) | |
| ) | |
| for chapter in chapters { | |
| let loc = chapter.location() | |
| let nr = counter(page).display(at: loc) | |
| [#chapter.body #h(1fr) #nr \ ] | |
| } | |
| } | |
| = Introduction | |
| #lorem(10) | |
| #pagebreak() | |
| == Sub-Heading | |
| #lorem(8) | |
| = Discussion | |
| #lorem(18) | |
| ``` | |
| ], | |
| [ | |
| #set align(horizon) | |
| #image("images/8013.png") | |
| #image("images/8014.png") | |
| ] | |
| ) | |
| 要获取页码,我们首先通过#link(<location>)[位置]获取查询返回元素的位置。随后我们也会读取该位置处的#link(<page_numbering>)[页码编号]与#link(<counter_Page-counter>)[页面计数器],并将编号应用到计数器上。 | |
| === 一句提醒(A word of caution)<query_A-word-of-caution> | |
| 为解决所有查询,Typst会对文档的各个部分执行多次求值与排版。但并不能保证你的查询一定可以完全解析。如果不加留意,查询可能会对自身产生影响,导致结果永远无法稳定。 | |
| 在下面的示例中,我们查询文档内所有标题,随后根据查询结果生成同等数量的标题。一开始只有一个标题,标题名称为Real。因此计数值为1,并生成一个Fake标题。Typst发现查询结果发生变化,会再次进行处理。这一次,计数值变成2,于是生成两个Fake标题。这个过程会持续进行。可以看到,最终输出的标题数量是有限的。这是因为Typst在尝试若干次后就会停止迭代。 | |
| 通常情况下,应当尽量不要编写会影响自身的查询。这条注意事项同样适用于计数器、状态变量等其他自省功能。 | |
| #table( | |
| columns: 1fr, | |
| align: left, | |
| stroke: none, | |
| fill: (x,y)=> | |
| if (y==0) {white} | |
| else if (y==1) {luma(240)} | |
| , | |
| [ | |
| ```typst | |
| = Real | |
| #context { | |
| let elems = query(heading) | |
| let count = elems.len() | |
| count * [= Fake] | |
| } | |
| ``` | |
| ], | |
| [ | |
| #set align(horizon) | |
| #image("images/8015.png") | |
| ] | |
| ) | |
| === 命令行查询(Command line queries)<query_Command-line-queries> | |
| (如下内容,我个人没有试通) | |
| 你也可以在命令行中使用 typst eval 命令执行查询。该命令会运行 Typst 代码(可在文档上下文环境中执行),并以序列化形式输出结果值。它将待执行的代码作为第一个参数,还可通过 --in 参数(可选)指定文档路径。 | |
| 请看下面这个包含部分不可见元数据的example.typ文件示例: | |
| ```typst | |
| #metadata("This is a note") <note> | |
| ``` | |
| 你可以按下面的方式使用 Typst 的命令行界面对其执行查询。 | |
| ```typc | |
| $ typst eval 'query(<note>)' --in example.typ | |
| [ | |
| { | |
| "func": "metadata", | |
| "value": "This is a note", | |
| "label": "<note>" | |
| } | |
| ] | |
| ``` | |
| 该命令指示 Typst 编译 example.typ,随后执行代码 query(\<note\>),并可访问生成的文档。 | |
| 注意:代码被引号包裹,防止特殊字符被命令行解释器解析。字符串的引用方式取决于你的平台/命令行。 | |
| *检索特定字段(Retrieving a specific field)* | |
| 通常,你可能只关心返回元素中的某一个特定字段。对于元数据元素而言,value字段就是我们需要关注的字段。你可以通过修改代码,仅提取该字段。 | |
| ```typc | |
| $ typst eval 'query(<note>).map(it => it.value)' --in example.typ | |
| ["This is a note"] | |
| ``` | |
| *查询特定导出目标(Querying for a specific export target)* | |
| 如果在针对特定目标导出时需要查询文档,可以使用 ```typc --target``` 参数。有效值为 paged 和 html(已启用 #link(<HTML>)[html] 功能时)。 | |
| === 参数(Parameters) | |
| ```typc | |
| query(target: label selector location function) → array | |
| ``` | |
| - *target*: label selector location function | |
| 可以是 | |
| #set par(hanging-indent: 1.3cm) | |
| -- 诸如标题或图形这类元素函数, | |
| -- 一个\<label\>, | |
| -- 像heading.where(level: 1)这样更复杂的选择器, | |
| -- 或者selector(heading).before(here())。 | |
| #set par(hanging-indent: 0cm) | |
| 仅支持#link(<location_Locatable-elements>)[可定位]的元素函数。 | |
| == 状态(state)<state> | |
| 管理文档中有状态的部分。 | |
| 假设你的文档里有一些计算,并且想要保存上一次的计算结果,供下一次计算使用。你可能会尝试编写类似下面的代码,期望它依次输出 10、13、26 和 21。但这在 Typst 中无法实现。如果你运行这段代码,会看到 Typst 抛出如下报错信息:函数外部的变量为只读状态,不可修改。 | |
| ```typst | |
| // This doesn't work! | |
| #let star = 0 | |
| #let compute(expr) = { | |
| star = eval( | |
| expr.replace("⭐", str(star)) | |
| ) | |
| [New value is #star.] | |
| } | |
| #compute("10") \ | |
| #compute("⭐ + 3") \ | |
| #compute("⭐ * 2") \ | |
| #compute("⭐ - 5") | |
| ``` | |
| === 状态与文档标记(State and document markup)<state_State-and-document-markup> | |
| 为什么会出现这种情况?因为一般来说,这类带有副作用的计算在文档标记中会产生问题,而 Typst 对此直言不讳。要让计算结果具备合理性,计算的执行顺序必须和结果在文档里的排版顺序保持一致。在我们这个简单示例中,确实满足该条件,但在一般场景下未必如此。 | |
| 我们来看一种略有区别但性质相近的状态:标题编号。我们希望每遇到一个标题就把标题计数器加一。听起来很简单,对吧?直接加1就行。但实际并没有这么简单。请看下面这个示例: | |
| #table( | |
| columns: 1fr, | |
| align: left, | |
| stroke: none, | |
| fill: (x,y)=> | |
| if (y==0) {white} | |
| else if (y==1) {luma(240)} | |
| , | |
| [ | |
| ```typst | |
| #set heading(numbering: "1.") | |
| #let template(body) = [ | |
| = Outline | |
| ... | |
| #body | |
| ] | |
| #show: template | |
| = Introduction | |
| ... | |
| ``` | |
| ], | |
| [ | |
| #set align(horizon) | |
| #image("images/8016.png") | |
| ] | |
| ) | |
| 在这里,Typst 会先处理显示规则之后的文档正文,识别到“引言”标题,再将处理后的内容传入模板函数,之后才会读取目录。如果单纯依次计数,引言会被编为1,目录则编为2。 | |
| === 在Typst中管理状态(Managing state in Typst)<state_Managing-state-in-Typst> | |
| 那我们该用什么替代方案呢?我们使用 Typst 的状态管理系统。调用 state 函数时传入标识字符串键以及可选的初始值,就可以得到一个状态值,该状态值会提供若干方法。其中最重要的两个是 get 和 update: | |
| -- #link(<state_get>)[get] 函数获取状态的当前值。由于该值会在文档生成过程中发生变化,它是一个上下文函数,仅在存在上下文的场景下可用。 | |
| -- #link(<state_update>)[update] 函数修改状态。你可以向它传入任意值。如果传入非函数值,它会将状态设置为该值;如果传入函数,该函数会接收上一个状态,并且必须返回新的状态。 | |
| 我们最初的示例现在会是这样: | |
| #table( | |
| columns: 1fr, | |
| align: left, | |
| stroke: none, | |
| fill: (x,y)=> | |
| if (y==0) {white} | |
| else if (y==1) {luma(240)} | |
| , | |
| [ | |
| ```typst | |
| #let star = state("star", 0) | |
| #let compute(expr) = { | |
| star.update(old => | |
| eval(expr.replace("⭐", str(old))) | |
| ) | |
| [New value is #context star.get().] | |
| } | |
| #compute("10") \ | |
| #compute("⭐ + 3") \ | |
| #compute("⭐ * 2") \ | |
| #compute("⭐ - 5") | |
| ``` | |
| ], | |
| [ | |
| #set align(horizon) | |
| #image("images/8017.png") | |
| ] | |
| ) | |
| Typst 管理的状态始终按照排版顺序更新,而非求值顺序。update 方法会返回内容,其作用效果发生在返回内容被插入文档的位置。 | |
| 因此,我们现在也可以将部分计算结果存入变量中,且它们仍然会输出正确的结果: | |
| #table( | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| fill: (rgb(250, 250, 250),luma(95%)), | |
| [ | |
| ```typst | |
| #let star = state("star", 0) | |
| #let compute(expr) = { | |
| star.update(old => | |
| eval(expr.replace("⭐", str(old))) | |
| ) | |
| [New value is #context star.get().] | |
| } | |
| #let more = [ | |
| #compute("⭐ * 2") \ | |
| #compute("⭐ - 5") | |
| ] | |
| #compute("10") \ | |
| #compute("⭐ + 3") \ | |
| #more | |
| ``` | |
| ],[ | |
| #set align(horizon+left) | |
| #let star = state("star", 0) | |
| #let compute(expr) = { | |
| star.update(old => | |
| eval(expr.replace("⭐", str(old))) | |
| ) | |
| [New value is #context star.get().] | |
| } | |
| #let more = [ | |
| #compute("⭐ * 2") \ | |
| #compute("⭐ - 5") | |
| ] | |
| #compute("10") \ | |
| #compute("⭐ + 3") \ | |
| #more | |
| ] | |
| ) | |
| 这个例子当然有点滑稽,但在实际应用中,这往往正是你想要的效果!标题计数器就是一个很好的例子,这也是 Typst 的计数系统与其状态系统非常相似的原因。 | |
| === 时间回溯(Time Travel)<state_Time-Travel> | |
| 借助 Typst 的状态管理系统,你还能获得时间回溯功能!我们可以在文档任意位置,查询文档里其他任意位置处的状态值。具体来说,at 方法可获取文档中任意特定位置的状态值,而 final 方法则能获取文档末尾处的状态值。 | |
| #table( | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| fill: (rgb(250, 250, 250),luma(95%)), | |
| [ | |
| ```typst | |
| ... | |
| Value at `<here>` is | |
| #context star.at(<here>) | |
| #compute("10") \ | |
| #compute("⭐ + 3") \ | |
| *Here.* <here> \ | |
| #compute("⭐ * 2") \ | |
| #compute("⭐ - 5") | |
| ``` | |
| ],[ | |
| #set align(horizon) | |
| #image("images/8018.png") | |
| ] | |
| ) | |
| === 提醒一句(A word of caution)<state_A-word-of-caution> | |
| 为求解所有状态的值,Typst 会多次执行代码的各个部分。但无法保证你对状态的操作一定可以完全求解。 | |
| 例如,如果你依据某个状态的最终值来生成状态更新,计算结果可能永远无法收敛。下面这个例子就说明了这一点。我们将状态初始化为 1,随后把它更新为其自身最终值加 1。那么它本应等于 2,可此时它的最终值变成了 2,于是又应该等于 3,以此类推。这个示例会输出一个有限数值,原因是 Typst 在尝试数次后就会直接停止迭代。 | |
| #table( | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| fill: (rgb(250, 250, 250),luma(95%)), | |
| [ | |
| ```typc | |
| // This is bad! | |
| #let x = state("key", 1) | |
| #context x.update(x.final() + 1) | |
| #context x.get() | |
| ``` | |
| ],[ | |
| #set align(horizon) | |
| #let x = state("key", 1) | |
| #context x.update(x.final() + 1) | |
| #context x.get() | |
| ] | |
| ) | |
| 一般来说,应尽量避免在上下文表达式内部生成状态更新。如果可行,请将更新逻辑写为非上下文相关的值,或是能够基于旧值计算出新值的函数。有时难免需要这样做,但在这类情况下,需要由你来保证结果能够收敛。 | |
| === 构造函数(Constructor) | |
| ```typc | |
| state( | |
| key: str, | |
| init: any, | |
| ) → state | |
| ``` | |
| - *key*: str | |
| 用于标识该状态的键。 | |
| 对该状态的任何更新都将通过这个字符串键进行识别。如果你使用相同的键创建多个状态,那么更新其中任意一个都会影响所有状态。 | |
| - *init*: any | |
| 状态的初始值。缺省值:none。 | |
| 如果你使用相同键但不同初始值构造多个状态,它们会各自使用自身的初始值,但共享更新操作。具体来说,文档中某个位置的状态值,将由该状态的初始值以及该状态键对应的所有前置更新计算得出。 | |
| #table( | |
| columns: (1fr,1fr), | |
| stroke: none, | |
| fill: (rgb(250, 250, 250),luma(95%)), | |
| [ | |
| ```typst | |
| #let banana = state("key", "🍌") | |
| #let broccoli = state("key", "🥦") | |
| #banana.update(it => it + "😋") | |
| #context [ | |
| - #state("key", "🍎").get() | |
| - #banana.get() | |
| - #broccoli.get() | |
| ] | |
| ``` | |
| ],[ | |
| #set align(horizon) | |
| #image("images/8019.png") | |
| ] | |
| ) | |
| === 定义(Definitions) | |
| - *get* (上下文)<state_get> | |
| 获取当前位置的状态值。 | |
| 这等价于```typc state.at(here())```。 | |
| ```typc | |
| self.get() → any | |
| ``` | |
| - *at* (上下文)<state_at> | |
| 检索指定选择器唯一匹配项处的状态值。 | |
| 该选择器必须与文档内恰好一个元素匹配。#link(<label>)[标签]和#link(<location>)[位置]是这类场景下最实用的选择器类型。 | |
| ```typc | |
| self.at(selector: label selector location function) → any | |
| ``` | |
| -- *selector*: label selector location function | |
| 应获取该状态值的位置。 | |
| - *final* (上下文)<state_final> | |
| 检索文档末尾处的状态值。 | |
| ```typc | |
| self.final() → any | |
| ``` | |
| - *update*<state_update> | |
| 更新状态的值。 | |
| 返回一段不可见#link(<content>)[内容],必须将其插入文档才能生效。该不可见内容会告知 Typst,在内容插入文档的位置执行指定更新操作。 | |
| 状态是文档的组成部分,如同嵌入文档内容中的一条线索。状态的值是文档内到当前位置为止所有状态更新执行后的结果。 | |
| 这就是 ```typc state.update``` 会返回一小段不可见内容,且你必须把它放回文档中的原因——没有在文档里“放置”的状态更新不会执行;更新发生的时机由放置位置决定。这也是读取状态需要上下文的原因:你需要借助文档当前位置,确定自己处于状态这条“线索”的哪个节点。 | |
| 将状态更新存入变量(例如 ```typc let my-update = state("key").update(c => c * 2)```)本身不会产生任何效果。只有当你把变量 ```typ #my-update``` 插入文档内容的某处时,更新才会在插入位置生效。你也可以在不同位置多次使用 `#my-update`,此时该更新也会多次执行。 | |
| 与 #link(<state_get>)[get]、#link(<state_at>)[at] 和 #link(<state_final>)[final] | |
| 不同,本函数不需要#link(<Context>)[上下文]。这是因为创建状态更新时,我们无需知道当前在文档中的位置;只有解析状态的值时才需要该信息。 | |
| ```typc | |
| self.update(update: function any) → content | |
| ``` | |
| -- *update*: function any | |
| 用于更新的目标值,或是用于更新的函数。 | |
| -- 如果传入非函数类型的值,则将状态设置为该值。 | |
| -- 如果传入函数,该函数会接收状态的上一个值,并且需要返回状态的新值。 | |
| 基于上一个值更新状态时,应当优先使用函数形式,而不是从#link(<Context>)[上下文]中读取上一个值。这种方式能让编译器高效解析最终状态,最大限度减少所需的#link(<Context_Compiler>)[布局迭代次数]。 | |
| 在下面的示例中,```typc fill.update(f => not f)``` 会按预期渲染项目符号列表里的奇数项。但如果将其替换为 ```typc context fill.update(not fill.get())```,布局在5次尝试内将无法收敛,因为每次更新都需要额外一次迭代来传递变更。 | |
| #table( | |
| columns: 1fr, | |
| align: left, | |
| stroke: none, | |
| fill: (x,y)=> | |
| if (y==0) {white} | |
| else if (y==1) {luma(240)} | |
| , | |
| [ | |
| ```typst | |
| #let fill = state("fill", false) | |
| #show list.item: it => { | |
| fill.update(f => not f) | |
| context { | |
| set text(fill: fuchsia) if fill.get() | |
| it | |
| } | |
| } | |
| #lorem(5).split().map(list.item).join() | |
| ... | |
| ``` | |
| ], | |
| [ | |
| #set align(horizon) | |
| #image("images/8020.png") | |
| ] | |
| ) | |