> 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/42-step-6-using-variable-fonts-to-add-bold-fonts-to-an-overleaf-project.md).

# 步骤 6：使用可变字体为 Overleaf 项目添加粗体字体

[引言](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)

在第 5 步中，我们使用字重轴值 400 为正体和斜体样式设置了文档主体文本字体 Noto Serif 的中等字重。在这一步中，我们将展示如何配置 Noto Serif，为正常（正体）和斜体样式提供粗体字形。

请确保你打开了在第 5 步中配置的教程项目。

## 关于未定义粗体字形的警告

重新编译当前项目后，LaTeX 会发出这些警告：

![显示 LaTeX 缺少字体警告的图片。](/files/51cfda3bfb792ac18346c61122409c412d44b66c)

这些警告汇总在下表中。请记住，m 会从系列标识符中去掉 *除了* 其中 *两个* 宽度和字重都是 m（中等）。

| **警告编号** | **警告描述**        | **系列标识符** | **字重** | **宽度** | **形状** |
| -------- | --------------- | --------- | ------ | ------ | ------ |
| 1        | 粗字重、中等字宽的正体字形字体 | b         | b      | m      | n      |
| 2        | 粗字重、中等字宽的斜体字形字体 | b         | b      | m      | 斜      |

我们将展示如何通过为正体（正常）和斜体字形配置粗体字形来修复这些警告。

## 使用可变字体的字重轴添加粗体字形

用于粗体正体和粗体斜体文本的字体可以使用 fontspec 的 `FontFace` 键，其格式为

```
FontFace = {⟨series⟩}{⟨shape⟩}{Font = ⟨font name⟩, ⟨features⟩}
```

其中

* `⟨系列⟩` 是 LaTeX 系列标识符（链接到该部分）
* `⟨字形⟩` 是 LaTeX 字形标识符（链接到该部分）
* `⟨字体名称⟩` 是字体文件名或字体名称
* `⟨特性⟩` 包含我们将用来配置可变字体的设置，例如为正体和斜体可变字体设置合适的字重轴值

我们将采用 Noto Serif 的命名实例（LINK）所建议的值，使用 600 的字重轴值为正体和斜体样式配置粗体字形。

### 如何配置粗体、中等字宽的正体字体

我们将使用 `FontFace` 键，具有以下设置

* `⟨系列⟩` = b
* `⟨字形⟩` = n
* `⟨字体名称⟩` = NotoSerif-VariableFont-wdth-wght.ttf
* `⟨特性⟩` = Weight=600

得到

```
FontFace = {b}{n}{Font=NotoSerif-VariableFont-wdth-wght.ttf,Weight=600}
```

### 如何配置粗体、中等字宽的斜体字体

我们将使用 `FontFace` 键，具有以下设置

* `⟨系列⟩` = b
* `⟨字形⟩` = it
* `⟨字体名称⟩` = NotoSerif-Italic-VariableFont-wdth-wght.ttf
* `⟨特性⟩` = Weight=600

得到

```
FontFace = {b}{it}{Font=NotoSerif-Italic-VariableFont-wdth-wght.ttf,Weight=600}
```

## 整合到一起

更新当前项目的 `\setmainfont` 命令，改用以下配置：

```latex
\setmainfont{NotoSerif-VariableFont-wdth-wght.ttf}[
Weight=400,
ItalicFont = NotoSerif-Italic-VariableFont-wdth-wght.ttf,
FontFace = {b}{n}{Font = NotoSerif-VariableFont-wdth-wght.ttf,Weight=600},
FontFace = {b}{it}{Font = NotoSerif-Italic-VariableFont-wdth-wght.ttf,Weight=600}
]
```

重新编译当前项目，看看是否不再有未定义字形警告。

## 附赠功能：免费获得小型大写字母！

在这一节中，我们注意到一个意外的附加福利：fontspec 会自动为支持小型大写字母的字体启用该功能。

### 什么是小型大写字母？

小型大写字母是一种使用与小写字母同等高度的大写字母的文本样式。它们用于包含大量大写单词的文本中的风格化效果，例如技术文本中的首字母缩略词或关键词。

将以下 LaTeX 文本粘贴到当前 Overleaf 项目中，以排版一个比较使用和不使用小型大写字母的示例——请确保你粘贴它 *后添加浮动位置修饰符* `\begin{document}`。LaTeX 的 `\textsc` 命令用于排版小型大写字母文本。

```latex
大量的全大写字母序列可能会让你排版的文本
看起来“排版上不美观”。可以通过
使用小型大写字母来改善其外观，使文本呈现更精致、更加优雅的效果。

\vskip10pt
大量的 \textsc{uppercase} 字母序列可能会让你的
\textsc{typeset} 文本看起来“排版上 \textsc{unsightly}”。
它的 \textsc{appearance} 可以通过使用小型大写字母来改善，
从而使文本呈现更精致、更加 \textsc{elegant} 的外观。
```

此示例生成以下输出：

![比较使用和不使用小型大写字母排版的文本的图片](/files/d82f360449291d151512cc586e981b15a3508dd5)

### 哪些可变字体启用了小型大写字母？

fontspec 会自动为支持它们的字体启用小型大写字母——我们的三种可变字体都提供小型大写字母。将以下 LaTeX 文本粘贴到你的文档中，即可使用这三种字体家族排版小型大写字母：

```latex
\begin{itemize}
\item 主文本字体族（Noto Serif）：\textsc{小型大写字母}
\item 无衬线字体族（Noto Sans）：{\sffamily \textsc{小型大写字母}}
\item 等宽字体族（Roboto Mono）：{\ttfamily \textsc{小型大写字母}}
\end{itemize}
```

此示例生成以下输出：

![显示在 LaTeX 中使用 3 种不同可变字体排版的小型大写字母的图片](/files/36d3a53b22e78026bfa4c10f9aecd0aff6fb8104)

### 那么粗体和斜体的小型大写字母呢？

由于我们已经为 Noto Serif 配置了粗体和斜体字体，因此可以用这些样式排版小型大写字母。要使用 Noto Serif 排版一段小型大写字母，请将以下代码粘贴到最新的 Overleaf 项目中：

```latex
\begin{itemize}
\item 主文本字体族（Noto Serif）：
\begin{itemize}
    \item \textsc{常规小型大写字母}
    \item \textit{\scshape 斜体小型大写字母}
    \item \textbf{\scshape 粗体小型大写字母}
    \item \textbf{\itshape\scshape 粗斜体小型大写字母}
\end{itemize}
\end{itemize}
```

这会产生以下输出：

![显示在 LaTeX 中使用可变字体排版的粗体和斜体小型大写字母的图片](/files/9d9bf27e454e52c3ee81a4b3f2939f77a0c6dfbf)

## 在下一步中

在下一步中，我们将为无衬线字体族（Noto Sans）和等宽字体族（Roboto Mono）配置粗体和斜体字体样式。


---

# 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/42-step-6-using-variable-fonts-to-add-bold-fonts-to-an-overleaf-project.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.
