> 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/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` 命令。

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

我们先选择字重轴值来定义 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=⟨字体名称⟩
```

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

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 = {⟨系列⟩}{⟨字形⟩}{ Font = ⟨字体名称⟩ , ⟨特性⟩ }
```

其中

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

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

对于粗字重、正常（直立）字形，我们有：

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

得到

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

对于粗字重、斜体字形：

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

得到

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

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

对于粗字重、正常（直立）字形，我们有：

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

得到

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

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

* `⟨系列⟩` = b
* `⟨字形⟩` = it
* `⟨字体名称⟩` = RobotoMono-VariableFont-wght.ttf
* `⟨特性⟩` = Weight=550

得到

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

## 整合到一起

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

```latex
% 文档字族无衬线字体
\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
% 文档字族等宽字体
\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 主文本字族（Noto Serif）：
\begin{itemize}
    \item \textsc{常规小型大写字母}
    \item \textit{\scshape 斜体小型大写字母}  
    \item \textbf{\scshape 粗体小型大写字母}
    \item \textbf{\itshape\scshape 粗斜体小型大写字母}
\end{itemize}
\item 无衬线字族（Noto Sans）：
\begin{itemize}
    \item \textsc{\sffamily 常规小型大写字母}
    \item \textit{\sffamily\scshape 斜体小型大写字母}  
    \item \textbf{\sffamily\scshape 粗体小型大写字母}
    \item \textbf{\sffamily\itshape\scshape 粗斜体小型大写字母}
\end{itemize}
\item 等宽字族（Roboto Mono）：
\begin{itemize}
    \item \textsc{\ttfamily 常规小型大写字母}
    \item \textit{\ttfamily\scshape 斜体小型大写字母}  
    \item \textbf{\ttfamily\scshape 粗体小型大写字母}
    \item \textbf{\ttfamily\itshape\scshape 粗斜体小型大写字母}
\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://overleaf-pro.ayaka.space/latex/zh-cn/shen-ru-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.
