> 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/35-overleaf-projects-showing-how-to-use-variable-fonts-with-latex.md).

# 展示如何在 LaTeX 中使用可变字体的 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)

本教程最后一部分列出了 Overleaf 创建的五个项目，用于展示 OpenType 可变字体的使用。所有项目都包含所需的可变字体文件和一个 `latexmkrc` 用于使这些字体文件可供 LaTeX 使用的文件。

免费套餐用户需注意，其中一些项目的首次编译时间较长，这是因为它们使用了 LuaLaTeX 和 OpenType 可变字体。

## 项目 1：一个已配置好 OpenType 可变字体的即用项目

* 打开 Overleaf 项目 <https://www.overleaf.com/1311295292wvdkgcbnfxsw#94292d>

此项目配置为使用 Noto Serif、Noto Sans 和 Roboto Mono。章节标题、图表标题，甚至脚注都配置为使用特定的可变字体设置——请参见项目文件 `myvariablefonts.sty` 了解更多详情。

## 项目 2：使用可变字体演示 LaTeX 的字体选择命令

* 打开 Overleaf 项目： <https://www.overleaf.com/1941895198cqjzyhzvxhwz#794ae2>

此项目为 Noto Serif、Noto Sans 和 Roboto Mono 配置了 LaTeX 字体宽度、字重和字形的多种组合。它展示了如何将这些字体与 LaTeX 的字体选择命令一起使用 `\fontseries`, `\fontshape` 和 `\fontfamily`.

![图像显示使用 LaTeX 的 \fontseries 命令来排版可变字体](/files/0e9cd996f832c4396107f4634b7a7056d75b04c9)

## 项目 3：使用可变字体配合 babel 宏包排版阿拉伯语和英语

* 打开 Overleaf 项目 <https://www.overleaf.com/3933144323kswvxhnqkbcw#097c77>

此项目向您展示如何配置 OpenType 可变字体以用于 [babel 宏包](https://ctan.org/pkg/babel?lang=en)。其中包含使用几种阿拉伯文字 OpenType 可变字体排版英阿混排文本的示例：

* [Noto Naskh Arabic](https://fonts.google.com/noto/specimen/Noto+Naskh+Arabic/tester)
* [Noto Nastaliq](https://fonts.google.com/noto/specimen/Noto+Nastaliq+Urdu/tester)
* [Noto Sans Arabic](https://fonts.google.com/noto/specimen/Noto+Sans+Arabic/tester)

此项目展示了 fontspec 的 `\addfontfeatures` 命令可用于对 OpenType 可变字体进行临时更改，包括本项目中使用的阿拉伯语可变字体。

![图像显示使用带有 OpenType 可变字体的 LaTeX 排版的一段阿拉伯语和英语文本](/files/ddfacdbd44a55b018613caaa700eb29fd65082ca)

**免责声明**：所有非英语文本及其对应翻译均由 ChatGPT 生成。

## 项目 4：展示可变字体的多种示例

* 打开 Overleaf 项目 <https://www.overleaf.com/2858462957mvthxfmtgxsc#11aac1>

此项目包含多个示例，展示如何使用 fontspec 的 `\addfontfeatures` 命令对可变字体进行临时调整；例如，临时更改字体的宽度或字重，以调整排版某些文本所需的空间。以下各节概述了这些示例，并提供背景信息以帮助您充分利用每一个示例。

### 示例：更改字体宽度以在给定空间内容纳更多文本

此示例使用 fontspec 的 `\addfontfeatures` 命令来演示：对可变字体宽度进行微小、几乎不可察觉的缩减，就能在给定空间内排入更多文本。两个包含相同文本的 minipage 并排排版，用于比较非压缩版 Noto Serif 与其宽度轴值减少 4% 的版本。

![图像显示可变字体宽度的缩减可在给定空间内容纳更多文本](/files/61e537acf4c289b18d71d02ba3231e8c10e2e3db)

### 示例：在 fancyvrb 宏包中使用可变字体

使用 fontspec 的 `\newfontfamily` 命令，本示例演示如何使用您选择的可变字体在 `Verbatim` 由 [`fancyvrb` 宏包](https://ctan.org/pkg/fancyvrb?lang=en)提供的 Verbatim 环境中排版程序代码。它还展示了如何使用 LaTeX 的 `\fontseries` 该命令。

![图像显示使用可变字体排版的逐字文本](/files/4818e2c418aa4a61d5529b7d1a2c718392579d3d)

#### 关于 Verbatim 环境的一些说明

该 `Verbatim` 环境的形式如下

```latex
\begin{Verbatim}[⟨settings⟩]
⟨input⟩
\end{Verbatim}
```

其中

* 该 `[⟨settings⟩]` 部分是可选的。 `⟨settings⟩` 可以包含参数（键值对），使您能够配置此特定 `Verbatim` 环境
* `⟨input⟩` 是您想要排版的文本

一个 `Verbatim` 环境会禁用 LaTeX 的默认文本处理，因此 `⟨input⟩` 文本可以原样排版——包括空格、LaTeX 命令、特殊字符和换行。

要格式化 `⟨input⟩` 文本，例如应用粗体或斜体，请使用 `commandchars` 键，在 `⟨settings⟩` 部分中的链接。 `commandchars` 指定三个“特殊”字符，可在 `⟨input⟩` 中用来调用 LaTeX 宏，而不是被直接排版。为避免错误和冲突，请选择三个不会出现在常规 `⟨input⟩` 文本。

例如，写作

```latex
\begin{Verbatim}[commandchars=|()]
⟨input⟩
\end{Verbatim}
```

会告诉 `Verbatim` 环境将字符 `|`, `(`，以及 `)` 视为命令字符。若它们出现在 `⟨input⟩` 中，LaTeX 将按如下方式处理：

* `|` 用于引入 LaTeX 命令——替代通常的 `\` 字符。
* `(` 和 `)` 用于界定 LaTeX 命令参数——而不是通常的 `{` 和 `}`

例如，要将部分 `⟨input⟩` 文本排为粗体，您可以写 `|textbf(some text)` 而不是写 `\textbf{some text}`.

### 示例：使用不同样式排版数字

此示例使用 fontspec 的 `\addfontfeatures` 命令来临时更改用于排版字符 0、1、2、……9 的样式，这些字符通常被称为“digits”或“numbers”。排版师和设计师将这些字符称为“figures”，本示例中采用的是该术语。

#### 关于排版和数字样式的一些说明

随着时间的推移，针对不同文档元素（如表格、正文或文档标题）排版数字的样式已逐渐发展出来。这些样式依据数字的三个属性来定义：宽度、高度以及相对于基线的对齐方式。

有两种样式基于数字宽度：

* **比例宽度**：每个数字的宽度会根据其设计而变化。
* **tabular**：每个数字具有相同的宽度。

有两种样式基于数字高度和对齐方式：

* **齐线数字**：数字位于基线上，其设计特征不会低于基线。所有数字高度相同。
* **旧式数字**：数字高度不一，数字设计的一部分可能会出现在基线以下。

将这些组合起来，就得到下图所示的四种排版样式：

![](/files/8b0741cacb32d3021ccf45728ee48e7d33470e8f)

一个 [Monotype 发布的文章](https://www.monotype.com/resources/expertise/how-use-figure-styles-illustrator) 讨论了如何使用这些数字样式。

#### 如何获取这些数字样式

这些数字样式的可用性取决于用于排版文档的可变或非可变 OpenType 字体。幸运的是，Noto Serif 支持这四种数字样式，因此我们可以使用 `\addfontfeatures` 来使用 fontspec 的 `Numbers` 键——请注意 `OldStyle`的大小写。下表是所提供的示例之一：

![一张展示用于排版数字的四种样式的表格：表格旧式数字、比例旧式数字、表格齐线数字和比例齐线数字。](/files/500a37fa150f2e0a0bf43912e97462c59ab2e932)

* 打开 Overleaf 项目 <https://www.overleaf.com/2858462957mvthxfmtgxsc#11aac1>

## 项目 5：使用可变字体演示 LaTeX 的“虚拟”字体字形

* 打开 Overleaf 项目 <https://www.overleaf.com/4719455662kydjkfvdwjpn#2ea0f1>

LaTeX 的“虚拟”字体字形并不代表真实字体；相反，它们旨在与 `\fontshape` 配合，以更改当前字形。项目中的示例展示了如何使用命令 `\fontshape`, `\textup`, `\textulc`, `\upshape`，以及 `\ulcshape`.


---

# 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/35-overleaf-projects-showing-how-to-use-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.
