> For the complete documentation index, see [llms.txt](https://ayakaleaf-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://ayakaleaf-pro.ayaka.space/latex/zh-cn/shen-du-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}
% Document family sans serif font
\setsansfont{NotoSans-VariableFont-wdth-wght.ttf}
% Document family monospaced font
\setmonofont{RobotoMono-VariableFont-wght.ttf}
```

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

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

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

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

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

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

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

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

### 这些警告的结构

每条警告的结构为

``字体形状 `*encoding/family name/series/shape*' 未定义``

如下图所示：

![显示 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://ayakaleaf-pro.ayaka.space/latex/zh-cn/shen-du-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.
