> 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/43-step-7-using-noto-sans-and-roboto-mono-variable-fonts-with-latex.md).

# 步骤 7：在 LaTeX 中使用 Noto Sans 和 Roboto Mono 可变字体

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

在这一步中，我们为 Noto Sans 和 Roboto Mono 配置粗体、斜体和粗斜体字形，并演示如何用它们排版小型大写字母的粗体、斜体和粗斜体样式。

打开在第 6 步中配置的当前 Overleaf 项目。

## 配置 Noto Sans 和 Roboto Mono

我们希望为 Noto Sans 和 Roberto Mono 启用粗体、斜体和粗斜体字形。这些要求在下表中使用 LaTeX 的标准字体系列和字形标识符进行了总结。请记住，系列标识符中的 m 被去掉了 *除非* 其中 *两个* 宽度和字重都是 m（中等）。

| **字体说明**       | **系列标识符** | **字重** | **宽度** | **形状** |
| -------------- | --------- | ------ | ------ | ------ |
| 粗字重、中等宽度、正体字体  | b         | b      | m      | n      |
| 中等字重、中等宽度、斜体字体 | m         | m      | m      | 斜      |
| 粗字重、中等宽度、斜体字体  | b         | b      | m      | 斜      |

以下各节展示如何配置 Noto Sans 和 Roberto Mono，以使用这些字体系列和字形。第 [整合起来](#putting-it-all-together) 展示了如何使用 `\setsansfont` 和 `\setmonofont` 命令的作用被保存并重新插入输入（作为单 token 列表）

### 为中等和粗体选择字重轴数值

我们将首先选择字重轴数值，以定义 Noto Sans 和 Roberto Mono 的中等字重和粗体实例（教程部分链接）。 [Noto Sans 的字重轴](https://fonts.google.com/specimen/Noto+Sans/tester) 范围为 100 到 900， [Roboto Mono](https://fonts.google.com/specimen/Roboto+Mono/tester) 其范围为 100 到 700，因此建议使用以下字重值：

| **字体名称**    | **中等字重** | **粗体字重** |
| ----------- | -------- | -------- |
| Noto Sans   | 400      | 600      |
| Roboto Mono | 300      | 550      |

### 为 Noto Sans 和 Roboto Mono 配置中等字重的斜体字体

要添加斜体字体，我们使用 fontspec 的 `ItalicFont` 键，其形式为

```
ItalicFont=⟨font name⟩
```

其中 ⟨font name⟩ 是字体名称，或字体文件名。

Overleaf 项目的 `VariableFonts` 文件夹包含我们需要使用的斜体字体文件：

* NotoSans-Italic-VariableFont-wdth-wght.ttf
* RobotoMono-VariableFont-wght.ttf

使用这些文件名以及上面列出的字重轴数值，可以用以下各节所示的命令添加中等字重的斜体。

#### Noto Sans 中等字重正体和斜体字体的设置

对于 Noto Sans，下面的代码将添加到文档导言区（参见该部分 [整合起来](#putting-it-all-together)).

```latex
\setsansfont{NotoSans-VariableFont-wdth-wght.ttf}[
Weight=400,
ItalicFont=NotoSans-Italic-VariableFont-wdth-wght.ttf
]
```

#### Roboto Mono 中等字重正体和斜体字体的设置

对于 Roboto Mono，下面的代码将添加到文档导言区（参见该部分 [整合起来](#putting-it-all-together)).

```latex
\setmonofont{RobotoMono-VariableFont-wght.ttf}[
Weight=300,
ItalicFont=RobotoMono-Italic-VariableFont-wght.ttf
]
```

### 为 Noto Sans 和 Roboto Mono 配置粗体和粗斜体

如第 XXX 步（链接）所示，我们将使用 fontspec 的 `FontFace` 键，其形式如下

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

其中

* `⟨series⟩` 是 LaTeX 的系列标识符（链接到该部分）
* `⟨shape⟩` 是 LaTeX 的字形标识符（链接到该部分）
* `⟨font name⟩` 是字体文件名或字体名称
* `⟨features⟩` 包含我们将用于配置可变字体的设置，例如为宽度轴或字重轴设置数值

#### Noto Sans 粗体和粗斜体字体的设置

对于粗字重的正体，我们有：

* `⟨series⟩` = b
* `⟨shape⟩` = n
* `⟨font name⟩` = NotoSans-VariableFont-wdth-wght.ttf
* `⟨features⟩` = Weight=600

得到

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

对于粗字重的斜体形状：

* `⟨series⟩` = b
* `⟨shape⟩` = it
* `⟨font name⟩` = NotoSans-VariableFont-wdth-wght.ttf
* `⟨features⟩` = Weight=600

得到

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

#### Roboto Mono 粗体和粗斜体字体的设置

对于粗字重的正体，我们有：

* `⟨series⟩` = b
* `⟨shape⟩` = n
* `⟨font name⟩` = RobotoMono-VariableFont-wght.ttf
* `⟨features⟩` = Weight=550

得到

```
FontFace = {b}{n}{Font = RobotoMono-VariableFont-wght.ttf, Weight=550}
```

对于粗字重的斜体形状，我们有：

* `⟨series⟩` = b
* `⟨shape⟩` = it
* `⟨font name⟩` = RobotoMono-VariableFont-wght.ttf
* `⟨features⟩` = Weight=550

得到

```
FontFace = {b}{it}{Font = RobotoMono-VariableFont-wght.ttf, Weight=550}
```

## 整合起来

要应用上面讨论的配置，请打开当前的 Overleaf 项目，并将现有的 `\setsansfont` 和 `\setmonofont` 命令替换为以下版本。

```latex
% Document family sans serif font
\setsansfont{NotoSans-VariableFont-wdth-wght.ttf}[
Weight=400,
ItalicFont=NotoSans-Italic-VariableFont-wdth-wght.ttf,
FontFace = {b}{n}{Font = NotoSans-VariableFont-wdth-wght.ttf, Weight=600},
FontFace = {b}{it}{Font =NotoSans-Italic-VariableFont-wdth-wght.ttf,Weight=600}
]
```

```latex
% Document family monospaced font
\setmonofont{RobotoMono-VariableFont-wght.ttf}[
Weight=400,
ItalicFont=RobotoMono-Italic-VariableFont-wght.ttf,
FontFace = {b}{n}{Font =RobotoMono-VariableFont-wght.ttf, Weight=550},
FontFace = {b}{it}{Font = RobotoMono-Italic-VariableFont-wght.ttf, Weight=550}
]
```

## 如何使用我们的新字体

使用 fontspec，我们已经为文档的所有三种字体族配置了粗体、斜体和粗斜体字体。除了我们已配置的字体之外，fontspec 还启用了小型大写字母版本，如将以下代码粘贴到文档中（在 \begin{document} 之后）所示：

```latex
\begin{itemize}
\item Main text family (Noto Serif):
\begin{itemize}
    \item \textsc{regular small caps}
    \item \textit{\scshape italic small caps}  
    \item \textbf{\scshape bold small caps}
    \item \textbf{\itshape\scshape bold italic small caps}
\end{itemize}
\item Sans serif family (Noto Sans):
\begin{itemize}
    \item \textsc{\sffamily regular small caps}
    \item \textit{\sffamily\scshape italic small caps}  
    \item \textbf{\sffamily\scshape bold small caps}
    \item \textbf{\sffamily\itshape\scshape bold italic small caps}
\end{itemize}
\item Monospaced family (Roboto Mono):
\begin{itemize}
    \item \textsc{\ttfamily regular small caps}
    \item \textit{\ttfamily\scshape italic small caps}  
    \item \textbf{\ttfamily\scshape bold small caps}
    \item \textbf{\ttfamily\itshape\scshape bold italic small caps}
\end{itemize}
```

这段代码会生成以下输出

![显示使用三种文档字体族排版的正常、粗体和粗斜体小型大写字母的图片。](/files/8d5f41dbe260878586a1c298fcdfea90c7829928)

## 在下一步中

我们项目文档的导言区现在因大量 fontspec 配置代码而相当杂乱。在第 8 步中，我们将把 fontspec 设置代码移到一个简单项目中，从而简化文档导言区，并使更新字体更容易。


---

# 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/43-step-7-using-noto-sans-and-roboto-mono-variable-fonts-with-latex.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.
