typst_reference / chp05.typ
孟祥瑞
上传语料:typst_reference
fdbb2bf verified
Raw History Blame Contribute Delete
58.2 kB
#let rtext(body)={ text(fill:red,body) } = 数学库(Math)<math> Typst 拥有专门的#link(<Syntax_Math_mode>)[语法]与库函数来排版数学公式。数学公式可以内嵌在文本中,也可以作为独立块展示。如果公式的开头和结尾至少有一个空格,就会排版成独立块(例如 \$ x^2 \$)。 *变量(Variables)*<math_Variables> 在数学中,单个字母会按原样显示。但多个字母会被识别为变量和函数。若要原样显示多个字母,可以将其放在引号内;若要调用单个字母变量,则可以使用#link(<Scripting_Expressions>)[井号语法]。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ A = pi r^2 $ $ "area" = pi dot "radius"^2 $ $ cal(A) := { x in RR | x "is natural" } $ #let x = 5 $ #x < 17 $ ``` ],[ #set align(horizon) #image("images/5001.png",width: auto) ] ) *符号(Symbols)*<math_Symbols> 数学模式可使用大量符号,例如圆周率、点符号或实数集符号。许多数学符号拥有不同变体。你可以通过给符号添加修饰符来切换不同变体。Typst 还支持诸多简写序列(如 =>),用来对应相关符号。当某个符号存在这类简写时,会在该符号的文档中注明。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ x < y => x gt.eq.not y $ ``` ],[ #set align(horizon) #image("images/5002.png",width: auto) ] ) *换行符(Line Breaks)*<math_Line-Breaks> 公式中也可以包含换行符。每一行可以有一个或多个对齐点(&),系统会按照这些对齐点进行对齐。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ sum_(k=0)^n k &= 1 + ... + n \ &= (n(n+1)) / 2 $ ``` ],[ #set align(horizon) #image("images/5003.png",width: auto) ] ) *函数调用(Function calls)*<math_Function-calls> 数学模式支持不带井号前缀的特殊函数调用。在这类“数学调用”中,参数列表的工作方式和代码里略有不同: #set par(hanging-indent: 1cm) -- 在数学调用内部,Typst 仍然处于“数学模式”。因此你可以直接在其中书写数学表达式,但传入代码表达式需要使用井号语法(字符串除外,字符串可直接在数学语法中使用)。\ -- 支持位置参数、命名参数以及参数展开,但不支持尾部内容块。\ -- 提供用于二维参数列表的额外语法。分号(;)会把前面由逗号分隔的参数合并为一个数组参数。 #set par(hanging-indent: 0cm) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ frac(a^2, 2) $ $ vec(1, 2, delim: "[") $ $ mat(1, 2; 3, 4) $ $ mat(..#range(1, 5).chunks(2)) $ $ lim_x = op("lim", limits: #true)_x $ ``` ],[ #set align(horizon) #image("images/5004.png",width: auto) ] ) 在数学调用中,如果要原样输出逗号或分号,需要用反斜杠进行转义。而冒号只有在紧跟标识符时才会被特殊识别,因此在这类场景下若想原样显示冒号,只需在其前面插入一个空格即可。 以井号开头的函数调用属于普通代码函数调用,不受上述规则影响。 *对齐(Alignment)*<math_Alignment> 当公式中包含多个对齐点(&)时,会生成交替右对齐与左对齐的列块。在下面的示例中,表达式 $(3x + y) / 7$ 为右对齐,而 $= 9$ 为左对齐。“已知”一词同样为左对齐,这是因为 && 连续设置了两个对齐点,使对齐方式发生两次切换。& & 和 && 的作用完全相同。与此同时,“乘以7”为右对齐,因为它前面只有一个 &。每一个对齐点都会简单地在右对齐、左对齐之间切换。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ (3x + y) / 7 &= 9 && "given" \ 3x + y &= 63 & "multiply by 7" \ 3x &= 63 - y && "subtract y" \ x &= 21 - y/3 & "divide by 3" $ ``` ],[ #set align(horizon) #image("images/5005.png",width: auto) ] ) *数学字体(Math fonts)*<math_fonts> 你可以通过如下所示的#link(<Styling-show>)[show-set]规则来设置数学字体。请注意,只有专用的OpenType数学字体才适合排版数学公式。 #table( columns: 1fr, align: left, stroke: none, [ ```typ #show math.equation: set text(font: "Pennstander Math") $ sum_(i in NN) 1 + i $ ``` ], [ #set quote(block: true) #show quote: set align(center) #rect( fill: luma(240), width: 100%, height: auto, quote[ #image("images/5006.png",width: 80%) ] ) ] ) *数学模块(Math module)*<math_module> 所有数学函数都属于math模块,该模块在公式内默认可用。在公式外,需要添加math.前缀才能调用这些函数。 *无障碍性(Accessibility)* 要让数学内容具备可访问性,你必须借助 math.equation 的 alt 参数,用自然语言为公式提供替代描述。更多信息,请参阅#link(<Accessibility_Guide>)[《无障碍指南》]中的文本表示章节。 #table( columns: 1fr, align: left, stroke: none, [ ```typ #math.equation( alt: "d S equals delta q divided by T", block: true, $ dif S = (delta q) / T $, ) ``` ], [ #set quote(block: true) #show quote: set align(center) #rect( fill: luma(240), width: 100%, height: auto, quote[ #image("images/5007.png",width: 80%) ] ) ] ) 未来,Typst 在导出 HTML 和 PDF 2.0 时,会自动让没有替代描述的公式具备可访问性。 == 重音符号(accent)<accent> 为基础字符添加重音符号。 在数学模式中,常用重音符号也可作为命名#link(<symbol>)[符号]直接调用(类似#link(<Function>)[函数]),为某些内容添加重音。 === 示例(Example) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $grave(a) = accent(a, `)$ \ $arrow(a) = accent(a, arrow)$ \ $tilde(a) = accent(a, \u{0303})$ ``` ],[ #set align(horizon) #image("images/5008.png",width: auto) ] ) 参数(Parameters)<accent_Parameters> ```typc accent( base: content, accent: str content, size: relative, dotless: bool, ) → content ``` - *base*: content 重音所作用的基音字符,可由多个字母组成。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $arrow(A B C)$ ``` ],[ #set align(horizon) #image("images/5009.png",width: auto) ] ) - *accent*: str content<accent_accent> 要应用到基底上的重音。 支持的重音包括: #table( columns: (1fr,1fr,1fr), stroke: none, align: left, table.header([Accent],[Name],[Codepoint]), table.hline(), [Grave],[grave],[\`], [Acute],[acute],[´], [Circumflex],[hat],[^], [Tilde],[tilde],[~], [Macron],[macron],[¯], [Dash],[dash],[‾], [Breve],[breve],[˘], [Dot],[dot],[.], [Double dot, Diaeresis],[dot.double, diaer],[¨], [Triple dot],[dot.triple],[\\u{20db}], [Quadruple dot],[dot.quad],[\\u{20dc}], [Circle],[circle],[∘], [Double acute],[acute.double],[˝], [Caron],[caron],[ˇ], [Right arrow],[arrow, ->],[→], [Left arrow],[arrow.l, \<-],[←], [Left/Right arrow],[arrow.l.r],[↔], [Right harpoon],[harpoon],[⇀], [Left harpoon],[harpoon.lt],[↼], ) - *size*: relative<accent_size> 重音符号的尺寸,相对于基线宽度的比值。默认值:```typc 100%+0pt```。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $dash(A, size: #150%)$ ``` ],[ #set align(horizon) #image("images/5010.png",width: auto) ] ) 请注意,生成的重音符号大小可能无法完全符合预期。例如,箭头可以是预定义的短字形,也可以是由字体提供的基础构件(箭头头部+线段)拼接而成的长字形。这两种形式的尺寸无法覆盖全部区间,因此部分中间尺寸的箭头无法生成。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #for i in range(6) { $ arrow(#box( width: 0.4em + 0.3em * i, fill: aqua, height: 0.4em, )) $ } ``` ],[ #set align(horizon) #image("images/5011.png",width: auto) ] ) - *dotless*: bool<accent_dotless> 添加顶部重音符号时,是否移除小写字母i和j上方的点。默认值:```typc true```。 该选项启用dtls OpenType特性。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $hat(dotless: #false, i)$ ``` ],[ #set align(horizon) #image("images/5012.png",width: auto) ] ) == 附属内容(Attach)<Attach> 下标、上标与限界符号。 附属内容可以以下标/上标形式或者限界形式展示。Typst 会根据基础符号自动判断哪种形式更合适,你也可以通过 `scripts` 和 `limits` 函数手动控制。 如果你希望基础符号拉伸以容纳较长的顶部和底部附属内容(例如上方带有文字的箭头),请使用 `stretch` 函数。 === 示例(Example) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ sum_(i=0)^n a_i = 2^(1+i) $ ``` ],[ #set align(horizon) #image("images/5013.png",width: auto) ] ) === 语法(Syntax) 该函数还提供专门的语法,用于在基础符号后添加附着符号:使用下划线(\_)表示下标,也就是底部附着符号;使用脱字符(^)表示上标,也就是顶部附着符号。 === 函数(Functions)<Attach_Functions> 一个可选附件的基。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ attach( Pi, t: alpha, b: beta, tl: 1, tr: 2+3, bl: 4+5, br: 6, ) $ ``` ],[ #set align(horizon) #image("images/5014.png",width: auto) ] ) 如果你想添加重音符号(抑扬符、波浪号、箭头等)而非下标符号或边角附属符号,请改用重音函数。 ```typc math.attach( base: content, t: none content, b: none content, tl: none content, bl: none content, tr: none content, br: none content, ) → content ``` - *base*: content 物体所附着的基。 - *t*: none content 顶部附件会智能放置在右上角或基上方。默认值:```typc none```。 你可以将基座封装在 limits() 或 scripts() 中,以此覆盖智能定位设置。 - *b*: none content 底部附件巧妙放置在右下角或基下方。默认值:```typc none```。 你可以将基座包裹在 limits() 或 scripts() 中,以覆盖该智能定位设置。 - *tl*: none content 位于左上角的附件(基之前)。默认值:```typc none```。 - *bl*: none content 基前侧的左下角连接件。默认值:```typc none```。 - *tr*: none content 基后方的右上角附件。默认值:```typc none```。 - *br*: none content 底之后的右下角附件。默认值:```typc none```。 === 程序脚本(scripts)<Attach_scripts> 强制基将附件以脚本形式显示。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ scripts(sum)_1^2 != sum_1^2 $ ``` ],[ #set align(horizon) #image("images/5015.png",width: auto) ] ) ```typc math.scripts(body:content) → content ``` - *body*: content 脚本所要挂载的基。 === limits<Attach_limits> 强制将基准以附件形式显示为limits。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ limits(A)_1^2 != A_1^2 $ ``` ],[ #set align(horizon) #image("images/5016.png",width: auto) ] ) ```typc math.limits( body: content, inline: bool, ) → content ``` - *body*: content 用于附加到limits的基。 - *inline*: bool 是否也强制在行内公式中使用极限形式。默认值:```typc true```。 在全局应用极限设置时(例如通过显示规则),通常建议关闭该选项。 == 组合数(binom)<binom> 组合数表达式。 === 示例(Example) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ binom(n, k) $ $ binom(n, k_1, k_2, k_3, ..., k_m) $ ``` ],[ #set align(horizon) #image("images/5017.png",width: auto) ] ) === 参数(Parameters) ```typc binom( upper: content, lower: ..content, ) → content ``` - *upper*: content 组合数的上标。 - *lower*: ..content 组合数的下标。 == 取消(cancel)<cancel> 在公式的一部分上方显示一条斜线。 这通常用于表示消去某一项。 === 示例(Example) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ Here, we can simplify: $ (a dot b dot cancel(x)) / cancel(x) $ ``` ],[ #set align(horizon) #image("images/5018.png",width: auto) ] ) === 参数(Parameters) ```typc cancel( body: content, length: relative, inverted: bool, cross: bool, angle: auto angle function, stroke: length color gradient stroke tiling dictionary, ) → content ``` - *body*: content 需要添加线条覆盖的内容。 - *length*: relative 线条的长度,相对于横跨整个元素的对角线长度而言,会被“抵消”。取值为100%时,线条将恰好铺满该元素的对角线。默认值:```typc 100%+0.3em```。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ a + cancel(x, length: #200%) - cancel(x, length: #200%) $ ``` ],[ #set align(horizon) #image("images/5019.png",width: auto) ] ) - *inverted*: bool 是否需要反转取消线(沿Y轴翻转)。在默认角度设置下,反转代表取消线指向左上角,而非右上角。默认值:```typc false```。 - *cross*: bool 是否绘制两条相对的删除线,在元素上形成十字。覆盖反转设置。默认值:```typc false```。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ (a cancel((b + c), inverted: #true)) / cancel(b + c, inverted: #true) $ ``` ],[ #set align(horizon) #image("images/5020.png",width: auto) ] ) - *angle*: auto angle function\ 取消线的旋转角度设置。默认值:```typc auto```。\ - 如果指定一个角度,则该线条相对于y轴顺时针旋转该角度。 - 如果设为#rtext()[auto],线条使用默认角度,即沿着内容框的上升对角线方向。 - 如果传入函数 angle => angle,则线条相对于y轴按该函数返回的角度进行旋转。该函数接收默认角度作为输入参数。 #table( columns: 1fr, align: left, stroke: none, [ ```typ $ cancel(Pi) cancel(Pi, angle: #0deg) cancel(Pi, angle: #45deg) cancel(Pi, angle: #90deg) cancel(1/(1+x), angle: #(a => a + 45deg)) cancel(1/(1+x), angle: #(a => a + 90deg)) $ ``` ], [ #set quote(block: true) #show quote: set align(center) #rect( fill: luma(240), width: 100%, height: auto, quote[ #image("images/5021.png",width: 80%) ] ) ] ) - *stroke*: length color gradient stroke tiling dictionary 如何[添加]删除线。默认值:```typc 0.05em```。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ cancel( sum x, stroke: #( paint: red, thickness: 1.5pt, dash: "dashed", ), ) $ ``` ],[ #set align(horizon) #image("images/5022.png",width: auto) ] ) == 分情况(cases)<cases> 分情况讨论。 不同分支的内容可通过 & 符号对齐。 === 示例(Example) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ f(x, y) := cases( 1 "if" (x dot y)/2 <= 0, 2 "if" x "is even", 3 "if" x in NN, 4 "else", ) $ ``` ],[ #set align(horizon) #image("images/5023.png",width: auto) ] ) === 参数(Parameters) ```typc cases( delim: none str symbol array, reverse: bool, gap: relative, children: content, ) → content ``` - *delim*: none str symbol array 要使用的分隔符。默认值:```typc ("{","}")```。 可以是单个字符,用于指定左分隔符,此时会自动推断出右分隔符。也可以是包含左、右分隔符的数组。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #set math.cases(delim: "[") $ x = cases(1, 2) $ ``` ],[ #set align(horizon) #image("images/5024.png",width: auto) ] ) - *reverse*: bool 是否应当反转案例的方向。默认值:```typc false```。 查看示例 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #set math.cases(reverse: true) $ cases(1, 2) = x $ ``` ],[ #set align(horizon) #image("images/5025.png",width: auto) ] ) - *gap*: relative 分支之间的间隙。默认值:```typc 0%+0.2em```。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #set math.cases(gap: 1em) $ x = cases(1, 2) $ ``` ],[ #set align(horizon) #image("images/5026.png",width: auto) ] ) - *children*: content 不同情况的各个分支。 == 类(class)<class> 强制使用指定的数学类别。 这一功能可将特定符号视作其他类别的符号来处理,例如让某个符号表现得像关系符号。符号的类别决定其排版方式,包括符号周边的间距,以及上下标的默认附着形式。请注意,后者始终可以通过limits与scripts手动改写。 === 示例(Example) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #let loves = math.class( "relation", sym.suit.heart, ) $x loves y and y loves 5$ ``` ],[ #set align(horizon) #image("images/5027.png",width: auto) ] ) === 参数(Parameters) ```typc class( class: str, body: content, ) → content ``` - *class*: str 要应用于内容的类。 #table( columns: (0.5fr,1.5fr), stroke: none, align: left, table.header([Variant],[Details]), table.hline(), [\"normal\"],[非特殊对象的默认类别。], [\"punctuation\"],[标点符号,例如逗号。], [\"opening\"],[起始分隔符,例如 (。], [\"closing\"],[结束分隔符,例如 )。], [\"fence\"],[两侧相同的分隔符,例如 |。], [\"large\"],[像求和符号这样的大型运算符。如果主体是一个单一字符,则此类运算符会在数学轴(分数线所在的位置)上垂直居中,并在显示样式中垂直拉伸。], [\"relation\"],[一种诸如等号 = 或预序符号 prec 的关系符号。], [\"unary\"],[一元运算符,例如非。], [\"binary\"],[类似乘号这样的二元运算符。], [\"vary\"],[一种既可作为一元运算符、也可作为二元运算符的运算符,例如加号+。], ) - *body*: content 应用该类的内容。 == 公式(equation)<equation> 一个数学公式。 可以和文本行内展示,也可以作为独立块展示。若起始美元符号后存在空白字符,且结束美元符号前存在空白字符,则该公式会变为块级公式。 === 示例(Example) #table( columns: 1fr, align: left, stroke: none, [ ```typ #set text(font: "New Computer Modern") Let $a$, $b$, and $c$ be the side lengths of a right-angled triangle. Then, we know that: $ a^2 + b^2 = c^2 $ Prove by induction: $ sum_(k=1)^n k = (n(n+1)) / 2 $ ``` ], [ #set quote(block: true) #show quote: set align(center) #rect( fill: luma(240), width: 100%, height: auto, quote[ #image("images/5028.png",width: 80%) ] ) ] ) === 语法(Syntax) 该函数还有专用语法:在美元符号内编写数学标记,即可生成公式。若在公式首尾添加空白字符,公式将单独成块并水平居中。有关数学语法的更多详情,请参阅#link(<math>)[数学主页]。 === 参数(Parameters) ```typc equation( block: bool, numbering: none str function, number-align: alignment, supplement: none auto content function, alt: none str, body: content, ) → content ``` - *block*: bool<equation_block> 该方程是否以独立块形式显示。默认值:```typc false```。 - *numbering*: none str function<equation_numbering> 如何给块级方程编号。可接收编号格式字符串,或是仅接收单个数字作为参数的函数。默认值:```typc none```。 #table( columns: 1fr, align: left, stroke: none, [ ```typ #set math.equation(numbering: "(1)") We define: $ phi.alt := (1 + sqrt(5)) / 2 $ <ratio> With @ratio, we get: $ F_n = floor(1 / sqrt(5) phi.alt^n) $ ``` ], [ #set quote(block: true) #show quote: set align(center) #rect( fill: luma(240), width: 100%, height: auto, quote[ #image("images/5029.png",width: 80%) ] ) ] ) - *number-align*: alignment<equation_number-align> 公式编号的对齐方式。默认值:```typc end+horizon```。 默认对齐方式为底端+水平居中。水平方向可选用右对齐、左对齐,或是文字阅读方向的起始端与末尾端;垂直方向可选用顶端、水平居中、底端。 #table( columns: 1fr, align: left, stroke: none, [ ```typ #set math.equation(numbering: "(1)", number-align: bottom) We can calculate: $ E &= sqrt(m_0^2 + p^2) \ &approx 125 "GeV" $ ``` ], [ #set quote(block: true) #show quote: set align(center) #rect( fill: luma(240), width: 100%, height: auto, quote[ #image("images/5030.png",width: 80%) ] ) ] ) - *supplement*: none auto content function<equation_supplement> 方程式的补充说明。默认值:```typc auto```。 在引用方程式时,该内容会添加在引用编号之前。 如果指定了函数,会将被引用的方程式传入该函数,并由函数返回内容。 #table( columns: 1fr, align: left, stroke: none, [ ```typ #set math.equation(numbering: "(1)", supplement: [Eq.]) We define: $ phi.alt := (1 + sqrt(5)) / 2 $ <ratio> With @ratio, we get: $ F_n = floor(1 / sqrt(5) phi.alt^n) $ ``` ], [ #set quote(block: true) #show quote: set align(center) #rect( fill: luma(240), width: 100%, height: auto, quote[ #image("images/5031.png",width: 80%) ] ) ] ) - *alt*: none str<equation_alt> 该数学方程式的另一种描述。默认值:```typc none```。 需使用自然语言完整描述方程式,该内容将提供给辅助技术。你可以在无障碍指南的文本表示章节了解更多信息。 #table( columns: 1fr, align: left, stroke: none, [ ```typ #math.equation( alt: "integral from 1 to infinity of a x squared plus b with respect to x", block: true, $ integral_1^oo a x^2 + b dif x $, ) ``` ], [ #set quote(block: true) #show quote: set align(center) #rect( fill: luma(240), width: 100%, height: auto, quote[ #image("images/5032.png",width: 80%) ] ) ] ) - *body*: content<equation_body> 该方程的内容。 == 分数(frac)<frac> 一个数学分数。 === 示例(Example) #table( columns: 1fr, align: left, stroke: none, [ ```typ $ 1/2 < (x+1)/2 $ $ ((x+1)) / 2 = frac(a, b) $ ``` ], [ #set quote(block: true) #show quote: set align(center) #rect( fill: luma(240), width: 100%, height: auto, quote[ #image("images/5033.png",width: 80%) ] ) ] ) === 语法(Syntax) 该函数还有专用语法:使用斜杠可将相邻表达式转换为分数。可通过圆括号将多个原子组合为单个表达式。这类括号不会出现在输出结果中,但你可以多层嵌套括号来保留括号。 === 参数(Parameters) ```typc frac( num: content, denom: content, style: str, ) → content ``` - *num*: content 该分数的分子。 - *denom*: content 该分数的分子。 - *style*: str<frac_style> 分数应该如何排版。默认值:```typc "vertical"```。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ frac(x, y, style: "vertical") $ $ frac(x, y, style: "skewed") $ $ frac(x, y, style: "horizontal") $ ``` ],[ #set align(horizon) #image("images/5034.png",width: auto) ] ) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #set math.frac(style: "skewed") $ a / b $ ``` ],[ #set align(horizon) #image("images/5035.png",width: auto) ] ) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ // Grouping parentheses are removed. #set math.frac(style: "vertical") $ (a + b) / b $ // Grouping parentheses are removed. #set math.frac(style: "skewed") $ (a + b) / b $ // Grouping parentheses are retained. #set math.frac(style: "horizontal") $ (a + b) / b $ ``` ],[ #set align(horizon) #image("images/5036.png",width: auto) ] ) #table( columns: 1fr, align: left, stroke: none, [ ```typ // This changes the style for inline equations only. #show math.equation.where(block: false): set math.frac(style: "horizontal") This $(x-y)/z = 3$ is inline math, and this is block math: $ (x-y)/z = 3 $ ``` ], [ #set quote(block: true) #show quote: set align(center) #rect( fill: luma(240), width: 100%, height: auto, quote[ #image("images/5037.png",width: 80%) ] ) ] ) #table( columns: 1fr, align: left, stroke: none, [ ```typ // Change the default style. #set math.frac(style: "horizontal") // Define a shorthand with the original style. #let frac = math.frac.with(style: "vertical") $ p/q = frac(p, q) $ // The shadowed definition can still be accessed. #assert.eq($p/q$, $std.math.frac(p, q)$) ``` ], [ #set quote(block: true) #show quote: set align(center) #rect( fill: luma(240), width: 100%, height: auto, quote[ #image("images/5038.png",width: 80%) ] ) ] ) #table( columns: (0.5fr,1.5fr), align: left, stroke: none, table.header([Variant],[Details]), table.hline(), [\"vertical\"],[带有分数线的叠式分子与分母。], [\"skewed\"],[分子和分母用斜杠分隔。], [\"horizontal\"],[分子与分母行内排布,且括号不会被合并。], ) == 左/右(Left/Right)<Left_Right> 分隔符匹配。 lr 函数可让你匹配两个分隔符,并根据其包含的内容调整分隔符大小。虽然语法上相匹配的分隔符会自动执行该操作,但 lr 支持匹配任意两个分隔符,并精准控制它们的尺寸。除 lr 函数外,Typst 还提供了其他若干函数,用于生成对应绝对值、向上取整值、向下取整值以及范数的分隔符对。 若想让 Typst 不对某个分隔符进行匹配,从而关闭自动缩放功能,可使用反斜杠对其转义。如果需要完全禁用自动缩放,则使用 set math.lr(size: 1em)。 === 示例(Example) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ [a, b/2] $ $ lr(]sum_(x=1)^n], size: #50%) x $ $ abs((x + y) / 2) $ $ \{ (x / y) \} $ #set math.lr(size: 1em) $ { (a / b), a, b in (0; 1/2] } $ ``` ],[ #set align(horizon) #image("images/5039.png",width: auto) ] ) === 函数(Functions) - lr 缩放分隔符。 虽然默认情况下匹配的分隔符会进行缩放,但此功能也可用于缩放非匹配的分隔符,并更精确地控制分隔符的缩放。 ```typc math.lr( size: relative, body: content, ) → content ``` -- *size*: relative<Left_Right_lr_size> 括号的大小相对于所包裹内容的高度。默认值:```typc 100%+0pt```。 -- *body*: content 定界内容,包括定界符。 - mid<Left_Right_lr_mid> 刻度分隔符会垂直限定到距离最近的外层lr()组。 ```typc math.mid(body: content) → content ``` -- *body*: content 待缩放的内容。 - abs<Left_Right_abs> 计算表达式的绝对值。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ abs(x/2) $ ``` ],[ #set align(horizon) #image("images/5040.png",width: auto) ] ) ```typc math.abs( size: relative, body: content, ) → content ``` -- *size*: relative<Left_Right_abs_size> 括号的尺寸,相对于所包裹内容的高度。 默认值:#link(<Left_Right_lr_size>)[lr.size] 的当前值。 -- *body*: content<Left_Right_abs_body> 待求取绝对值的表达式。 - norm<Left_Right_norm> 计算表达式的范数。 ```typc math.norm( size: relative, body: content, ) → content ``` -- *size*: relative 括号的尺寸,相对于所包裹内容的高度。 默认值:#link(<Left_Right_lr_size>)[lr.size] 的当前值。 -- *body*: content 要求取范数的表达式。 - floor 对表达式向下取整。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ floor(x/2) $ ``` ],[ #set align(horizon) #image("images/5041.png",width: auto) ] ) ```typc math.floor( size: relative, body: content, ) → content ``` -- *size*: relative 括号的尺寸,相对于所包裹内容的高度。 默认值:#link(<Left_Right_lr_size>)[lr.size] 的当前值。 -- *body*: content 待向下取整的表达式。 - ceil 对表达式向上取整。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ ceil(x/2) $ ``` ],[ #set align(horizon) #image("images/5042.png",width: auto) ] ) ```typc math.ceil( size: relative, body: content, ) → content ``` -- *size*: relative 括号的尺寸,相对于所包裹内容的高度。 默认值:#link(<Left_Right_lr_size>)[lr.size] 的当前值。 -- *body*: content 待向上取整的表达式。 - round 对表达式进行四舍五入。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ round(x/2) $ ``` ],[ #set align(horizon) #image("images/5043.png",width: auto) ] ) ```typc math.round( size: relative, body: content, ) → content ``` -- *size*: relative 括号的尺寸,相对于所包裹内容的高度。 默认值:#link(<Left_Right_lr_size>)[lr.size] 的当前值。 -- *body*: content 要进行舍入运算的表达式。 == 矩阵(Matrix)<mat> 一个矩阵。 一行内的元素应以逗号分隔,各行之间则以分号分隔。分号语法会将前面由逗号分隔的参数合并为一个数组。你也可以在数学函数调用中使用这种特殊语法,来定义可接收二维数据的自定义函数。 单元格中的内容可通过align参数设置对齐方式,同一行单元格的内容也可以使用&符号进行对齐。 === 示例(Example) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ mat( 1, 2, ..., 10; 2, 2, ..., 10; dots.v, dots.v, dots.down, dots.v; 10, 10, ..., 10; ) $ ``` ],[ #set align(horizon) #image("images/5044.png",width: auto) ] ) === 参数(Parameters) ) ```typc mat( delim: none str symbol array, align: alignment, augment: none int dictionary, gap: relative, row-gap: relative, column-gap: relative, rows: ..array, ) → content ``` - *delim*: none str symbol array 要使用的分隔符。 可以是一个指定左分隔符的单个字符,此时右分隔符会被推断出来。否则,可以是一个包含左分隔符和右分隔符的数组。默认值:```typc ("{","}")```。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #set math.mat(delim: "[") $ mat(1, 2; 3, 4) $ ``` ],[ #set align(horizon) #image("images/5045.png",width: auto) ] ) - *align*: alignment 每个单元格应采用的水平对齐方式。默认值:```typc center```。 - *augment*: none int dictionary\ 在矩阵中绘制增强线。默认值:```typc none```。 - 无:不绘制任何线。 - 单个数字:在指定列号后绘制一条垂直增强线。负数从末尾开始计数。 - 字典:使用字典可以绘制多条水平和垂直增强线。此外,还可以设置线条的样式。字典可以包含以下键: - hline:绘制水平线的偏移量。例如,偏移量为2表示在矩阵的第2行之后绘制一条水平线。可以传入单个整数用于绘制一条线,或是传入整数数组绘制多条线。和单个数字的规则一样,负数从末尾开始计数。 - vline:绘制竖直线的偏移量。例如,偏移量为2表示在矩阵的第2列之后绘制一条竖直线。可以传入单个整数用于绘制一条线,或是传入整数数组绘制多条线。和单个数字的规则一样,负数从末尾开始计数。 - stroke:线条的描边样式。若设置为auto,则线条粗细为0.05em,且线帽为方形。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #set math.mat(delim: "[") $ mat(1, 2; 3, 4) $ ``` ],[ #set align(horizon) #image("images/5045.png",width: auto) ] ) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ mat(1, 0, 1; 0, 1, 2; augment: #2) $ // Equivalent to: $ mat(1, 0, 1; 0, 1, 2; augment: #(-1)) $ ``` ],[ #set align(horizon) #image("images/5046.png",width: auto) ] ) #table( columns: 1fr, align: left, stroke: none, [ ```typ $ mat(0, 0, 0; 1, 1, 1; augment: #(hline: 1, stroke: 2pt + green)) $ ``` ], [ #set quote(block: true) #show quote: set align(center) #rect( fill: luma(240), width: 100%, height: auto, quote[ #image("images/5047.png",width: 80%) ] ) ] ) - *gap*: relative 行与列之间的间距。默认值:```typc 0%+0pt```。 这是将行间距和列间距设置为相同值的简写方式。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #set math.mat(gap: 1em) $ mat(1, 2; 3, 4) $ ``` ],[ #set align(horizon) #image("images/5048.png",width: auto) ] ) - *row-gap*: relative 行间距。默认值:```typc 0%+0.2em```。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #set math.mat(row-gap: 1em) $ mat(1, 2; 3, 4) $ ``` ],[ #set align(horizon) #image("images/5049.png",width: auto) ] ) - *column-gap*: relative 列之间的间距。默认值:```typc 0%+0.5em```。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #set math.mat(column-gap: 1em) $ mat(1, 2; 3, 4) $ ``` ],[ #set align(horizon) #image("images/5050.png",width: auto) ] ) - *rows*: array 由矩阵各行构成的数组的数组。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #let data = ((1, 2, 3), (4, 5, 6)) #let matrix = math.mat(..data) $ v := matrix $ ``` ],[ #set align(horizon) #image("images/5051.png",width: auto) ] ) == 撇号(primes)<primes> 成组撇号。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ a'''_b = a^'''_b $ ``` ],[ #set align(horizon) #image("images/5052.png",width: auto) ] ) === 语法(Syntax) 该函数有专用语法:使用撇号而非撇标记。它们会自动附着在前一个元素上,将上标提升至下一级。 === 参数(Parameters) ```typc primes(count:int) → content ``` - *count*: int 分组素数的数量 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #math.primes(5) ``` ],[ #set align(horizon) #math.primes(5) ] ) == 根式(roots)<roots> 平方根与非平方根。 === 示例(Example) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ sqrt(3 - 2 sqrt(2)) = sqrt(2) - 1 $ $ root(3, x) $ ``` ],[ #set align(horizon) #image("images/5053.png",width: auto) ] ) === 函数(Functions) - *root*<roots_root> 一般根号。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ root(3, x) $ ``` ],[ #set align(horizon) #image("images/5054.png",width: auto) ] ) ```typc math.root( index: none content, radicand: content, ) → content ``` -- *index*: none content 选取被开方数的哪一个根。默认值:```typc none```。 -- *radicand*: content 待求根的表达式。 - *sqrt* 平方根。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ sqrt(3 - 2 sqrt(2)) = sqrt(2) - 1 $ ``` ],[ #set align(horizon) #image("images/5055.png",width: auto) ] ) ```typc math.sqrt(radicand: content) → content ``` -- *radicand*: content 待求平方根的表达式。 == 尺寸(Sizes)<Sizes> 公式内表达式的强制尺寸样式。 这些函数支持手动设置公式元素的大小,使其效果与行间公式/行内公式一致,或是匹配在根式、上标或下标中使用时的显示效果。 === 函数(Functions) - *display* 数学中的强制显示样式。 这是块方程式的标准大小。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $sum_i x_i/2 = display(sum_i x_i/2)$ ``` ],[ #set align(horizon) #image("images/5056.png",width: auto) ] ) ```typc math.display( body: content, cramped: bool, ) → content ``` -- *body*: content 待确定尺寸的内容。 -- *cramped*: bool 是否对指数设置高度限制,就像普通下标和上标那样。默认值:```typc false```。 - *inline* 数学中的强制行内(文本)样式。 这是行内公式的标准大小。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ sum_i x_i/2 = inline(sum_i x_i/2) $ ``` ],[ #set align(horizon) #image("images/5057.png",width: auto) ] ) ```typc math.inline( body: content, cramped: bool, ) → content ``` -- *body*: content 调整内容尺寸。 -- *cramped*: bool 是否要为指数设置高度限制,就像普通下标和上标那样。默认值:```typc false```。 - *script* 数学中的强制脚本样式。 这是用于幂运算或上下标的小字号。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $sum_i x_i/2 = script(sum_i x_i/2)$ ``` ],[ #set align(horizon) #image("images/5058.png",width: auto) ] ) ```typc math.script( body: content, cramped: bool, ) → content ``` -- *body*: content 调整内容尺寸。 -- *cramped*: bool 是否对指数施加高度限制,就像普通下标和上标那样。默认值:```typc true```。 - *sscript* 数学中的强制二级脚本样式。 这是最小字号,用于二级上下标(脚本的脚本)。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $sum_i x_i/2 = sscript(sum_i x_i/2)$ ``` ],[ #set align(horizon) #image("images/5059.png",width: auto) ] ) ```typc math.sscript( body: content, cramped: bool, ) → content ``` -- *body*: content 调整内容尺寸。 -- *cramped*: bool 是否对指数施加高度限制,就像普通下标和上标那样。默认值:```typc true```。 == 拉伸(stretch)<stretch> 拉伸字形。 该函数还可用于自动拉伸附加符号的基底,使其与上下方的附加符号相适配。 请注意,仅有部分字形支持拉伸,具体哪些字形取决于所使用的数学字体。不过,大多数数学字体在这方面的表现是一致的。 #table( columns: 1fr, align: left, stroke: none, [ ```typ $ H stretch(=)^"define" U + p V $ $ f : X stretch(->>, size: #150%)_"surjective" Y $ $ x stretch(harpoons.ltrb, size: #3em) y stretch(\[, size: #150%) z $ ``` ], [ #set quote(block: true) #show quote: set align(center) #rect( fill: luma(240), width: 100%, height: auto, quote[ #image("images/5060.png",width: 80%) ] ) ] ) === 参数(Parameters) ```typc stretch( body: content, size: relative, ) → content ``` - *body*: content 要拉伸的字形。 - *size*: relative 要拉伸到的尺寸,相对于字形及其附属元素的最大尺寸。默认值:```typc 100%+0pt```。 请注意,最终生成的字形可能无法达到所需的精确尺寸。经过拉伸的字形可以是预定义字形,也可以是由字体提供的基础组件拼接而成的字形。可用尺寸不一定覆盖全部区间。在下方示例中,当尺寸参数从101%增加至200%时,所选用的字形保持不变,因此实际尺寸不会发生变化。 #table( columns: 1fr, align: left, stroke: none, [ ```typ #for size in ( 100%, // short 101%, 200%, // tall 201%, 300%, 400%, 500%, 600%, // taller ) { $stretch(integral, size: #size)$ } ``` ], [ #set quote(block: true) #show quote: set align(center) #rect( fill: luma(240), width: 100%, height: auto, quote[ #image("images/5061.png",width: 80%) ] ) ] ) == 样式(Styles)<math_Styles> 公式内的备选字母字形。 这些函数与文本函数不同,因为数学字体中每个字母都包含多种变体。 === 函数(Functions) - upright<math_Styles_upright> 数学中的正体(非斜体)字体样式。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ upright(A) != A $ ``` ],[ #set align(horizon) #image("images/5062.png",width: auto) ] ) ```typc math.upright(body: content) → content ``` -- *body*: content 需要设置样式的内容。 - *italic* 数学中的斜体字体样式。 对于罗马字母和希腊小写字母,这已经是默认设置。 ```typc math.italic(body: content) → content ``` -- *body*: content 需要设置样式的内容。 - *bold* 数学中的粗体字体样式。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ bold(A) := B^+ $ ``` ],[ #set align(horizon) #image("images/5063.png",width: auto) ] ) ```typc math.bold(body: content) → content ``` -- *body*: content 需要设置样式的内容。 == 文本运算符(op)<op> 方程中的文本运算符 === 示例(Example) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ tan x = (sin x)/(cos x) $ $ op("custom", limits: #true)_(n->oo) n $ ``` ],[ #set align(horizon) #image("images/5064.png",width: auto) ] ) === 预定义运算符(Predefined Operators) Typst 预定义运算符:arccos、arcsin、arctan、arg、cos、cosh、cot、coth、csc、csch、ctg、deg、det、dim、exp、gcd、lcm、hom、id、im、inf、ker、lg、lim、liminf、limsup、ln、log、max、min、mod、Pr、sec、sech、sin、sinc、sinh、sup、tan、tanh、tg 和 tr。 === 参数(Parameters) ```typc op( text: content, limits: bool, ) → content ``` - *text*: content 运算符文本。 - *limits*: bool, 运算符是否应在显示模式中将附件显示为limits项。默认值:```typc false```。 == 低于/高于(Under/Over)<Under_Over> 可在方程式局部的上方或下方添加分隔符。 大括号与方括号还支持在其自身上方或下方添加可选注释。 === 函数(Functions) - *underline* 内容下方的一条水平线。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ underline(1 + 2 + ... + 5) $ ``` ],[ #set align(horizon) #image("images/5065.png",width: auto) ] ) ```typ math.underline(body: content) → content ``` -- *body*: content 线上方的内容。 - *overline* 内容下方的一条水平线。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ overline(1 + 2 + ... + 5) $ ``` ],[ #set align(horizon) #image("images/5066.png",width: auto) ] ) ```typ math.overline(body: content) → content ``` -- *body*: content 线下方的内容。 - *underbrace* 内容下方的一条水平线。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ underbrace(1 + 2 + ... + 5, n + 1 "numbers") $ ``` ],[ #set align(horizon) #image("images/5067.png",width: auto) ] ) ```typ math.underbrace( body: content, annotation: none content, ) → content ``` -- *body*: content 大括号上方的内容。 -- *annotation*: none content 大括号下方的可选内容。默认值:```typc none```。 - *overbrace* 内容下方的一条水平线。 #table( columns: 1fr, align: left, stroke: none, fill: luma(86.27%), [ ```typ $ overbrace(0 + 1 + dots.c + n, n + 1 "numbers") $ ``` ],[ #set align(horizon) #image("images/5068.png",width: auto) ] ) ```typ math.overbrace( body: content, annotation: none content, ) → content ``` -- *body*: content 大括号下方的内容。 -- *annotation*: none content 大括号上方的可选内容。默认值:```typc none```。 - *underbracket* 内容下方的水平方括号,括号下方可附带可选注释。 #table( columns: 1fr, align: left, stroke: none, fill: luma(86.27%), [ ```typ $ underbrace(0 + 1 + dots.c + n, n + 1 "numbers") $ ``` ],[ #set align(horizon) #image("images/5069.png",width: auto) ] ) ```typ math.underbracket( body: content, annotation: none content, ) → content ``` -- *body*: content 方括号上方的内容。 -- *annotation*: none content 方括号下方的可选内容。 - *overbracket* 在内容上方添加水平方括号,可在上方附带可选注释。默认值:```typc none```。 #table( columns: 1fr, align: left, stroke: none, fill: luma(86.27%), [ ```typ $ overbracket(0 + 1 + dots.c + n, n + 1 "numbers") $ ``` ],[ #set align(horizon) #image("images/5070.png",width: auto) ] ) ```typ math.overbracket( body: content, annotation: none content, ) → content ``` -- *body*: content 方括号下方的内容。 -- *annotation*: none content 方括号上方的可选内容。默认值:```typc none```。 - *underparen* 内容下方的水平圆括号,圆括号下方可附带可选注释。 #table( columns: 1fr, align: left, stroke: none, fill: luma(86.27%), [ ```typ $ underparen(0 + 1 + dots.c + n, n + 1 "numbers") $ ``` ],[ #set align(horizon) #image("images/5071.png",width: auto) ] ) ```typ math.underparen( body: content, annotation: none content, ) → content ``` -- *body*: content 圆括号上方的内容。 -- *annotation*: none content 圆括号下方的可选内容。默认值:```typc none```。 - *overparen* 在内容上方添加水平圆括号,可在上方附带可选注释。 #table( columns: 1fr, align: left, stroke: none, fill: luma(86.27%), [ ```typ $ overparen(0 + 1 + dots.c + n, n + 1 "numbers") $ ``` ],[ #set align(horizon) #image("images/5072.png",width: auto) ] ) ```typ math.overparen( body: content, annotation: none content, ) → content ``` -- *body*: content 圆括号下方的内容。 -- *annotation*: none content 圆括号上方的可选内容。默认值:```typc none```。 - *undershell* 内容下方设置横向龟壳形托架,托架下方可附加注释。 #table( columns: 1fr, align: left, stroke: none, fill: luma(86.27%), [ ```typ $ undershell(0 + 1 + dots.c + n, n + 1 "numbers") $ ``` ],[ #set align(horizon) #image("images/5073.png",width: auto) ] ) ```typ math.undershell( body: content, annotation: none content, ) → content ``` -- *body*: content 龟甲支架上方的内容。 -- *annotation*: none content 龟甲支架下方的可选内容。默认值:```typc none```。 - *overshell* 内容上方设置横向龟壳形托架,托架下方可附加注释。 #table( columns: 1fr, align: left, stroke: none, fill: luma(86.27%), [ ```typ $ overshell(0 + 1 + dots.c + n, n + 1 "numbers") $ ``` ],[ #set align(horizon) #image("images/5074.png",width: auto) ] ) ```typ math.overshell( body: content, annotation: none content, ) → content ``` -- *body*: content 龟甲支架下方的内容。 -- *annotation*: none content 龟甲支架上方的可选内容。默认值:```typc none```。 == 变体(Variants)<Variants> 在公式中使用不同的字体。 这些函数与文本函数不同,因为数学字体中的每个字母都包含多种变体。 === 函数(Functions) - *serif*<Variants_serif> 数学中的衬线(罗马)字体样式。 这已经是默认设置。 ```typ math.serif(body: content) → content ``` -- *body*: content 要设置样式的内容。 - *sans*<Variants_sans> 数学中的无衬线字体样式。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ sans(A B C) $ ``` ],[ #set align(horizon) #image("images/5075.png",width: auto) ] ) ```typ math.sans(body: content) → content ``` -- *body*: content 要设置样式的内容。 - *frak*<Variants_frak> 数学中的哥特体字体样式。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ frak(P) $ ``` ],[ #set align(horizon) #image("images/5076.png",width: auto) ] ) ```typ math.frak(body: content) → content ``` -- *body*: content 要设置样式的内容。 - *mono*<Variants_mono> 数学中的等宽字体样式。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ mono(x + y = z) $ ``` ],[ #set align(horizon) #image("images/5077.png",width: auto) ] ) ```typ math.mono(body: content) → content ``` -- *body*: content 要设置样式的内容。 - *bb*<Variants_bb> 数学中的黑板粗体(双线体)字体样式。 对于大写拉丁字母,还可通过形如$NN$和$RR$这类符号使用黑板粗体。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ bb(b) $ $ bb(N) = NN $ $ f: NN -> RR $ ``` ],[ #set align(horizon) #image("images/5078.png",width: auto) ] ) ```typ math.bb(body: content) → content ``` -- *body*: content 要设置样式的内容。 - *cal*<Variants_cal> 数学中的手写花体(宫廷体)字体样式。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ Let $cal(P)$ be the set of ... ``` ],[ #set align(horizon) #image("images/5079.png",width: auto) ] ) ```typ math.cal(body: content) → content ``` -- *body*: content 要设置样式的内容。 - *scr*<Variants_scr> 数学中的手写花体(圆体)字体样式。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $scr(L)$ is not the set of linear maps $cal(L)$. ``` ],[ #set align(horizon) #image("images/5080.png",width: auto) ] ) 字体有两种方式可以支持区分大小写。第一种是使用Unicode变体序列。这在Typst中可以直接使用,但目前只有少数数学字体支持这种方式。 另一种方法是使用字体特性。例如,一种字体可能通过风格集1(ss01)特性提供手写体样式。要在Typst中使用它,你可以像下面的示例一样,定义自己的scr版本。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #let scr(it) = text( stylistic-set: 1, $cal(it)$, ) We establish $cal(P) != scr(P)$. ``` ],[ #set align(horizon) #image("images/5081.png",width: auto) ] ) ```typ math.scr(body: content) → content ``` -- *body*: content 要设置样式的内容。 == 向量(vec)<vec> 列向量。 向量元素中的内容可通过align参数或&符号进行对齐。 该函数用于排版向量分量。若要排版代表向量的符号,通常使用#link(<accent>)[箭头符号]和#link(<math_Styles>)[粗体]。 === 示例(Example) #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ $ vec(a, b, c) dot vec(1, 2, 3) = a + 2b + 3c $ ``` ],[ #set align(horizon) #image("images/5082.png",width: auto) ] ) === 参数(Parameters) ```typc vec( delim: none str symbol array, align: alignment, gap: relative, children: content, ) → content ``` - *delim*: none str symbol array 要使用的分隔符。默认值:```typc ("(",")")```。 可以是单个字符,用于指定左分隔符,此时会自动推断出右分隔符。也可以是包含左、右分隔符的数组。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #set math.vec(delim: "[") $ vec(1, 2) $ ``` ],[ #set align(horizon) #image("images/5083.png",width: auto) ] ) - *align*: alignment 每个元素应具备的水平对齐方式。默认值:```typc center```。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #set math.vec(align: right) $ vec(-1, 1, -1) $ ``` ],[ #set align(horizon) #image("images/5084.png",width: auto) ] ) - *gap*: relative 元素之间的间隙。默认值:```typc 0%+0.2em```。 #table( columns: (1fr,1fr), align: (left,left), stroke: none, fill: (rgb(250, 250, 250),luma(86.27%)), [ ```typ #set math.vec(gap: 1em) $ vec(1, 2) $ ``` ],[ #set align(horizon) #image("images/5085.png",width: auto) ] ) - *children*: content 向量的元素。
#let rtext(body)={
text(fill:red,body)
}
= 数学库(Math)<math>
Typst 拥有专门的#link(<Syntax_Math_mode>)[语法]与库函数来排版数学公式。数学公式可以内嵌在文本中,也可以作为独立块展示。如果公式的开头和结尾至少有一个空格,就会排版成独立块(例如 \$ x^2 \$)。
*变量(Variables)*<math_Variables>
在数学中,单个字母会按原样显示。但多个字母会被识别为变量和函数。若要原样显示多个字母,可以将其放在引号内;若要调用单个字母变量,则可以使用#link(<Scripting_Expressions>)[井号语法]。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
$ A = pi r^2 $
$ "area" = pi dot "radius"^2 $
$ cal(A) :=
{ x in RR | x "is natural" } $
#let x = 5
$ #x < 17 $
```
],[
#set align(horizon)
#image("images/5001.png",width: auto)
]
)
*符号(Symbols)*<math_Symbols>
数学模式可使用大量符号,例如圆周率、点符号或实数集符号。许多数学符号拥有不同变体。你可以通过给符号添加修饰符来切换不同变体。Typst 还支持诸多简写序列(如 =>),用来对应相关符号。当某个符号存在这类简写时,会在该符号的文档中注明。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
$ x < y => x gt.eq.not y $
```
],[
#set align(horizon)
#image("images/5002.png",width: auto)
]
)
*换行符(Line Breaks)*<math_Line-Breaks>
公式中也可以包含换行符。每一行可以有一个或多个对齐点(&),系统会按照这些对齐点进行对齐。
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
$ sum_(k=0)^n k
&= 1 + ... + n \
&= (n(n+1)) / 2 $
```
],[
#set align(horizon)
#image("images/5003.png",width: auto)
]
)
*函数调用(Function calls)*<math_Function-calls>
数学模式支持不带井号前缀的特殊函数调用。在这类“数学调用”中,参数列表的工作方式和代码里略有不同:
#set par(hanging-indent: 1cm)
-- 在数学调用内部,Typst 仍然处于“数学模式”。因此你可以直接在其中书写数学表达式,但传入代码表达式需要使用井号语法(字符串除外,字符串可直接在数学语法中使用)。\
-- 支持位置参数、命名参数以及参数展开,但不支持尾部内容块。\
-- 提供用于二维参数列表的额外语法。分号(;)会把前面由逗号分隔的参数合并为一个数组参数。
#set par(hanging-indent: 0cm)
#table(
columns: (1fr,1fr),
align: (left,left),
stroke: none,
fill: (rgb(250, 250, 250),luma(86.27%)),
[
```typ
$ frac(a^2, 2) $
$ vec(1, 2, delim: "[") $
$ mat(1, 2; 3, 4) $
$ mat(..#range(1, 5).chunks(2)) $
$ lim_x =
op("lim", limits: #true)_x $
```
],[
#set align(horizon)
#image("images/5004.png",width: auto)
]
)
在数学调用中,如果要原样输出逗号或分号,需要用反斜杠进行转义。而冒号只有在紧跟标识符时才会被特殊识别,因此在这类场景下若想原样显示冒号,只需在其前面插入一个空格即可。
以井号开头的函数调用属于普通代码函数调用,不受上述规则影响。
*对齐(Alignment)*<math_Alignment>
当公式中包含多个对齐点(&)时,会生成交替右对齐与左对齐的列块。在下面的示例中,表达式 $(3x + y) / 7$ 为右对齐,而 $=
9$ 为左对齐。“已知”一词同样为左对齐,这是因为 && 连续设置了两个对齐点,使对齐方式发生两次切换。& & 和 &&
的作用完全相同。与此同时,“乘以7”为右对齐,因为它前面只有一个 &。每一个对齐点都会简单地在右对齐、左对齐之间切换。