Skip to content

如何修改公式里的中文字体?

如何修改公式里的中文字体?

若不专门设置,公式中的中文通常是楷体:

typst
$ hat(alpha)(f) = f(alpha) "(同上,α–map的“定义”)"$
$ f(alpha) #[或者*任意*内容 _α–map_ $alpha$–map] $
$ (1,...,n) dot bold(v) + dots.c + f(dot.c) = cases("Math" 1 I l, "正文 1Il") $
Typst compiled image

Typst 0.14

配置正文字体后,请按以下任一方法继续设置数学公式的字体。

“引号”泛指中西共用标点

下文所谓“引号”并不专指单双引号,而泛指中西共用标点,具体包括:

  • Latin-1 Supplement:
    • U+00B7 · MIDDLE DOT, $dot.c$ (≠ $dot$ = $dot.op$)
  • General Punctuation:
    • U+2013 EN DASH
    • U+2014 EM DASH
    • U+2018 LEFT SINGLE QUOTATION MARK
    • U+2019 RIGHT SINGLE QUOTATION MARK
    • U+201C LEFT DOUBLE QUOTATION MARK
    • U+201D RIGHT DOUBLE QUOTATION MARK
    • U+2025 TWO DOT LEADER
    • U+2026 HORIZONTAL ELLIPSIS, $...$ (= $dots.h$$dots.c$ = $dots.h.c$)
    • U+2027 HYPHENATION POINT
  • Supplemental Punctuation:
    • U+2E3A TWO-EM DASH

参考Typst 源代码中的 Covers::LatinInCjk

若引号想用数学字体(数学、西文字体统一)

此法最简洁,且对数学部分干扰最小,推荐使用。

typst
#show math.equation: set text(font: (
  "New Computer Modern Math", // 数学
  "Source Han Serif SC", // 中文
))
Typst compiled image

若引号想用中文字体(数学、西文字体统一)

有两种思路。第一种在数学字体前单独选择要使用的标点。这里没有选择间隔号·与省略号……,因为它们也可能用作数学符号。

typst
#show math.equation: set text(font: (
  (name: "Source Han Serif SC", covers: regex("[–—‘’“”‥‧⸺]")), // 中文
  "New Computer Modern Math", // 数学
  "Source Han Serif SC", // 中文
))
Typst compiled image

第二种从数学字体排除所有中西共用标点。

typst
#show math.equation: set text(font: (
  (name: "New Computer Modern Math", covers: "latin-in-cjk"), // 数学
  (name: "Source Han Serif SC", covers: regex(".")), // 中文
  "New Computer Modern Math", // 数学
))
Typst compiled image

TIP

Typst 0.14 会将首个没有covers的字体用作数学基准字体,从中提取间距等数学排版信息。因此,中文字体若在数学字体之前,必须设置covers: regex("."),不然中文字体会被误当做数学基准字体,导致数学间距异常,并让你满篇文章都有警告。

其它方法(数学、西文字体分开)

如果您真的需要,也可以分开设置数学和西文字体。不过请注意以下问题。

  1. 数学公式中,文本($"x"$$#[x]$)和正体变量($upright(x)$)使用相同字符,无法分开设置字体。以下几种方法中,它们都会变成西文字体。

    类似地,数字$1$$"1"$也无法区分。以下几种方法中,它们仍使用数学字体。

  2. 数学公式中有些普通字符需要数学特性。例如cases的大括号,就字符而言只是普通的{,而排版时需随内容拉伸。西文字体虽然也提供这些字符,但不支持数学,无法使用。

    因此,西文字体的covers不能使用latin-in-cjkregex("."),必须换成范围更小的正则表达式。

具体设置方法如下。

如果引号想用中文字体
typst
#show math.equation: set text(font: (
  (name: "Libertinus Serif", covers: regex("[.\d\p{Latin}]")), // 西文
  (name: "Source Han Serif SC", covers: regex("[·–—‘’“”‥…‧⸺]")), // 中文
  "New Computer Modern Math", // 数学
  "Source Han Serif SC", // 中文
))
Typst compiled image
如果引号想用西文字体
typst
#show math.equation: set text(font: (
  (name: "Libertinus Serif", covers: regex("[.\d\p{Latin}·–—‘’“”‥…‧⸺]")), // 西文
  "New Computer Modern Math", // 数学
  "Source Han Serif SC", // 中文
))
Typst compiled image
如果引号想用数学字体
typst
#show math.equation: set text(font: (
  (name: "Libertinus Serif", covers: regex("[.\d\p{Latin}]")), // 西文
  "New Computer Modern Math", // 数学
  "Source Han Serif SC", // 中文
))
Typst compiled image

Typst 0.13

#5305 目前可以对不同字符分别设置不同的字体。

配置正文字体后,请继续设置数学公式的字体:

typst
#show math.equation: set text(font: (
  (name: "Libertinus Serif", covers: "latin-in-cjk"), // 西文
  "Source Han Serif SC", // 中文
  "New Computer Modern Math", // 数学
))
Typst compiled image

并非最新版

上例使用 v0.13.1 编译,可能不适用于最新版。

  1. 西文字体,负责 123abc,"!、{}()

    Libertinus Serif 是 typst 内置的字体,可换为其它西文字体。

    注意此处不建议设为 New Computer Modern,不然会导致大括号无法伸缩等问题。

  2. 中文字体,负责汉字和,“”!

    Source Han Serif SC 可换为其它中文字体。

  3. 数学字体,负责公式中的复杂排版

    New Computer Modern Math 是 typst 内置的字体,可换为其它支持 OpenType 数学特性的字体。

若计划中西共用同一字体

可合并前两项,例如:

typst
#show math.equation: set text(font: (
  "Source Han Serif SC",
  "New Computer Modern Math",
))
Typst compiled image

并非最新版

上例使用 v0.13.1 编译,可能不适用于最新版。

注意这里第一处_α–map_没有倾斜,因为 Source Han Serif SC 缺少相应字形。

此外,不建议设置 #show math.equation: set text(fallback: false)

Typst 0.12

如果你使用旧版本,请使用以下旧方案。

使用 regex("\p{script=Han}") 匹配中文。

typst
#show math.equation: it => {
  show regex("\p{script=Han}"): set text(font: "Source Han Serif SC")
  it
}
Typst compiled image

这种方法不会设置()等中文独占标点的字体(它们仍然随机回落),而且在使用后难以覆盖,请谨慎使用。

另请参见

贡献者

The avatar of contributor named as Y.D.X. Y.D.X.The avatar of contributor named as Copilot Copilot
The avatar of contributor named as 梦飞翔 梦飞翔
The avatar of contributor named as Yiheng Wu Yiheng Wu
The avatar of contributor named as QuadnucYard QuadnucYard

页面历史

Comments
  • Latest
  • Oldest
  • Hottest

基于 MIT 许可发布