typst

Typst 物件

Typst 物件分为三种:

  • TypstDoc 是所有其它 Typst 物件的基类,它表示一个 Typst 文档

    这意味着,它会自动与画面的最上方对齐,以便从文档的开头开始查看

  • Typst 表示 Typst 公式,传入的字符串会被包裹在 $ $ 中作为公式进行编译

    它会直接被放到画面的中间

  • TypstText 表示 Typst 文字,传入的字符串不会被 $ $ 所包裹

    它会直接被放到画面的中间

也就是说,TypstTypstText 的区别仅是是否被包裹在公式环境中,例如 Typst('x^2')TypstText('$ x^2 $') 是等效的

Typst 子物件索引

我们知道,对于一般的对象可以使用下标索引或者布尔索引

备注

以防你不知道,这里补充一下

  • 下标索引,例如 t[0]t[1],这是常用的子物件索引方式

    还有 t[0, 1, 4] 表示取出指定索引的多个子物件

  • 布尔索引,例如 t[False, True, False, True, True] 表示取出 Group(t[1], t[3], t[4])

    也就是将那些为 True 的位置取出组成一个 Group

当你要索引 Typst 对象的子物件时,还可以使用字符索引的方式,比如说对于 Typst 对象 t 而言

t = Typst('cos^2 theta + sin^2 theta = 1')

可以使用 t['cos'] 得到 cos 对应的部分,这样你可以就可以使用类似于 t['cos'].digest_styles(color=BLUE) 的方式进行着色

当出现多个匹配时的处理

你应该注意到了这里有两个 "θ"(theta),当你使用 t['theta'] 的方式进行索引时,将会取出第一个匹配的 θ,也就是前一个

因为他们是从 01、... 依次编号的,所以你可以使用 t['theta', 1] 得到后一个,如果有更多的匹配则是使用更后面的序号

备注

这也意味着 t['theta']t['theta', 0] 是等效的

如果想要同时取出多个,则将多个编号写在一个序列中即可,例如 t['theta', (0, 1)] 则是取出编号为 01 的匹配项,在这里就是所有匹配到的 θ 符号

对于这种取出所有匹配项的情况,也可以使用 t['theta', ...],这里的省略号就表示取出所有的匹配项

一些特殊情况

当你想要取出这个 Typst 公式中的上标 "2" 时

使用 t['2'] 无法匹配到它,这是因为普通的 "2" 和上标的 "2" 长得不同

为了正确匹配,你需要把索引中的 2 也表示为“上标”的形式,例如 t['""^2'] 或者 t['#box[]^2']

这两者都是把 "2" 作为一个空元素( "" 或者 #box[] )的上标,这样就可以正常匹配了

重要

上面以 Typst 作为字符索引的例子,TypstDocTypstText 也是几乎一致的,但是会有略微区别

我们知道,在这三种对象中,只有 Typst 是在公式环境中的,所以进行它的字符索引时,作为索引的字符串也会在公式环境中解析

这意味着,对于 TypstDocTypstText 而言,作为索引的字符串不在公式环境中

这里给出几段示例作为参考:

t = Typst('cos theta')
t['theta']

t = TypstText('$ cos theta $')
t['$theta$']
t = TypstText('this is a formula: $cos^2 x + sin^2 x = 1$')
t['formula']
t['$x$']

参考文档

class janim.items.svg.typst.TypstDoc(text: str, **kwargs)

基类:SVGItem

Typst 文档

group_key: str | None = 'data-typst-label'
move_into_position() None
static compile_typst(text: str) str

编译 Typst 文档

classmethod typstify(obj: TypstPattern) TypstDoc

将字符串变为 Typst 对象,而本身已经是的则直接返回

get_label(name: str) Group[SVGElemItem]
match_pattern(target: TypstDoc, pattern: TypstPattern, ordinal: int = 0, target_ordinal: int | None = None) Self

配对并通过变换使得配对的部分重合

例如

t1 = Typst('x^2 + y^2')
t2 = Typst('x + y')
t2.points.match_pattern(t1, '+')

则会将 t2 进行变换使得二者的加号重合

get(slices, gapless: bool = False)

根据切片得到切分的子物件

在默认情况下,gapless=False

  • 表示通过给定的 slices 直接切取子物件,例如

    item.get(slice(1, 3)) == item[1:3]

  • 支持使用列表获取一批的子物件,例如

    item.get([slice(1, 3), slice(4, 7)]) == [item[1:3], item[4:7]]

  • 列表支持嵌套,并且结果保持原嵌套结构,例如

    item.get([slice(1, 3), [slice(4, 6), slice(10, 12)]]) == [item[1:3], [item[4:6], item[10:12]]]

gapless=True

  • 表示通过给定 slices 的所有起止位置将所有子物件切分并一起返回,例如

    item.get(slice(1, 3), gapless=True) == [item[:1], item[1:3], item[3:]]

  • 也支持列表以及嵌套的列表,例如

    item.get([slice(1, 3), slice(5, 7)]) == [item[:1], item[1:3], item[3:5], item[5:7], item[7:]]

  • 注:在这种情况下,所有嵌套结构都会先被展平后处理

slice(pattern: TypstPattern, ordinal: int) slice
slice(pattern: TypstPattern, ordinal: Iterable[int] | EllipsisType) list[slice]

得到指定 pattern 在该物件中形状配对的切片

  • 默认返回首个匹配的(即 ordinal=0

  • ordinal 传入其它索引可得到随后匹配的特定部分

  • ordinal 传入索引列表可得到多个匹配的特定部分

  • ordinal 传入省略号 ... 可以得到所有匹配的部分

cmpt_init_datas = {'depth': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.item.Item'>), 'points': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.points.Points'>)}
indices(pattern: TypstPattern) list[int]

找出该公式中所有出现了 pattern 的位置

  • pattern 支持使用字符串或者 Typst 对象

lps_map: dict[str, list[int]] = {}
property lps: list[int]

KMP 算法涉及的部分匹配表

class janim.items.svg.typst.Typst(text: str, *, use_math_environment: bool = True, **kwargs)

基类:TypstDoc

Typst 公式

cmpt_init_datas = {'depth': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.item.Item'>), 'points': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.points.Points'>)}
move_into_position() None
class janim.items.svg.typst.TypstText(text: str, use_math_environment: bool = False, **kwargs)

基类:Typst

Typst 文本

相当于 Typst 传入 use_math_environment=False

cmpt_init_datas = {'depth': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.item.Item'>), 'points': _ItemMeta._CmptInitData(info=<janim.components.component.CmptInfo object>, decl_cls=<class 'janim.items.points.Points'>)}
janim.items.svg.typst.get_typst_template() str