Skip to content

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

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

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

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

Typst 0.14

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

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

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("."),不然中文字体会被误当做数学基准字体,导致数学间距异常,并让你满篇文章都有警告。

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

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

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

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

  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("\p{Latin}")), // 西文
  (name: "Source Han Serif SC", covers: regex("[\p{Han}\u{00B7}\u{2013}\u{2014}\u{2018}\u{2019}\u{201C}\u{201D}\u{2025}-\u{2027}\u{2E3A}]")), // 中文
  "New Computer Modern Math", // 数学
))
Typst compiled image

以上这一长串正则表达式源于 Typst 源代码中的 Covers::LatinInCjk

如果引号想用西文字体
typst
#show math.equation: set text(font: (
  (name: "Libertinus Serif", covers: regex("[\p{Latin}\u{00B7}\u{2013}\u{2014}\u{2018}\u{2019}\u{201C}\u{201D}\u{2025}-\u{2027}\u{2E3A}]")), // 西文
  (name: "Source Han Serif SC", covers: regex("\p{Han}")), // 中文
  "New Computer Modern Math", // 数学
))
Typst compiled image

以上这一长串正则表达式源于 Typst 源代码中的 Covers::LatinInCjk

如果引号想用数学字体
typst
#show math.equation: set text(font: (
  (name: "Libertinus Serif", covers: regex("\p{Latin}")), // 西文
  (name: "Source Han Serif SC", covers: regex("\p{Han}")), // 中文
  "New Computer Modern Math", // 数学
))
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

并非最新版

上例使用 typst 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

并非最新版

上例使用 typst 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

这种方法在使用后难以覆盖,谨慎使用。

What do you think?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
Comments
  • Latest
  • Oldest
  • Hottest
Powered by Waline v3.5.1

基于 MIT 许可发布