text
元素
元素函数可以通过 set 和 show 规则进行样式自定义。
以各种方式自定义文本的外观和布局。
该函数经常被使用,无论是 set 规则还是直接使用。虽然 set 规则通常是更简单的选择,但是直接调用 text 函数在将文本作为参数传递给另一个函数时非常有用。
Example
#set text(18pt)
With a set rule.
#emph(text(blue)[
With a function call.
])

参数
参数是函数的输入,它们在函数名称后面的括号中传入。
font
优先使用的字体系列。
当处理文本时,Typst 按顺序尝试所有指定的字体系列,直到找到具有必要字形的字体为止。在下面的示例中,首选字体是 Inria Serif
,但由于它不包含阿拉伯字形,因此阿拉伯文本使用 Noto Sans Arabic
替代。
The collection of available fonts differs by platform:
-
In the web app, you can see the list of available fonts by clicking on the "Ag" button. You can provide additional fonts by uploading
.ttf
or.otf
files into your project. They will be discovered automatically. -
Locally, Typst uses your installed system fonts. In addition, you can use the
--font-path
argument orTYPST_FONT_PATHS
environment variable to add directories that should be scanned for fonts.
默认:"linux libertine"
查看示例
#set text(font: "PT Sans")
This is sans-serif.
#set text(font: (
"Inria Serif",
"Noto Sans Arabic",
))
This is Latin. \
هذا عربي.

fallback
当主要字体列表中没有匹配项时,是否允许最后的备用字体,Typst 会搜索所有可用字体找到具有必要字形的最相似的字体。
注: 当禁用 fallback 并且找不到适当的字形时不会发出警告,而是以“豆腐块”形式显示文本:表示缺少适当字形的小框。将来,你将能够指示 Typst 发出警告,以便你知道出了什么问题。
默认:true
查看示例
#set text(font: "Inria Serif")
هذا عربي
#set text(fallback: false)
هذا عربي

style
所需的字体样式。
当需要 italic 样式但只有 oblique 样式可用时,将使用 oblique ,反之亦然。当 italic 样式和 oblique 样式都不可用时,Typst 会选择 normal 样式。由于大多数字体只有 italic 或 oblique 样式,因此很少能观察到 italic 和 oblique 样式之间的区别。
如果你想强调你的文本,你应该使用 emph 函数。 这样,如果您改变主意如何表示强调,以后就可以轻松调整样式.
-
"normal"
默认的,典型的直立风格。
-
"italic"
带有自定义字母形式的草书风格(意大利斜体)。
-
"oblique"
仅是正常风格的倾斜版本。(倾斜体)
默认:"normal"
查看示例
#text(font: "Linux Libertine", style: "italic")[Italic]
#text(font: "DejaVu Sans", style: "oblique")[Oblique]

weight
所需的字体字重。接受介于 100
和 900
之间的整数或预定义的字重名称之一。当所需的字重不可用时,Typst 会从字体系列中选择最接近字重的字体。
如果要着重强调文本,应使用 emph 函数。这样,如果你改变了强调的方式就很容易调整样式。
-
"thin"
Thin weight (100).
-
"extralight"
Extra light weight (200).
-
"light"
Light weight (300).
-
"regular"
Regular weight (400).
-
"medium"
Medium weight (500).
-
"semibold"
Semibold weight (600).
-
"bold"
Bold weight (700).
-
"extrabold"
Extrabold weight (800).
-
"black"
Black weight (900).
默认:"regular"
查看示例
#set text(font: "IBM Plex Sans")
#text(weight: "light")[Light] \
#text(weight: "regular")[Regular] \
#text(weight: "medium")[Medium] \
#text(weight: 500)[Medium] \
#text(weight: "bold")[Bold]

stretch
所需的字体拉伸宽度。接受介于 50%
和 200%
之间的比率。当所需的字体不可用时,Typst 会从字体系列中选择最接近拉伸的字体。这只会拉伸文本,如果字体的压缩或扩展版本可用,则会选择字体系列中最接近的字体。
如果要调整字符之间的间距而不是拉伸字形本身,请改用 tracking
属性。
默认:100%
查看示例
#text(stretch: 75%)[Condensed] \
#text(stretch: 100%)[Normal]

size
所需的字体大小。该值构成 em
基础单位:1em
等于字体大小。
你也可以直接给出字体大小,单位为 em
。当然,这是相对于前面指定的字体大小。
默认:11pt
查看示例
#set text(size: 20pt)
very #text(1.5em)[big] text

fill
字体填充颜色
默认:luma(0%)
查看示例
#set text(fill: red)
This text is red.

tracking
字符之间的间距。
默认:0pt
查看示例
#set text(tracking: 1.5pt)
Distant text.

spacing
单词之间的间距。
可以给出绝对长度,也可以相对于字体中空格字符的宽度。
如果想调整字符之间的间距而不是单词之间的间距,请改用 tracking
属性。
默认:100%
查看示例
#set text(spacing: 200%)
Text with distant words.

cjk-latin-spacing
Whether to automatically insert spacing between CJK and Latin characters.
默认:auto
查看示例
#set text(cjk-latin-spacing: auto)
第4章介绍了基本的API。
#set text(cjk-latin-spacing: none)
第4章介绍了基本的API。

baseline
文本基准线移动的距离。
默认:0pt
查看示例
A #text(baseline: 3pt)[lowered]
word.

overhang
是否允许某些字形悬挂到边距中。这可以使对齐更加美观。
默认:true
查看示例
#set par(justify: true)
This justified text has a hyphen in
the paragraph's first line. Hanging
the hyphen slightly into the margin
results in a clearer paragraph edge.
#set text(overhang: false)
This justified text has a hyphen in
the paragraph's first line. Hanging
the hyphen slightly into the margin
results in a clearer paragraph edge.

top-edge
文本布局和定位中使用的文本周围概念框的顶端。这会影响包含文本的容器的大小。
-
"ascender"
上行线高度,通常超过所有字形的高度。
-
"cap-height"
大写字母的近似高度。
-
"x-height"
非上行线小写字母 x 的近似高度。
-
"baseline"
字母所依托的基线高度。
-
"bounds"
字形边界框的顶部边缘高度。
默认:"cap-height"
查看示例
#set rect(inset: 0pt)
#set text(size: 20pt)
#set text(top-edge: "ascender")
#rect(fill: aqua)[Typst]
#set text(top-edge: "cap-height")
#rect(fill: aqua)[Typst]

bottom-edge
文本布局和定位中使用的文本周围概念框的底部。这会影响包含文本的容器的大小。
-
"baseline"
字母所依托的基线高度。
-
"descender"
下行线高度,通常超过所有字形的深度。
-
"bounds"
字形边界框的底部边缘高度。
默认:"baseline"
查看示例
#set rect(inset: 0pt)
#set text(size: 20pt)
#set text(bottom-edge: "baseline")
#rect(fill: aqua)[Typst]
#set text(bottom-edge: "descender")
#rect(fill: aqua)[Typst]

lang
设置正确的语言会影响 Typst 的各个部分:
- 文本处理功能可以做出更明智的选择。 The text processing pipeline can make more informed choices.
- 连字符会匹配相应的语言模式。
- 智能引号 会变成响应语言的引号。
- 以及其他所有能感知语言的功能。
默认:"en"
查看示例
#set text(lang: "de")
#outline()
= Einleitung
In diesem Dokument, ...

region
这使文本处理功能能够做出更明智的选择。
默认:none
script
OpenType 编写脚本。
lang
和 script
的组合决定了字体功能(如字形替换)的实现方式。通常,该值是修改后的(全小写)ISO 15924 脚本标识符,数学书写脚本用于适用于数学符号的特征。
当设置为 auto
(默认设置和建议设置)时,将为共享公共 Unicode 脚本属性的每个字符块选择适当的脚本。
默认:auto
查看示例
#set text(
font: "Linux Libertine",
size: 20pt,
)
#let scedilla = [Ş]
#scedilla // S with a cedilla
#set text(lang: "ro", script: "latn")
#scedilla // S with a subscript comma
#set text(lang: "ro", script: "grek")
#scedilla // S with a cedilla

dir
文本和内联对象的主导方向。可能的取值有:
-
auto
:从lang
属性自动推断方向。 ltr
:从左到右布局文本。rtl
:从右到左布局文本。
当使用从右到左的语言(如阿拉伯语或希伯来语)书写时,应设置 text language 或 direction。虽然各个文本行会自动按正确的方向布局,但设置主要方向可为双向重新排序算法提供必要的信息,以正确放置标点符号和内联对象。此外,设置方向会影响对齐值 start
和 end
,在 ltr
文本中等同于 left
和 right
,在 rtl
文本中则相反。
如果设置为 rtl
遇到错误或某种方式产生不佳的输出,请通过 contact table 或我们的 Discord server 与我们联系!
默认:auto
查看示例
#set text(dir: rtl)
هذا عربي.

hyphenate
是否使用连字符改善断行。当设定为 auto
时,只有在启用对齐时才会连字符化文本。
设置 text language 可确保使用正确的连字符化模式。
默认:auto
查看示例
#set page(width: 200pt)
#set par(justify: true)
This text illustrates how
enabling hyphenation can
improve justification.
#set text(hyphenate: false)
This text illustrates how
enabling hyphenation can
improve justification.

kerning
是否应用字距调整。
启用后,特定的字母配对会相互靠近或远离,以获得更美观的结果。下面的示例演示了如何通过减小 "T" 和 "o" 之间的间距来获得更自然的外观。将其设置为 false
会通过关闭 OpenType kern
字体特性来禁用字距调整。
默认:true
查看示例
#set text(size: 25pt)
Totally
#set text(kerning: false)
Totally

alternates
是否应用样式替代。
有时一些字体会包含相同代码点的替代字形。将其设置为 true
会通过启用 OpenType salt
字体特性来切换到这些字形。
默认:false
查看示例
#set text(
font: "IBM Plex Sans",
size: 20pt,
)
0, a, g, ß
#set text(alternates: true)
0, a, g, ß

stylistic-set
应用哪个样式集。字体设计师可以将替代字形形式分类为样式集。由于该值高度依赖于字体,因此你需要查阅字体以了解可用的集合。当设置为介于 1
和 20
之间的整数时,会启用相应的 OpenType 字体特性 ss01
、...、ss20
。
默认:none
ligatures
是否启用标准连字。
某些字母组合(例如 "fi")通常显示为单个合并字形,称为 连字。将其设置为 false
会通过关闭 OpenType liga
和 clig
字体特性来禁用这些连字。
默认:true
查看示例
#set text(size: 20pt)
A fine ligature.
#set text(ligatures: false)
A fine ligature.

discretionary-ligatures
是否启用应当谨慎使用的连字。将其设置为 true
会通过启用 OpenType dlig
字体特性来启用这些连字。
默认:false
historical-ligatures
是否启用历史连字。将其设置为 true
会通过启用 OpenType hlig
字体特性来启用这些连字。
默认:false
number-type
选择哪种数字字体。当设置为 auto
时,使用字体的默认数字。
-
"lining"
适合大写文本的数字(OpenType
lnum
字体功能)。 -
"old-style"
非常适合大写和小写文本流的数字(OpenType
onum
字体功能)。
默认:auto
查看示例
#set text(font: "Noto Sans", 20pt)
#set text(number-type: "lining")
Number 9.
#set text(number-type: "old-style")
Number 9.

number-width
数字字符的宽度。当设置为 auto
时,使用字体的默认数字。
-
"proportional"
具有特定于字形宽度的数字(OpenType
pnum
字体功能)。 -
"tabular"
等宽的数字(OpenType
tnum
字体功能)。
默认:auto
查看示例
#set text(font: "Noto Sans", 20pt)
#set text(number-width: "proportional")
A 12 B 34. \
A 56 B 78.
#set text(number-width: "tabular")
A 12 B 34. \
A 56 B 78.

slashed-zero
是否使用带斜线的零字形。将其设置为 true
会通过启用 OpenType zero
字体特性来使用这些字形。
默认:false
查看示例
0, #text(slashed-zero: true)[0]

fractions
是否将数字转换为分数。将其设置为 true
会通过启用 OpenType frac
字体特性来使用这些字形。
不建议全局启用此属性,因为它会破坏斜杠后所有数字的外观(例如,在 URL 中)。相反,当你想要分数时,请在本地启用它。
默认:false
查看示例
1/2 \
#text(fractions: true)[1/2]

features
要使用的原始 OpenType 功能。
- 如果给定字符串数组,则将字符串标识的功能设置为
1
。 - 如果给定映射到数字的字典,则将键标识的功能设置为值。
默认:(:)
查看示例
// Enable the `frac` feature manually.
#set text(features: ("frac",))
1/2

body
要应用样式的内容,根据其他参数进行设定。
text
该文本。