#let rtext(content)={ text(fill: red,content) } #let btext(content)={ text(fill: blue,content) } = 基础库 基础类型与函数。 在这里,你可以查看整数、字符串等基础数据类型的文档,以及核心计算函数的详细说明。 == 函数参数(arguments) 捕获函数的参数。 参数可以是位置参数或命名参数,可以通过pos、named和at方法访问。 此外,可以使用类似于字典的类型语法访问命名参数。 === 参数接收器(Argument Sinks) 与内置函数一样,自定义函数也可以接受可变数量的参数。您可以指定一个参数接收器,将所有多余的参数收集为..sink。生成的sink值属于参数类型。它公开了访问位置参数和命名参数的方法。 #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(240)), [ ```typst #let format(title, ..authors) = { let by = authors .pos() .join(", ", last: " and ") [*#title* _Written by #by;_] } #format("ArtosFlow", "Jane", "Joe") ``` ],[ #set align(horizon) #let format(title, ..authors) = { let by = authors .pos() .join(", ", last: " and ") [*#title*\ _Written by #by;_] } #format("ArtosFlow", "Jane", "Joe") //#image("images/2001.png",width: auto) ] ) === 扩散(Spreading) 和参数接收器相反,您可以将参数、数组和字典扩散到函数调用中..sead运算符: #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),rgb(220,220,220)), [ ```typst #let array = (2, 3, 5) #calc.min(..array) #let dict = (fill: blue) #text(..dict)[Hello] ``` ],[ #set align(horizon) #let array = (2, 3, 5) #calc.min(..array) #let dict = (fill: blue) #text(..dict)[Hello] //#image("images/2002.png",width: auto) ] ) === 构造函数(Constructor) 构建可传播的参数。 此函数的行为类似于let args(..sink)=sink。 #table( columns: (1.2fr), align: (left), stroke: none, fill: (x,y)=> if (x==0 and y==1) {luma(240)}, [ ```typst #let args = arguments(stroke: red, inset: 1em, [Body]) #box(..args) ``` ],[ #set align(horizon) //#image("images/2003.png",width: auto) #let args = arguments(stroke: red, inset: 1em, [Body]) #box(..args) ] ) #table( columns: (1.2fr), align: (left), stroke: none, fill: rgb(250, 250, 250), [ ```typc arguments(..any) → arguments ``` ] ) #strong("arguments") any Required Positional Variadic 要构建的参数。 === 定义(Definitions) - *len* 参数的数量,无论是位置参数还是命名参数。 ```typc self.len() → int ``` - *at* 返回指定索引处的位置参数,或具有指定名称的命名参数。 如果键是整数,则相当于首先调用#link()[pos],然后调用#link()[array.at]。如果键是字符串,则等同于首先调用#link()[named],然后调用#link()[dictionary.at]。 如果不需要默认值,也可以使用字段语法(例如```typc arguments(key:42).key)```访问命名参数。与字典不同,参数上的类型不能修改。 ```typc self.at( key: int str, default: any, ) → any ``` - *pos* 将捕获的位置参数作为数组返回。 ```typc self.pos() → array ``` - named 将捕获的命名参数作为字典返回。 ```typc self.named() → dictionary ``` - *filter* 仅使用值通过测试的参数生成新参数。 ```typc self.filter(test:function) → arguments ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),rgb(220,220,220)), [ ```typst #{ arguments(-1, a: 0, b: 1, 2) .filter(v => v > 0) } ``` ],[ #set align(horizon) #image("images/2004.png",width: auto) ] ) - *map* 通过使用传递的函数转换每个参数值来生成新参数。 ```typc self.map(mapper function) → arguments ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),rgb(220,220,220)), [ ```typst #{ arguments(0, a: 1, 2) .map(v => v + 1) } ``` ],[ #set align(horizon) #image("images/2005.png",width: auto) ] ) == 数组(array) 一个值序系列。 您可以通过将逗号分隔的值序列括在括号中来构造数组。这些值不必是同一类型。 您可以使用```typc .at()```方法访问和更新数组项。索引从零开始,负索引环绕到数组的末尾。您可以使用#link()[for循环]迭代数组。数组可以用+运算符加在一起,#link()[连接在一起]并乘以整数。 注意:长度为1的数组需要一个尾随逗号,如(#rtext()[1],)中所示。这是为了与简单的括号表达式(如#rtext()[```typc (1+2)*3```])区分开来。空数组写为()。 === 示例(Example) #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),rgb(220,220,220)), [ ```typst #let values = (1, 7, 4, -3, 2) #values.at(0) \ #(values.at(0) = 3) #values.at(-1) \ #values.find(calc.even) \ #values.filter(calc.odd) \ #values.map(calc.abs) \ #values.rev() \ #(1, (2, 3)).flatten() \ #(("A", "B", "C") .join(", ", last: " and ")) ``` ],[ #set align(horizon) #image("images/2006.png",width: auto)] ) === 构造函数(Example) 将值转换为数组。 请注意,此函数仅用于将类似集合的值转换为数组,而不是从单个项创建数组。请改用数组语法(1,2,3)(或(1,)表示单个元素数组)。 ```typst array(value:bytes array version) → array ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #let hi = "Hello 😃" #array(bytes(hi)) ``` ],[#image("images/2007.png",width: auto)] ) === 定义(Definitions) - *len* 数组中值的个数。 ```typc self.len() → int ``` - *first* 返回数组中的第一个项。可用于作业的左侧。如果数组为空,则返回默认值;如果未指定默认值,则返回错误。 ```typc self.first(default:any) → any ``` - *last* 返回数组中的最后一个项。可用于作业的左侧。如果数组为空,则返回默认值;如果未指定默认值,则返回错误。 ```typc self.last(default:any) → any ``` - *at* 返回数组中指定索引处的项。可用于作业的左侧。如果索引超出界限,则返回默认值;如果未指定默认值,则返回错误。 ```typc self.at( index:int, default: any, ) → any ``` - *push* 在数组末尾添加一个值。 ```typc self.push(value:ny) → none ``` - *pop* 从数组中删除最后一项并返回。如果数组为空,则失败并返回错误。 ```typc self.pop() → any ``` - *insert* 在数组的指定索引处插入一个值,将所有后续元素向右移动。如果索引超出界限,则失败并出现错误。 要替换数组的元素,请使用at。 ```typc self.insert( index:int, value:any, ) → none ``` - *remove* 从数组中删除指定索引处的值并返回。 ```typc self.remove( index:int, default: any, ) → any ``` - *slice* 提取阵列的子切片。如果开始或结束索引超出界限,则失败并出现错误。 ```typc self.slice( start:int, end:none int, count: int, ) → array ``` - *contains* 数组是否包含指定值。 此方法还具有专用语法:您可以在(1,2,3)中写入2,而不是在(1, 2, 3).contains(2). ```typc self.contains(value:any) → bool ``` - *find* 搜索给定函数返回true的项目,如果没有匹配项,则返回第一个匹配项或无匹配项。 ```typc self.find(searcher:function) → none any ``` - *position* 搜索给定函数返回true的项目,并返回第一个匹配项的索引,如果没有匹配项,则返回无。 ```typc self.position(searcher:function) → none int ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #let values = (1, 7, 4, 6, 9) #values.position(x => calc.even(x)) \ // Or equivalently: #values.position(calc.even) ``` ],[#image("images/2008.png",width: auto)] ) - *range* 创建一个由数字序列组成的数组。 如果只传递一个位置参数,则将其解释为范围的结束。如果你通过两个,它们描述了范围的开始和结束。 此函数在数组函数的作用域和全局范围内都可用。 ```typc array.range( start:int, end:int, inclusive: bool, step: int, ) → array ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #range(5) \ #range(2, 5) \ #range(20, step: 4) \ #range(21, step: 4) \ #range(5, 2, step: -1) ``` ],[#image("images/2009.png",width: auto)] ) #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #range(0, inclusive: true) \ #range(7, 10, inclusive: true) \ #range(-8, -4, inclusive: true) \ #range(-6, step: -2, inclusive: true) ``` ],[#image("images/2010.png",width: auto)] ) - *filter* 生成一个新数组,其中仅包含原始数组中给定函数返回true的项。 ```typc self.filter(test:function) → array ``` - *map* 生成一个新数组,其中原始数组中的所有项目都用给定的函数进行了转换。 ```typc self.map(mapper:function) → array ``` - *enumerate* 返回一个新数组,其中包含值及其索引。 返回的数组由长度为2的数组形式的(索引,值)对组成。这些可以用let绑定或for循环来访问。 ```typc self.enumerate(start:int) → array ``` #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst #for (i, value) in ("A", "B", "C").enumerate() { [#i: #value \ ] } #("A", "B", "C").enumerate(start: 1) ``` ],[#image("images/2011.png",width: auto)] ) - *zip* 将数组与其他数组压缩。 返回一个数组数组,其中第i个内部数组包含每个原始数组中的所有第i个元素。 如果要压缩的数组长度不同,则将其压缩到最短数组的最后一个元素,并忽略所有剩余元素。 此函数是可变的,这意味着您可以一次将多个数组压缩在一起: ```typc (1, 2).zip(("A", "B"), (10, 20))``` 产生 ```typc ((1, "A", 10), (2, "B", 20)). ``` ```typc self.zip( exact: bool, others:..array, ) → array ``` ```typC (1, 2).zip((1, 2, 3), exact: true) ``` 产生一个错误。 - *fold* 使用累加器函数将所有项目折叠成一个值。 ```typc self.fold( init:any, folder:function, ) → any ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #let array = (1, 2, 3, 4) #array.fold(0, (acc, x) => acc + x) ``` ],[#image("images/2012.png",width: auto)] ) - *sum* 将所有项目相加(适用于可以相加的所有类型)。 ```typc self.sum(default:any) → any ``` - *product* 计算所有项目的乘积(适用于所有可以相乘的类型)。 ```typc self.product(default:any) → any ``` - *any* 给定函数是否对数组中的任何项返回true。 ```typc self.any(test:function) → bool ``` - *all* 给定函数是否对数组中的所有项返回true。 ```typc self.all(test:function) → bool ``` - *flatten* 将所有嵌套数组组合成一个平面数组。 ```typc self.flatten() → array ``` - *rev* 返回一个具有相同项但顺序相反的新数组。 ```typc self.rev() → array ``` - *split* 在出现指定值时拆分数组。 ```typc self.split(at:any) → array ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #(1, 1, 2, 3, 2, 4, 5).split(2) ``` ],[#image("images/2013.png",width: auto)] ) - *join* 将数组中的所有项合并为一个。 ```typc self.join( separator:none any, last: any, default: none any, ) → any ``` - *intersperse* 返回一个数组,其中分隔符值的副本放置在相邻元素之间。 ```typc self.intersperse(separator:any) → array ``` - *chunks* 将数组拆分为不重叠的块,从开头开始,以单个余数块结束。 除最后一个块外,所有块都有块大小元素。如果exact设置为true,则如果余数包含的元素小于块大小,则将其丢弃。 ```typc self.chunks( chunk-size:int, exact: bool, ) → array ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #let array = (1, 2, 3, 4, 5, 6, 7, 8) #array.chunks(3) \ #array.chunks(3, exact: true) ``` ],[#image("images/2014.png",width: auto)] ) - *windows* 返回数组上窗口大小元素的滑动窗口。 如果数组长度小于窗口大小,则将返回一个空数组。 ```typc self.windows(window-size:int) → array ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #let array = (1, 2, 3, 4, 5, 6, 7, 8) #array.windows(5) ``` ],[#image("images/2015.png",width: auto)] ) - *sorted* 返回此数组的排序版本,可选地按给定的键函数返回。所使用的排序算法是稳定的。 如果无法比较所选的一对值,或者如果键或比较函数(如果给定)产生错误,则返回错误。 要同时根据多个条件进行排序,例如,在某些条件之间相等的情况下,key函数可以返回一个数组。结果按字典顺序排列。 ```typc self.sorted( key: function, by: function, ) → array ``` #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst #let array = ( (a: 2, b: 4), (a: 1, b: 5), (a: 2, b: 3), ) #array.sorted(key: it => (it.a, it.b)) ``` ],[#image("images/2016.png",width: auto)] ) #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(240)), [ ```typst #( "sorted", "by", "decreasing", "length", ).sorted( key: s => s.len(), by: (l, r) => l >= r, ) ``` ],[ #set align(horizon) #image("images/2017.png",width: auto)] ) - *dedup* 对阵列中的所有项目进行重复数据消除。 返回一个删除了所有重复项的新数组。只保留每个副本的第一个元素。 ```typc self.dedup(key:function) → array ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #(3, 3, 1, 2, 3).dedup() ``` ],[#image("images/2018.png",width: auto)] ) #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst #("apple", "banana", " apple ").dedup(key: s => s.trim()) ``` ],[#image("images/2019.png",width: auto)] ) - *to-dict* 将成对数组转换为字典。每对的第一个值是键,第二个值是值。 如果同一键出现多次,则选择最后一个值。 ```typc self.to-dict() → dictionary ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #( ("apples", 2), ("peaches", 3), ("apples", 5), ).to-dict() ``` ],[ #set align(horizon) #image("images/2020.png",width: auto) ] ) - *reduce* 通过反复应用缩减操作,将元素缩减为单个元素。 如果数组为空,则返回none,否则返回缩减的结果。 reduce函数是一个有两个参数的闭包:一个“累加器”和一个元素。 对于至少有一个元素的数组,这与array.fold相同,将数组的第一个元素作为初始累加器值,将每个后续元素折叠到其中。 ```typc self.reduce(reducer:function) → any ``` #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst #let array = (2, 1, 4, 3) #array.reduce((acc, x) => calc.max(acc, x)) ``` ],[#image("images/2021.png",width: auto)] ) == 断言(assert) 确保满足条件。 如果条件未满足,则失败并出现错误。文档中不产生任何输出。 如果你想测试两个值之间的相等性,请参阅#link()[assert.eq]和#link()[assert.ne]。 === 示例(Example) ```typst #assert(1 < 2, message: "math broke") ``` === 参数(Parameters) ```typc assert( condition:bool, message: str, ) → none ``` === 定义(Definitions) - eq 确保两个值相等。 如果第一个值不等于第二个值,则失败并出现错误。文档中不产生任何输出。 ```typst #assert.eq(10, 10) ``` ```typc assert.eq( left:any, right:any, message: str, ) → none ``` - ne 确保两个值不相等。 如果第一个值等于第二个值,则失败并出现错误。文档中不产生任何输出。 ```typst #assert.ne(3, 4) ``` ```typc assert.ne( left:any, right:any, message: str, ) → none ``` == 自动(auto) 表示智能默认值的参数。 auto类型仅有一个值:#rtext()[auto]。 支持自动值的参数具有智能默认或上下文行为。一个典型的例子是文本方向参数。将其设置为自动后,Typst会根据文本语言自动确定方向。 == 布尔(bool) 一种有两个州的类型。 布尔类型有两个值:#rtext()[true]和#rtext()[false]。它表示某物是否处于活动状态或启用状态。 #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #false \ #true \ #(1 < 2) ``` ],[#image("images/2022.png",width: auto)] ) == 字节(bytes) 字节序列。 这在概念上类似于#rtext()[0]到#rtext()[255]之间的#link()[整数]数组,但表示起来更有效。您可以使用#link()[for循环]对其进行迭代。 您可以转换 #set par(hanging-indent: 1.3cm) -- 使用bytes构造函数将字符串或整数数组转换为字节 -- 使用str构造函数将字节转换为字符串,并采用UTF-8编码 -- 使用数组构造函数将字节转换为整数数组 #set par(hanging-indent: 0cm) 从文件读取数据时,您可以决定是将其作为字符串还是原始字节加载。 #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(240)), [ ```typst #bytes((123, 160, 22, 0)) \ #bytes("Hello 😃") #let data = read( "rhino.png", encoding: none, ) // Magic bytes. #array(data.slice(0, 4)) \ #str(data.slice(1, 4)) ``` ],[ #set align(horizon) #image("images/2023.png",width: auto) ] ) === 构造函数(Constructor) 将值转换为字节。 #set par(hanging-indent: 1.3cm) -- 字符串以UTF-8编码。 -- 0到255之间的整数数组直接转换。专用字节表示比数组表示高效得多,因此通常用于大字节缓冲区(例如图像数据)。 #set par(hanging-indent: 0cm) ```typc bytes(value:str bytes array) → bytes ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #bytes("Hello 😃") \ #bytes((123, 160, 22, 0)) ``` ],[#image("images/2024.png",width: auto)] ) === 定义(Definitions) - *len* 长度(以字节为单位)。 ```typc self.len() → int ``` - *at* 返回指定索引处的字节。如果索引超出界限,则返回默认值;如果未指定默认值,则返回错误。 ```typc self.at( index:int, default: any, ) → any ``` - *slice* 提取字节的子切片。如果开始或结束索引超出界限,则失败并出现错误。 ```typc self.slice( start:int, end:none int, count: int, ) → bytes ``` == 计算(Calculation) 数值计算和处理模块。 这些定义是计算模块的一部分,默认情况下不会导入。除了下面列出的函数外,calc模块还定义了常数pi、tau、e和inf。 === 函数(Functions) - *abs* 计算数值的绝对值。 ```typc calc.abs(value:int float length angle ratio fraction decimal) → any ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.abs(-5) \ #calc.abs(5pt - 2cm) \ #calc.abs(2fr) \ #calc.abs(decimal("-342.440")) ``` ],[#image("images/2025.png",width: auto)] ) - *pow* 幂函数。 ```typc calc.pow(base:int float decimal,exponent:int float,) → int float decimal ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.pow(2, 3) \ #calc.pow(decimal("2.5"), 2) ``` ],[#image("images/2026.png",width: auto)] ) - *exp* 底数e的指数函数。 ```typc alc.exp(exponent:int float ) → float ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.exp(1) ``` ],[#image("images/2027.png",width: auto)] ) - *sqrt* 计算一个数的平方根。 ```typc calc.sqrt(value:int float) → float ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.sqrt(16) \ #calc.sqrt(2.5) ``` ],[#image("images/2028.png",width: auto)] ) - *root* 计算一个数的n次方根。 ```typc calc.root(radicand:float,index:int,) → float ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.root(16.0, 4) \ #calc.root(27.0, 3) ``` ],[#image("images/2029.png",width: auto)] ) - *sin* 计算角度的正弦值。 当用整数或浮点数调用时,它们将被解释为弧度。 ```typc calc.sin(value:int float angle) → float ``` - *cos* 计算角度的余弦值。 当用整数或浮点数调用时,它们将被解释为弧度。 ```typc calc.cos(value:int float angle) → float ``` - *tan* 计算角度的正切值。 当用整数或浮点数调用时,它们将被解释为弧度。 ```typc calc.tan(value:int float angle) → float ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.tan(1.5) \ #calc.tan(90deg) ``` ],[#image("images/2030.png",width: auto)] ) - *asin* 计算一个数字的反正弦。 ```typc calc.asin(value:int float) → angle ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.asin(0) \ #calc.asin(1) ``` ],[#image("images/2031.png",width: auto)] ) - *acos* 计算一个数字的反余弦。 ```typc calc.acos(value:int float) → angle ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.acos(0) \ #calc.acos(1) ``` ],[#image("images/2032.png",width: auto)] ) - *atan* 计算一个数字的反正切。 ```typc calc.atan(value:int float) → angle ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.atan(0) \ #calc.atan(1) ``` ],[#image("images/2033.png",width: auto)] ) - *atan2* 计算坐标的四象限反正切。 $(x,y)$的四象限反正切被定义为复数$x+i y$的自变量。 返回-180度和180度之间的角度。 请注意,此函数接受$(x,y)$,而不是$(y,x)$。 ```typc calc.atan2(x:int float,y:int float,) → angle ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.atan2(1, 1) \ #calc.atan2(-2, -3) ``` ],[#image("images/2034.png",width: auto)] ) - *sinh* 计算双曲角的双曲正弦。 #set math.equation(numbering: none) $x$值双曲正弦定义如下: $ (e^x-e^(-x))/2 $ ```typc calc.sinh(value:float) → float ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.sinh(0) \ #calc.sinh(1.5) ``` ],[#image("images/2035.png",width: auto)] ) - *cosh* 计算双曲角的双曲余弦。 #set math.equation(numbering: none) $x$值双曲余弦定义如下: $ (e^x+e^(-x))/2 $ ```typc calc.cosh(value:float) → float ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.cosh(0) \ #calc.cosh(1.5) ``` ],[#image("images/2036.png",width: auto)] ) - *tanh* 计算双曲角的双曲正切。 #set math.equation(numbering: none) $x$值双曲正切定义如下: $ (e^x-e^(-x))/(e^x+e^(-x)) $ ```typc calc.tanh(value:float) → float ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.tanh(0) \ #calc.tanh(1.5) ``` ],[#image("images/2037.png",width: auto)] ) - *asinh* 计算双曲角的反双曲正弦。 $x$值双曲反正弦定义如下: $ ln(x+sqrt(x^2+1)) $ ```typc calc.asinh(value:float) → float ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.asinh(0) \ #calc.asinh(1) ``` ],[#image("images/2038.png",width: auto)] ) - *acosh* 计算双曲角的反双曲余弦。 #set math.equation(numbering: none) $x$值反双曲余弦定义如下: $ ln(x+sqrt(x^2-1)) $ ```typc calc.acosh(value:float) → float ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.acosh(0) \ #calc.acosh(2.5) ``` ],[#image("images/2039.png",width: auto)] ) - *atanh* 计算双曲角的反双曲正切。 #set math.equation(numbering: none) $x$值反双曲正切定义如下: $ 1/2ln((1+x)/(1-x)) $ ```typc calc.atanh(value:float) → float ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.atanh(0) \ #calc.atanh(0.5) ``` ],[#image("images/2040.png",width: auto)] ) - *log* 计算数字的对数。如果未指定底数,则以十为底计算对数。 ```typc calc.log(value:int float,base: float,) → float ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.log(100) ``` ],[#image("images/2041.png",width: auto)] ) - *ln* 计算数字的自然对数。 ```typc calc.ln(value:int float) → float ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.ln(calc.e) ``` ],[#image("images/2042.png",width: auto)] ) - *erf* 一个数的误差函数。 数$x$的误差函数定义为: $ 2/sqrt(pi)integral_0^x e^(-t^2)d t $ ```typc calc.erf(value:float) → float ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.erf(0.2) ``` ],[#image("images/2043.png",width: auto)] ) - *fact* 计算一个数字的阶乘。 ```typc calc.fact(value:int) → int ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.fact(5) ``` ],[#image("images/2044.png",width: auto)] ) - *perm* 计算排列。 $n$个数的$k$排列定义为 $ cases(0 & "如果" k>n,n!/((n-k)!) &"如果" k<=n ) $ ```typc calc.perm(base:int,number:int,) → int ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.perm(5, 3) ``` ],[#image("images/2045.png",width: auto)] ) - *binom* 计算二项式系数。 返回$n$个物体的的$k$种组合数,定义为 $ cases(n!/(k!(n-k)!) &"当" 0<=k<=n "时",0 & "其它" ) $ ```typc calc.binom(n:int,k:int,) → int ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.binom(10, 5) ``` ],[#image("images/2046.png",width: auto)] ) - *gcd* 计算两个整数的最大公约数。 如果整数除法的结果大于最大64位有符号整数,则会出错。 ```typc calc.gcd(a:int,b:int,) → int ``` - *lcm* 计算两个整数的最小公倍数。 ```typc calc.lcm(a:int,b:int,) → int ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.lcm(96, 13) ``` ],[#image("images/2047.png",width: auto)] ) - *floor* 返回不超过一个数的最大整数。 ```typc calc.floor(value:int float decimal) → int ``` #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst #calc.floor(500.1) #assert(calc.floor(3) == 3) #assert(calc.floor(3.14) == 3) #assert(calc.floor(decimal("-3.14")) == -4) ``` ],[#image("images/2048.png",width: auto)] ) - *ceil* 返回不小一个数的最小整数。 ```typc calc.ceil(value:int float decimal) → int ``` #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst #calc.ceil(500.1) #assert(calc.ceil(3) == 3) #assert(calc.ceil(3.14) == 4) #assert(calc.ceil(decimal("-3.14")) == -3) ``` ],[#image("images/2049.png",width: auto)] ) - *trunc* 返回数字的整数部分。 如果该数字已经是整数,则将原封不动地返回。 请注意,此函数将始终返回一个整数,如果生成的浮点数或小数大于该类型的最大64位有符号整数或小于最小值,则将出错。 ```typc calc.trunc(value:int float decimal) → int ``` #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst #calc.trunc(15.9) #assert(calc.trunc(3) == 3) #assert(calc.trunc(-3.7) == -3) #assert(calc.trunc(decimal("8493.12949582390")) == 8493) ``` ],[#image("images/2050.png",width: auto)] ) - *fract* 返回数字的小数部分。 如果该数字是整数,则返回0。 ```typc calc.fract(value:int float decimal) → int float decimal ``` #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst #calc.fract(-3.1) #assert(calc.fract(3) == 0) #assert(calc.fract(decimal("234.23949211")) == decimal("0.23949211")) ``` ],[#image("images/2051.png",width: auto)] ) - *round* 将数字四舍五入到最接近的整数。 半整数从零开始四舍五入。 可选)可以指定小数位数。如果为负,其绝对值将指示小数点前要删除的有效整数位数。 请注意,此函数将返回与操作数相同的类型。也就是说,对浮点数应用舍入将返回一个浮点数,对小数点应用另一个小数点。您可以显式地将此函数的输出转换为int整数,但请注意,如果浮点数或小数大于最大64位有符号整数或小于最小整数,则这种转换将出错。 此外,如果试图四舍五入超过最大或最小整数或小数,此函数可能会出错。如果该数字是浮点数,则这种尝试将导致float.inf或-float.inf分别返回最大值和最小值。 ```typc calc.round(value:int float decimal,digits: int,) → int float decimal ``` -- digits: int 如果为正数,则表示小数位数。 如果为负数,则表示小数点前应删除的有效整数位数。 #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst #calc.round(3.1415, digits: 2) #assert(calc.round(3) == 3) #assert(calc.round(3.14) == 3) #assert(calc.round(3.5) == 4.0) #assert(calc.round(3333.45, digits: -2) == 3300.0) #assert(calc.round(-48953.45, digits: -3) == -49000.0) #assert(calc.round(3333, digits: -2) == 3300) #assert(calc.round(-48953, digits: -3) == -49000) #assert(calc.round(decimal("-6.5")) == decimal("-7")) #assert(calc.round(decimal("7.123456789"), digits: 6) == decimal("7.123457")) #assert(calc.round(decimal("3333.45"), digits: -2) == decimal("3300")) #assert(calc.round(decimal("-48953.45"), digits: -3) == decimal("-49000")) ``` ],[#image("images/2052.png",width: auto)] ) - *clamp* 将一个数字夹在最小值和最大值之间。 ```typc calc.clamp( value:int float decimal, min:int float decimal, max:int float decimal, ) → int float decimal ``` #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst #calc.clamp(5, 0, 4) #assert(calc.clamp(5, 0, 10) == 5) #assert(calc.clamp(5, 6, 10) == 6) #assert(calc.clamp(decimal("5.45"), 2, decimal("45.9")) == decimal("5.45")) #assert(calc.clamp(decimal("5.45"), decimal("6.75"), 12) == decimal("6.75")) ``` ],[#image("images/2053.png",width: auto)] ) - *min* 确定值序列的最小值。 ```typc calc.min(values:..any) → any ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.min(1, -3, -5, 20, 3, 6) \ #calc.min("typst", "is", "cool") ``` ],[#image("images/2054.png",width: auto)] ) - *max* 确定值序列的最大值。 ```typc calc.max(values:..any) → any ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.max(1, -3, -5, 20, 3, 6) \ #calc.max("typst", "is", "cool") ``` ],[#image("images/2055.png",width: auto)] ) - *even* 确定整数是否为偶数。 ```typc calc.even(values:int) → bool ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.even(4) \ #calc.even(5) \ #range(10).filter(calc.even) ``` ],[#image("images/2056.png",width: auto)] ) - *odd* 确定整数是否为奇数。 ```typc calc.odd(values:int) → bool ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.odd(4) \ #calc.odd(5) \ #range(10).filter(calc.odd) ``` ],[#image("images/2057.png",width: auto)] ) - *rem* 计算两个数字的余数。 如果输入的是小数,并且被除数与除数相比幅度太小,则可能会出错。 ```typc calc.rem( dividend:int float decimal, divisor:int float decimal, ) → int float decimal ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #calc.rem(7, 3) \ #calc.rem(7, -3) \ #calc.rem(-7, 3) \ #calc.rem(-7, -3) \ #calc.rem(1.75, 0.5) ``` ],[#image("images/2058.png",width: auto)] ) - *div-euclid* 执行两个数的欧几里得除法。 该计算结果为除法运算结果取整,即找到整数$n$,使得被除数大于等于$n$乘以除数。 如果结果数值超出其类型的最小值或最大值范围,可能会导致错误。 ```typc calc.div-euclid( dividend:int float decimal, divisor:int float decimal, ) → int float decimal ``` #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst #calc.div-euclid(7, 3) \ #calc.div-euclid(7, -3) \ #calc.div-euclid(-7, 3) \ #calc.div-euclid(-7, -3) \ #calc.div-euclid(1.75, 0.5) \ #calc.div-euclid(decimal("1.75"), decimal("0.5")) ``` ],[#image("images/2059.png",width: auto)] ) - *rem-euclid* 此函数用于计算除法的最小非负余数。 警告:由于浮点数舍入误差,当被除数的绝对值远小于除数且被除数为负时,余数可能等于除数的绝对值。此情况仅适用于浮点数输入。 此外,若输入为小数且被除数的绝对值远小于除数,可能会引发错误。 ```typc calc.rem-euclid( dividend:int float decimal, divisor:int float decimal, ) → int float decimal ``` #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst #calc.rem-euclid(7, 3) \ #calc.rem-euclid(7, -3) \ #calc.rem-euclid(-7, 3) \ #calc.rem-euclid(-7, -3) \ #calc.rem-euclid(1.75, 0.5) \ #calc.rem-euclid(decimal("1.75"), decimal("0.5")) ``` ],[#image("images/2060.png",width: auto)] ) - *quo* 计算两个数的商(向下取整除法)。 请注意,此函数始终返回整数,若结果超出64位有符号整数的最大值或最小值范围,则会报错。 ```typc calc.quo( dividend:int float decimal, divisor:int float decimal, ) → int float decimal ``` #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst $ "quo"(a, b) &= floor(a/b) \ "quo"(14, 5) &= #calc.quo(14, 5) \ "quo"(3.46, 0.5) &= #calc.quo(3.46, 0.5) $ ``` ],[#image("images/2061.png",width: auto)] ) - *norm* 计算一个序列值的$p$范数。 $x_1,...,x_n$的$p$范数定义为 $ cases( (sum_(i=1)^n |x_i|^p)^(1/p) &"如果" 0 一段文档内容。 这种类型是Typst的核心。你编写的所有标记和你调用的大多数#link()[函数]都会产生内容值。您可以通过将标记括在方括号中来创建内容值。这也是将内容传递给函数的方式。 === 示例(Example) #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst Type of *Hello!* is #type([*Hello!*]) ``` ],[#image("images/2063.png",width: auto)] ) 内容可以用+运算符添加,#link()[连接在一起]并乘以整数。无论在哪里需要内容,您也可以传递字符串或不传递。 === 表达式 内容由带字段的元素组成。当使用其元素函数构造元素时,您可以将这些字段作为参数提供,当您有内容值时,可以使用#link()[类型访问语法]访问其类型。 某些字段是必需的:在构造元素时必须提供这些字段,因此,通过对该类型内容的字段访问,它们始终可用。必填字段在文档中标记为必填字段。 大多数字段都是可选的:与必填字段一样,它们可以传递给元素函数,为单个元素配置它们。然而,这些也可以用#link()[set规则]进行配置,以将它们应用于范围内的所有元素。可选字段仅在显式传递给元素函数时才可用于字段访问语法,而不是在它们由集合规则产生时。 每个元素都有默认外观。但是,您也可以使用#link()[show规则]完全自定义其外观。show规则传递给元素。它可以访问元素的字段并从中生成任意内容。 在web应用程序中,您可以将鼠标悬停在内容变量上,以查看内容由哪些元素组成以及它们有哪些字段。或者,您可以检查#link()[repr]函数的输出。 === 定义(Definitions) - *func* 内容的元素函数。此函数可用于创建此内容中包含的元素。它可以用于元素的set和show规则。可以与全局函数进行比较,以检查是否具有特定类型的元素。 ```typc self.func() → function ``` - *has* 内容是否具有指定的字段。 ```typc self.has(field:str) → bool ``` - *at* 访问内容上的指定字段。如果字段不存在,则返回默认值;如果未指定默认值,则返回错误。 ```typc self.at(field:str,default: any,) → any ``` - *fields* 返回此内容的类型。 ```typc self.fields() → dictionary ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #rect( width: 10cm, height: 10cm, ).fields() ``` ],[ #set align(horizon) #image("images/2064.png",width: auto) ] ) - *location* 内容的位置。这仅适用于#link()[查询]返回的内容或#link()[show规则]提供的内容,对于其他内容,它将为#rtext()[none]。生成的位置可用于#link()[计数器]、#link()[状态]和#link()[查询]。 ```typc self.location() → none location ``` == 日期时间(datetime) 表示日期、时间或两者的组合。 可以通过使用此类型的构造函数指定自定义日期时间或使用#link()[#btext()[datetime.today]]获取当前日期来创建。 === 示例(Example) #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #let date = datetime( year: 2020, month: 10, day: 4, ) #date.display() \ #date.display( "y:[year repr:last_two]" ) #let time = datetime( hour: 18, minute: 2, second: 23, ) #time.display() \ #time.display( "h:[hour repr:12][period]" ) ``` ],[ #set align(horizon) #image("images/2065.png",width: auto)] ) === 日期时间和持续时间(Datetime and duration) 您可以通过减去两个日期时间来获得#link()[#btext()[持续时间]]: #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst #let first-of-march = datetime(day: 1, month: 3, year: 2024) #let first-of-jan = datetime(day: 1, month: 1, year: 2024) #let distance = first-of-march - first-of-jan #distance.hours() ``` ],[#image("images/2066.png",width: auto)] ) === 格式(Format) 您可以使用#link()[#btext()[显示]]方法指定自定义格式。日期时间的格式是通过为组件提供指定数量的修饰符来指定的。组件表示要显示的日期时间的特定部分,在修饰符的帮助下,您可以定义如何显示该组件。为了显示组件,您可以将组件的名称括在方括号中(例如,[year]将显示年份)。为了添加修饰符,您可以在组件名称后添加一个空格,后跟修饰符的名称、冒号和修饰符的值(例如[month repr:short]将显示月份的缩写)。 成分及其其可能组合如下: - year:显示日期时间的年份。 - base:可以是日历或iso_week。指定年份是基于公历还是ISO周数。 - padding:可以是零、空格或无。指定如何填充年份。 - repr可以是full,在这种情况下显示全年,也可以是last_two,在那种情况下只显示最后两位数字。 - sign:可以是自动的,也可以是强制的。指定何时显示标志。 - month:显示日期时间的月份。 - padding:可以是零、空格或无。指定如何填充月份。 - repr:可以是数字、长或短。指定月份应显示为数字还是单词。不幸的是,在选择单词表示时,它目前只能显示英文版本。未来,计划支持本地化。 - day:显示日期时间的日期。 - padding:可以是零、空格或无。指定如何填充一天。 - week_number:显示日期时间的周数。 - padding:可以是零、空格或无。指定如何填充周数。 - repr:可以是ISO,周日或周一。在ISO的情况下,周数在1到53之间,而其他周数在0到53之间。 - weekday:显示日期的工作日。 - repr可以是长、短、周日或周一。在长和短的情况下,将显示相应的英文名称(与月份相同,目前不支持其他语言)。在周日和周一的情况下,将显示数值(假设周日和周一分别是一周的第一天)。 - one_indexed:可以为true或false。定义一周的数字表示是以0还是1开头。 - hour:显示日期的小时数。 - padding:可以是零、空格或无。指定如何填充小时。 - repr:可以是24或12。更改小时是以24小时还是12小时的格式显示。 - period:一小时的AM/PM部分 - case:可以设置为小写,也可以设置为大写。 - minute: 显示日期的分钟。 - padding:可以是零、空格或无。指定如何填充分钟。 - second: 显示日期的秒。 - padding:可以是零、空格或无。指定如何填充第二个。 有关支持的语法的更多详细信息,#link("https://time-rs.github.io/book/api/format-description.html#components")[#btext()[请参阅此处]]。 请记住,并非所有组件都可以使用。例如,如果您使用```typc datetime(year:2023,month:10,day:13)```创建一个新的日期时间,它将在内部存储为纯日期,这意味着您不能使用小时或分钟等组件,这些组件只适用于具有指定时间的日期时间。 === 构造函数 创建新的日期时间。 您可以通过#link()[#btext()[datetime]]使用年、月、日、小时、分钟和秒指定日期时间。 注意:根据您指定的日期时间的组件,Typst将以以下三种方式之一存储它: #set par(hanging-indent: 1.3cm) -- 如果指定年、月和日,Typst将只存储日期。 -- 如果指定小时、分钟和秒,Typst将只存储一个时间。 -- 如果您指定了年、月、日、小时、分钟和秒,Typst将存储完整的日期时间。 #set par(hanging-indent: 0cm) 默认情况下,display方法将根据存储方式选择不同的格式。 ```typc datetime( year: int, month: int, day: int, hour: int, minute: int, second: int, ) → datetime ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #datetime( year: 2012, month: 8, day: 3, ).display() ) ``` ],[ #set align(horizon) #image("images/2067.png",width: auto)] ) === 定义 - *today* 返回当前日期。 在CLI中,这可以用--create timestamp参数或通过设置SOURCE_DATA_EPOCH环境变量来覆盖。在这两种情况下,该值都应该以UNIX时间戳的形式给出。 ```typc datetime.today(offset:auto int duration ) → datetime ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst Today's date is #datetime.today().display(). ``` ],[ #set align(horizon) #image("images/2068.png",width: auto)] ) - *display* 以指定格式显示日期时间。 根据您是仅定义日期、时间还是两者都定义,默认格式将有所不同。如果您指定了日期,它将是[年]-[月]-[日]。如果您指定了时间,它将是[小时]:[分钟]:[秒]。在日期时间的情况下,它将是[年]-[月]-[日][小时]:[分钟]:[秒]。 有关更多信息,请参阅#link()[格式语法]。 ```typc self.display(pattern:auto str) → str ``` - *year* 如果指定了年份,则为年份;如果没有日期,则为无年份。 ```typc self.year() → none int ``` - *month* 如果指定了月份,则为月份;如果没有日期,则为无月份。 ```typc self.month() → none int ``` - *weekday* 工作日(星期一计为1)或无日期的时间。 ```typc self.weekday() → none int ``` - *day* 如果指定了日期,则指定日期;如果没有日期,则不指定日期。 ```typc self.day() → none int ``` - *hour* 如果指定了小时,则为小时;对于没有时间的日期,则为无小时。 ```typc self.hour() → none int ``` - *minute* 如果指定了分钟,则为分钟;如果没有时间,则为无分钟。 ```typc self.minute() → none int ``` - *second* 如果指定了秒,则为秒;对于没有时间的日期,则为无。 ```typc self.second() → none int ``` - *ordinal* 序号(一年中的第几天),没有日期的时间为无。 ```typc self.ordinal() → none int ``` == 小数(decimal) 定点十进制数类型。 这种类型应用于对以10为基数表示的数字进行精确的算术运算。一个典型的用例是表示货币。 === 示例(Example) #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst Decimal: #(decimal("0.1") + decimal("0.2")) \ Float: #(0.1 + 0.2) ``` ],[#image("images/2069.png",width: auto)] ) === 构造和投射(Construction and casts) 要创建十进制数,请使用#btext()[decimal](字符串)构造函数,例如```typc decimal("3.141592653")```(注意双引号)。此构造函数保留所有给定的小数,前提是它们可以按照下面指定的限制表示(否则会引发错误)。 您还可以使用```typc decimal(int)```构造函数将任何整数转换为十进制,例如```typc decimal(59)```。但是,请注意,虽然支持从#link()[#btext()[浮点数]]构造小数,但这是一种不精确的转换,因此不建议使用。如果Typst检测到通过其构造函数发生了意外的浮点到十进制转换,例如如果写入decimal(3.14)(注意缺少双引号,表示这是意外的浮点转换,因此不精确),将发出警告。建议使用字符串代替常量十进制值(例如decimal(“3.14”))。 通过使用#link()[#btext()[calc.round]]将结果四舍五入到15位,可以略微提高浮点到十进制转换的精度,但这种转换仍然没有精度保证 === 运算(Operations) 基本算术运算支持两个小数以及小数和整数对。 不支持浮点和小数之间的内置操作,以防止意外丢失精度。他们将提出一个错误。 某些计算函数,如三角函数和两个实数之间的幂,也仅支持浮点运算(尽管支持将小数提升为整数指数)。您可以选择使用float(decimal)构造函数进行可能不精确的操作,该构造函数将十进制数转换为浮点数,允许在没有精度保证的情况下进行操作。 === 显示小数(Displaying decimals) 要显示小数,只需将值插入文档即可。要只显示一定数量的数字,请先将小数四舍五入。小数和其他数字的本地化格式尚不支持,但计划在未来使用。 您可以使用#link()[str]构造函数将小数转换为字符串。这样,您可以对显示的表示进行后处理,例如用逗号替换句点(作为对使用逗号的语言进行适当内置本地化的替代)。 === 精度和限制(Precision and limits) 十进制数的有效基数限制为28到29位。这包括小数点前后的数字之和。因此,小数位数较多的数字范围较小。最大和最小十进制数的值分别为79228162514264337593543950335和-79228162514264337593543950 335。与float不同,这种类型不支持无穷大或NaN,因此溢出或欠流操作将引发错误。 小数之间的典型操作,如加法、乘法和整数的#link()[#btext()[幂运算]],由于其定点表示,将非常精确。然而,请注意,在某些边缘情况下,乘法和除法可能无法保留所有数字:虽然它们被认为是精确的,但超过上述限制的数字会被四舍五入并丢失,因此可能会丢失超过最大可表示数字的精度。请注意,不仅在除法时,而且在乘以0到1之间的数字时都可以观察到这种行为,因为这两种操作都会将数字的小数位数推到上述限制之外,从而导致四舍五入。当这两个操作不超过数字限制时,它们是完全精确的。 === 构造函数(Constructor) 将值转换为小数。 建议使用字符串构造小数,或#link()[#btext()[整数]](如果需要)。字符串必须包含格式为“3.14159”的数字(负数为“-3.141519”)。小数被完全保留;如果由于达到有效数字的限制(大约28到29)而无法实现,则会出现错误,因为给定的十进制数无法表示。 虽然此构造函数可以与浮点数一起使用,将它们转换为小数,但不建议这样做,因为这种转换本质上是不精确的。通过编写decimal(1.234)很容易意外地执行这种转换(注意缺少双引号),这就是为什么在这种情况下Typst会发出警告。请在特定情况下写decimal(“1.234”)(初始化常量小数)。还要注意,NaN或无穷大的浮点数不能转换为小数,这将引发错误。 ```typc decimal(value:bool int float decimal str) → decimal ``` == 字典(dictionary) 一个从字符串键到值的映射。 你可以通过将逗号分隔的键值对用括号括起来来构造字典。值的类型不必相同。由于空括号已生成空数组,因此必须使用特殊(:)语法来创建空字典。 字典在概念上与数组相似,但其索引使用字符串而非整数。你可以通过 `.at()` 方法访问和创建字典条目。若已知键的静态值,亦可采用#link()[类型访问符号](`.key`)来获取值。判断键是否存在于字典中时,使用 `in` 关键字。 你可以使用#link()[for循环]遍历字典中的键值对。遍历顺序将按照键值对最初插入/声明的顺序进行。 字典可以通过 `+` 运算符进行#link()[合并和拼接]。还可以使用 `..spread` 运算符将其展开为函数调用或另一个字典#footnote[当展开到字典中时,如果括号之间的所有项目都展开,则必须以(:,如(:..dict,..other_dict)开始容器。否则,容器将被推断为数组,并引发错误。]。在每种情况下,如果某个键出现多次,最后的值将覆盖其他值。 === 示例(Example) #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #let dict = ( name: "Typst", born: 2019, ) #dict.name \ #(dict.launch = 20) #dict.len() \ #dict.keys() \ #dict.values() \ #dict.at("born") \ #dict.insert("city", "Berlin") #("name" in dict) ``` ],[ #set align(horizon) #image("images/2070.png",width: auto)] ) === 构造函数(Constructor) 将值转换为字典。 请注意,此函数仅用于将类似字典的值转换为字典,而不是从单个对创建字典。请改用字典语法(键:值)。另请参阅#link()[array.to-dict],了解如何将数组转换为字典。 ```typc dictionary(value:module) → dictionary ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #dictionary(sys).at("version") ``` ],[ #set align(horizon) #image("images/2071.png",width: auto)] ) === 定义 - *len* 字典中的(键→值)个数。 ```typc self.len() → int ``` - *at* 返回与字典中指定键关联的值。 如果字典中已经存在该键,则可以在作业的左侧使用。如果键不是字典的一部分,则返回默认值;如果未指定默认值,则返回错误。 如果不需要默认值,也可以使用字段语法(例如(key:42).key)访问值。 ```typc self.at( key:str, default: any, ) → any ``` - *insert* 将一对新单词插入词典。如果字典中已经包含此键,则更新该值。 要一次插入多对,您也可以使用+=运算符添加另一个字典。 ```typc self.insert( key:str, value: any, ) → any ``` - *remove* 按键从字典中删除一对并返回值。 ```typc self.remove( key:str, default: any, ) → any ``` - *keys* 按插入顺序以数组形式返回字典的键。 ```typc self.keys( ) → any ``` - *values* 按插入顺序以数组形式返回字典的值。 ```typc self.values( ) → any ``` - *pairs* 以成对数组的形式返回字典的键和值。每对都表示为长度为2的数组。 ```typc self.pairs( ) → any ``` - *filter* 生成一个新字典,只包含给定函数返回true的原始字典中的对。 ```typc self.filter(test:function) → dictionary ``` 基本用法示例: #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #{ (a: 0, b: 1, c: 2) .filter(v => v > 0) } ``` ],[ #set align(horizon) #image("images/2072.png",width: auto)] ) 基于键而不是值进行筛选示例: #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #{ (a: 0, b: 1, c: 2) .pairs() .filter(((k, v)) => k != "a") .to-dict() } ``` ],[ #set align(horizon) #image("images/2073.png",width: auto)] ) - *map* 生成一个新字典,其中键是相同的,但值是用给定的函数转换的。 ```typc self.map(mapper:function) → dictionary ``` #table( columns: (1.2fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #(a: 0, b: 1, c: 2).map(v => v + 1) ``` ],[ #set align(horizon) #image("images/2074.png",width: auto)] ) == 持续时间(duration) 表示正或负的时间跨度。 === 构造函数(Constructor) 创建新的持续时间。 您可以使用周、天、小时、分钟和秒指定持续时间。您还可以通过减去两个#link()[日期时间]来获得持续时间。 ```typst duration( seconds: int, minutes: int, hours: int, days: int, weeks: int, ) → duration ``` === 定义(Definitions) - *seconds* 以秒表示的持续时间。 此函数返回以浮点数表示的以秒为单位的总持续时间,而不是持续时间的秒分量。 ```typc self.seconds() → float ``` - *minutes* 以分钟表示的持续时间。 此函数返回以浮点数表示的总持续时间(以分钟为单位),而不是持续时间的分钟部分。 ```typc self.minutes() → float ``` - *hours* 以小时表示的持续时间。 此函数返回以浮点数表示的以小时为单位的总持续时间,而不是持续时间的小时部分。 ```typc self.hours() → float ``` - *days* 以天表示的持续时间。 此函数返回以浮点数表示的总持续时间(以天为单位),而不是持续时间的天数部分。 ```typc self.days() → float ``` - *weeks* 持续时间以周表示。 此函数返回以周为单位的浮点数表示的总持续时间,而不是持续时间的周部分。 ```typc self.weeks() → float ``` == 估算(eval) 将字符串作为Typst代码进行计算。 此功能仅应作为最后手段使用。 === 示例(Example) #table( columns: (1.5fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #eval("1 + 1") \ #eval("(1, 2, 3, 4)").len() \ #eval("*Markup!*", mode: "markup") \ ``` ],[ #set align(horizon) #image("images/2075.png",width: auto)] ) === 参数(Parameters) ```typc eval( source:str, mode: str, scope: dictionary, ) → any ``` -- *source*: str 要计算的Typst代码字符串。 -- *mode* str 解析字符串的#link()[语法模式]。 #table( align: left, stroke: none, columns: (1fr,2fr), table.header[*类型*][*细节*], table.hline(), [\"markup\"],[作为标记求值,如在Typst文件中。], [\"math\"],[像数学一样计算,就像在方程式中一样。], [\"code\"],[作为代码求值,就像在哈希之后一样。] ) #table( columns: (1.5fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #eval("= Heading", mode: "markup") #eval("1_2^3", mode: "math") ``` ],[ #set align(horizon) #image("images/2076.png",width: auto)] ) -- *scope*: dictionary 可用的定义范围。 #table( columns: (1.5fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #eval("x + 1", scope: (x: 2)) \ #eval( "abc/xyz", mode: "math", scope: ( abc: $a + b + c$, xyz: $x + y + z$, ), ) ``` ],[ #set align(horizon) #image("images/2077.png",width: auto)] ) == 浮点数(float) 浮点数。 实数的有限精度表示。Typst使用64位来存储浮点数。无论在哪里需要浮点数,你也可以传递一个#link()[#btext()[整数]]。 您可以使用此类型的构造函数将值转换为浮点数。 NaN和正无穷分别以float.nan和float.inf的形式提供。 === 示例(Example) #table( columns: (1.5fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #3.14 \ #1e4 \ #(10 / 4) ``` ],[ #set align(horizon) #image("images/2078.png",width: auto)] ) === 构造函数(Constructor) 将值转换为浮点数。 #set par(hanging-indent: 1.3cm) -- 布尔值转换为0.0或1.0。 -- 整数被转换为最接近的64位浮点数。对于绝对值小于```typc calc.pow(2,53)```的整数,这种转换是精确的。 -- 比率除以100%。 -- 字符串以10为基数解析到最接近的64位浮点数。支持指数表示法。 #set par(hanging-indent: 0cm) ```typc float(value:bool int float ratio decimal str) → float ``` #table( columns: (1.5fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #float(false) \ #float(true) \ #float(4) \ #float(40%) \ #float("2.7") \ #float("1e5") ``` ],[ #set align(horizon) #image("images/2079.png",width: auto)] ) === 定义(Definitions) - *is-nan* 检查浮点数是否不是数字。 在IEEE 754中,多个比特模式表示NaN。如果浮点数是这些位模式中的任何一个,则此函数返回true。 ```typc self.is-nan() → bool ``` #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #float.is-nan(0) \ #float.is-nan(1) \ #float.is-nan(float.nan) ``` ],[ #set align(horizon) #image("images/2080.png",width: auto)] ) - *is-infinite* 检查浮点数是否为无穷大。 浮点数可以表示正无穷大和负无穷大。如果浮点数为无穷大,则此函数返回true。 ```typc self.is-infinite() → bool ``` #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #float.is-infinite(0) \ #float.is-infinite(1) \ #float.is-infinite(float.inf) ``` ],[ #set align(horizon) #image("images/2081.png",width: auto)] ) - *signum* 计算浮点数的符号。 -- 如果该数字为正(包括+0.0),则返回1.0。 -- 如果该数字为负(包括-0.0),则返回-1.0。 -- 如果数字是NaN,则返回float.NaN。 ```typc self.signum() → float ``` #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #float.is-infinite(0) \ #float.is-infinite(1) \ #float.is-infinite(float.inf) ``` ],[ #set align(horizon) #image("images/2082.png",width: auto)] ) - *from-bytes* 将字节解释为浮点数。 ```typc float.from-bytes( bytes:bytes, endian: str, ) → float ``` -- bytes bytes 应转换为浮点数的字节。 长度必须为4或8。然后,根据字节的长度,以IEEE 754的binary32(单精度)或binary64(双精度)格式解释字节。 -- endian str 转换的结尾。 #table( columns: (0.5fr,2fr), align: (left,left), stroke: none, table.header[类型][细节], table.hline(), [\"big\"],[大端字节序:最高值字节位于字节的开头。], [\"little\"],[小端字节序:最低值字节位于字节的开头。] ) - *to-bytes* 将浮点数转换为字节。 ```typc self.to-bytes( endian: str, size: int, ) → bytes ``` == 函数(Function) 从参数值到返回值的映射。 您可以通过在函数名后直接用括号括起逗号分隔的函数参数列表来调用函数。此外,您可以在正常参数列表之后向函数传递任意数量的尾随内容块参数。如果正常参数列表变为空,则可以省略。Typst支持位置参数和命名参数。前者由位置和类型标识,而后者则写成name:value。 在数学模式中,函数调用具有特殊行为。有关更多详细信息,请参阅#link()[数学文档]。 === 示例(Example) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst // Call a function. #list([A], [B]) // Named arguments and trailing // content blocks. #enum(start: 2)[A][B] // Version without parentheses. #list[A][B] ``` ],[ #set align(horizon) #image("images/2083.png",width: auto)] ) 函数是Typst的基本构建块。Typst为各种排版任务提供了功能。此外,您编写的标记由函数支持,所有样式都是通过函数实现的。此参考列出了所有可用功能以及如何使用它们。另请参阅有关#link()[set]和#link()[show]规则的文档,以了解在Typst中使用函数的其他方法。 === 基本函数(Element functions) 一些函数与标题或表格等元素相关联。当被调用时,它们会创建各自类型的元素。与普通函数相比,它们还可以用于#link()[set规则]、#link()[show规则]和#link()[选择器]。 === 函数的范围(Function scopes) 函数可以在自己的范围内保存相关定义,类似于模块。例如#link()[assert.eq]或#link()[list.item]。但是,此功能目前仅适用于内置功能。 === 自定义函数(Defining functions) 您可以使用#link()[let绑定]定义自己的函数,该绑定在绑定名称后有一个参数列表。参数列表可以包含强制位置参数、具有默认值的命名参数和#link()[参数接收器]。 函数绑定的右侧是函数体,它可以是块或任何其他表达式。它定义了函数的返回值,并且可以依赖于参数。如果函数体是一个#link()[代码块],则返回值是连接块中每个表达式的值的结果。 在函数体中,return关键字可用于提前退出,并可选择指定返回值。如果没有给出显式返回值,则主体的计算结果为连接返回值之前的所有表达式的结果。 不返回任何有意义值的函数将返回none。文档中没有明确指定此类函数的返回类型。(例如#link()[array.push])。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #let alert(body, fill: red) = { set text(white) set align(center) rect( fill: fill, inset: 8pt, radius: 4pt, [*Warning:\ #body*], ) } #alert[ Danger is imminent! ] #alert(fill: blue)[ KEEP OFF TRACKS ] ``` ],[ #set align(horizon) #image("images/2084.png",width: auto)] ) === 导入函数(Importing functions) 可以使用#rtext()[import]将函数从一个文件(#link()[模块])导入到另一个文件中。例如,假设我们在一个名为foo.typ的文件中定义了前一个示例中的alert函数。我们可以通过编写```typc import "foo.typ":alert```将其导入到另一个文件中。 === 未命名函数(Unnamed functions) 您还可以通过指定参数列表,后跟=>和函数体,在不创建绑定的情况下创建未命名函数。如果您的函数只有一个参数,则参数列表周围的括号是可选的。未命名函数主要用于显示规则,但也可用作其他函数的参数,如#link()[array.position]。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #show "once?": it => [#it #it] once? ``` ],[ #set align(horizon) #image("images/2085.png",width: auto)] ) === 函数纯度说明(Note on function purity) 在Typst中,所有函数都是纯函数。这意味着对于相同的参数,它们总是返回相同的结果。当它们被第二次调用时,它们无法“记住”事物以产生另一种价值。 唯一的例外是内置方法,如#link()[array.push(value)]。这些可以修改它们被调用的值。 === 定义(Definitions) - *with* 返回一个预先应用了给定参数的新函数。 ```typst self.with(arguments:..any) → function ``` - *where* 返回一个选择器,用于筛选属于此函数的元素,这些元素的字段具有给定参数的值。 ```typc self.where(fields:..any) → selector ``` -- fields:any 需要筛选的字段。 #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst #show heading.where(level: 2): set text(blue) = Section == Subsection === Sub-subsection ``` ],[#image("images/2086.png",width: auto)] ) == 整数(int) 整数:正整数、负整数或零。 Typst以64位存储具有#link("https://en.wikipedia.org/wiki/Two%27s_complement")[两个补码]表示的有符号整数。这允许存储数字上至$2^(63)-1$或9223372036854775807,下至$-2^(62)$或$-9223372036854775808$。这些值可以作为```typc int.max```和```typc int.min```访问。 整数也可以以前缀0x、0o或0b开头指定为十六进制、八进制或二进制。 您可以使用此类型的构造函数将值转换为整数。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #(1 + 2) \ #(2 - 5) \ #(3 + 4 < 8) #0xff \ #0o10 \ #0b1001 #(int(3.8) + int("26")) / Max: #int.max / Min: #int.min ``` ],[ #set align(horizon) #image("images/2087.png",width: auto)] ) === 构造函数(Constructor) 将值转换为整数。如果试图解析无效字符串或生成不适合64位有符号整数的整数,则会引发错误。 #set par(hanging-indent: 1.3cm) -- 布尔值转换为0或1。 -- 浮点数和小数被四舍五入到下一个接近零的64位整数。 -- 默认情况下,字符串以10为基数进行解析。 #set par(hanging-indent: 0cm) ```typc int( value:bool int float decimal str, base: int, ) → int ``` #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #int(false) \ #int(true) \ #int(2.7) \ #int(decimal("3.8")) \ #(int("27") + int("4")) \ #int("beef", base: 16) ``` ],[ #set align(horizon) #image("images/2088.png",width: auto)] ) === 定义(Definitions) - *signum* 计算整数的符号。 -- 如果数字为正,则返回1。 -- 如果数字为负,返回-1。 -- 如果数值为零,则返回0。 ```typc self.signum() → int ``` #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #(5).signum() \ #(-5).signum() \ #(0).signum() ``` ],[ #set align(horizon) #image("images/2089.png",width: auto)] ) - *bit-not* 计算整数的按位取反。 就本函数而言,操作数被视为64位带符号整数。 ```typc self.bit-not() → int ``` #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #4.bit-not() \ #(-1).bit-not() ``` ],[ #set align(horizon) #image("images/2090.png",width: auto)] ) - *bit-and* 计算两个整数之间的位与。 出于此函数的目的,操作数被视为64位的有符号整数。 ```typc self.bit-and(rhs:int) → int ``` #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #128.bit-and(192) ``` ],[ #set align(horizon) #image("images/2091.png",width: auto)] ) - *bit-or* 计算两个整数之间的按位或。 出于此函数的目的,操作数被视为64位的有符号整数。 ```typc self.bit-or(rhs:int) → int ``` #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #64.bit-or(32) ``` ],[ #set align(horizon) #image("images/2092.png",width: auto)] ) - *bit-xor* 计算两个整数之间的按位异或。 出于此函数的目的,操作数被视为64位的有符号整数。 ```typc self.bit-xor(rhs:int) → int ``` #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #64.bit-xor(96) ``` ],[ #set align(horizon) #image("images/2093.png",width: auto)] ) - *bit-lshift* 将操作数的位向左移动指定的量。 出于此函数的目的,操作数被视为64位的有符号整数。如果结果太大,无法容纳64位整数,则会发生错误。 ```typc self.bit-lshift(shift:int) → int ``` #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #33.bit-lshift(2) \ #(-1).bit-lshift(3) ``` ],[ #set align(horizon) #image("images/2094.png",width: auto)] ) - *bit-rshift* 将操作数的位向右移动指定的量。 出于此函数的目的,操作数被视为64位的有符号整数。如果结果太大,无法容纳64位整数,则会发生错误。 ```typc self.bit-rshift( shift:int, logical: bool, ) → int ``` -- logical: bool 切换是否应执行逻辑(无符号)右移而不是算术右移。如果这是真的,负操作数将不会保留其符号位,移位后出现在左侧的位将为0。此参数对非负操作数没有影响。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typst #64.bit-rshift(2) \ #(-8).bit-rshift(2) \ #(-8).bit-rshift(2, logical: true) ``` ],[ #set align(horizon) #image("images/2095.png",width: auto)] ) - *from-bytes* 将字节转换为整数。 ```typc int.from-bytes( bytes:bytes, endian: str, signed: bool, ) → int ``` -- bytes: bytes 应转换为整数的字节。 长度必须不超过8,以便结果适合64位有符号整数。 -- endian: str 转换的结尾。 #table( columns: (0.5fr,1.5fr), align: left, stroke: none, table.header[*Variant*][*Details*], table.hline(), [\"big\"],[大端字节序:最高值字节位于字节的开头。], [\"little\"],[小端字节序:最低值字节位于字节的开头。] ) -- signed: bool 是否应将字节视为带符号整数。如果这是真的并且设置了最高有效位,则结果数字将为负。 #table( columns: 1fr, align: left, stroke: none, fill: rgb(250, 250, 250), [ ```typst #int.from-bytes(bytes((0, 0, 0, 0, 0, 0, 0, 1))) \ #int.from-bytes(bytes((1, 0, 0, 0, 0, 0, 0, 0)), endian: "big") ``` ],[ #set align(horizon) #image("images/2096.png",width: auto)] ) - *to-bytes* 将整数转换为字节。 ```typc self.to-bytes( endian: str, size: int, ) → bytes ``` == 标签(label)