> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/zh-cn/shen-ru-wen-zhang/39-step-3-replacing-latex-s-default-fonts-with-variable-fonts.md).

# 步骤 3：用可变字体替换 LaTeX 的默认字体

[引言](https://www.overleaf.com/learn/latex/Articles/How_to_use_OpenType_variable_fonts_with_LaTeX?preview=true) [步骤 1](https://www.overleaf.com/learn/latex/Articles/Step_1:_Setting_up_an_Overleaf_project_to_use_variable_fonts?preview=true) [步骤 2](https://www.overleaf.com/learn/latex/Articles/Step_2:_An_introduction_to_LaTeX_fonts?preview=true) [步骤 3](https://www.overleaf.com/learn/latex/Articles/Step_3:_Replacing_LaTeX’s_default_fonts_with_variable_fonts?preview=true) [步骤 4](https://www.overleaf.com/learn/latex/Articles/Step_4:_How_to_configure_an_italic_variable_font_using_fontspec?preview=true) [步骤 5](https://www.overleaf.com/learn/latex/Articles/Step_5:_LaTeX_font_weights_and_named_instances_of_variable_fonts?preview=true) [步骤 6](https://www.overleaf.com/learn/latex/Articles/Step_6:_Using_variable_fonts_to_add_bold_fonts_to_an_Overleaf_project?preview=true) [步骤 7](https://www.overleaf.com/learn/latex/Articles/Step_7:_Using_Noto_Sans_and_Roboto_Mono_variable_fonts_with_LaTeX?preview=true) [步骤 8](https://www.overleaf.com/learn/latex/Articles/Step_8:_How_to_create_a_simple_LaTeX_package_to_configure_your_variable_fonts?preview=true) [示例与项目](https://www.overleaf.com/learn/latex/Articles/Overleaf_projects_showing_how_to_use_variable_fonts_with_LaTeX?preview=true)

在这一步中，我们介绍 fontspec 宏包中的三个命令，它们将用于用可变字体替换 LaTeX 的默认字体。

## 使用 fontspec 更改 LaTeX 的默认字体

我们将使用以下 fontspec 宏包命令，将 LaTeX 的默认字体替换为我们选择的可变字体：

* `\setmainfont{⟨font⟩}[⟨font features⟩]` 设置用于正文文本的字体。
* `\setsansfont{⟨font⟩}[⟨font features⟩]` 设置无衬线字体。
* `\setmonofont{⟨font⟩}[⟨font features⟩]` 设置等宽字体。

这些命令各由两个部分组成：

* `⟨font⟩`：必选参数，放在花括号中 `{...}`，用于指定我们要使用的字体，可写作字体名称或字体文件名。
* `⟨font features⟩`：可选参数，放在方括号中 `[...]`，用于启用字体特定功能，包括配置可变字体。

这些命令稍后将在本教程中更详细地解释。

## 如何激活教程项目的可变字体

按照以下步骤激活并使用该项目的可变字体。

1. 打开初始教程项目，并通过将以下代码粘贴到文档导言区来更新它——把它粘贴在下面这一行之后 `\usepackage{fontspec}`.

```latex
% Document family main text font
\setmainfont{NotoSerif-VariableFont-wdth-wght.ttf}
% 文档无衬线字体
\setsansfont{NotoSans-VariableFont-wdth-wght.ttf}
% 文档等宽字体
\setmonofont{RobotoMono-VariableFont-wght.ttf}
```

![显示在文档导言区粘贴 LaTeX 代码位置的图片](/files/cef89182107ecf139b5c9279ab58ceec10bcffc3)

4. 重新编译更新后的项目，使其使用可变字体排版。你应该会注意到以下问题：

* 排版后的文档不再包含任何粗体或斜体文本。
* 有三条“未定义字体形状”警告：

![显示 LaTeX 缺失字体警告的图片。](/files/52836b5606d9614aa37737549e5393c9f74cb95f)

## 为什么我会看到“未定义形状”警告？

我们对可变字体的初始设置是 *非常* 基础的，只启用了直立、中等字重的字体。因此，LaTeX 会发出“未定义形状”警告，提醒你它已无法访问用于排版文档中粗体、粗斜体和常规斜体文本的字体。

## 理解 LaTeX 的“未定义形状”警告

如第 2 步所述，LaTeX 会根据五个属性对每种文本字体进行分类：编码、字体族、系列、形状和大小。如果你尝试使用一种字体，而其属性组合——编码、字体族、系列和形状——不对应于已知的“真实”字体，那么 LaTeX 就会发出“缺少形状”警告。通过将字体配置为所需属性，可以避免这些警告。

### 这些警告的结构

每条警告的格式为

``字体形状 `*编码/族名/系列/形状*' 未定义``

如这张图所示：

![显示 LaTeX 缺失形状警告结构的图片](/files/dde8dbbe52e9ea34c376422c911fd3c7e0654c64)

### 这些警告在告诉我们什么？

下面将解释以下每条警告。

![显示 LaTeX 缺失字体警告的图片。](/files/52836b5606d9614aa37737549e5393c9f74cb95f)

1. **``字体形状 `TU/NotoSerif-VariableFont-wdth-wght.ttf(0)/b/n' 未定义``**

LaTeX 无法访问具有以下属性组合的字体：

* *编码*: **`TU`** （Unicode）。
* *族*: `**NotoSerif-VariableFont-wdth-wght.ttf(0)**`——在我们使用 \setmainfont 命令时由 fontspec 宏包创建的一个较长的字体族名称。我们会在本系列后面探讨字体族名称。
* *系列*: **`b`** → **b**粗体 *重量*, **m**中等 *width* （该 **m** 被省略）。
* 形状： **`n`** → **n**正常（直立） *形状。*

LaTeX 缺少一个 **b**粗体字重， **m**中等宽度（系列），以及 **n**正常（直立）形状的字体，位于 `NotoSerif-VariableFont-wdth-wght.ttf(0)` 字体族。

4. **``字体形状 `TU/NotoSerif-VariableFont-wdth-wght.ttf(0)/b/it' 未定义``**

LaTeX 无法访问具有以下属性组合的字体：

* *编码*: **`TU`** （Unicode）。
* *族*: `**NotoSerif-VariableFont-wdth-wght.ttf(0)**`*.*
* *系列*: `**b**` → **b**粗体字重， **m**中等宽度（该 **m** 被省略）。
* *字形*：it → 斜体形状。

LaTeX 缺少一个 **b**粗体字重， **m**中等宽度（系列） **it**斜体（形状）字体，位于 `NotoSerif-VariableFont-wdth-wght.ttf(0)` 字体族。

8. ``**字体形状 `TU/NotoSerif-VariableFont-wdth-wght.ttf(0)/m/it' 未定义**``

LaTeX 无法访问具有以下参数组合的字体：

* 编码：TU（Unicode）
* 字体族：NotoSerif-VariableFont-wdth-wght.ttf(0)
* 系列：m → 中等字重、中等宽度（注意这里只用了一个 m）
* 形状：it → 斜体形状

LaTeX 缺少在 NotoSerif-VariableFont-wdth-wght.ttf(0) 字体族中具有中等字重、中等宽度（系列）的斜体（形状）字体。

## 在下一步中

在第 4 到第 6 步中，我们将通过配置可变字体以提供缺失的字体形状来修复这些警告。

[引言](https://www.overleaf.com/learn/latex/Articles/How_to_use_OpenType_variable_fonts_with_LaTeX?preview=true) [步骤 1](https://www.overleaf.com/learn/latex/Articles/Step_1:_Setting_up_an_Overleaf_project_to_use_variable_fonts?preview=true) [步骤 2](https://www.overleaf.com/learn/latex/Articles/Step_2:_An_introduction_to_LaTeX_fonts?preview=true) [步骤 3](https://www.overleaf.com/learn/latex/Articles/Step_3:_Replacing_LaTeX’s_default_fonts_with_variable_fonts?preview=true) [步骤 4](https://www.overleaf.com/learn/latex/Articles/Step_4:_How_to_configure_an_italic_variable_font_using_fontspec?preview=true) [步骤 5](https://www.overleaf.com/learn/latex/Articles/Step_5:_LaTeX_font_weights_and_named_instances_of_variable_fonts?preview=true) [步骤 6](https://www.overleaf.com/learn/latex/Articles/Step_6:_Using_variable_fonts_to_add_bold_fonts_to_an_Overleaf_project?preview=true) [步骤 7](https://www.overleaf.com/learn/latex/Articles/Step_7:_Using_Noto_Sans_and_Roboto_Mono_variable_fonts_with_LaTeX?preview=true) [步骤 8](https://www.overleaf.com/learn/latex/Articles/Step_8:_How_to_create_a_simple_LaTeX_package_to_configure_your_variable_fonts?preview=true) [示例与项目](https://www.overleaf.com/learn/latex/Articles/Overleaf_projects_showing_how_to_use_variable_fonts_with_LaTeX?preview=true)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/zh-cn/shen-ru-wen-zhang/39-step-3-replacing-latex-s-default-fonts-with-variable-fonts.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
