> 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/37-step-1-setting-up-an-overleaf-project-to-use-variable-fonts.md).

# 步骤 1：设置一个使用可变字体的 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 项目](https://www.overleaf.com/8438931911svtqtnmfvsrp#28008d) 其中包含以下可变字体：

* [Noto Serif](https://fonts.google.com/noto/specimen/Noto+Serif/tester) （主文档正文）
* [Noto Sans](https://fonts.google.com/noto/specimen/Noto+Sans/tester) （无衬线字体）
* [Roboto Mono](https://fonts.google.com/specimen/Roboto+Mono/tester) （等宽字体）

这些字体尚未配置为可供使用。起初，当你编译 [随附的 Overleaf 项目](https://www.overleaf.com/8438931911svtqtnmfvsrp#28008d)时，将应用 LaTeX 的标准（默认）文本字体。在本教程中，你将学习如何将这些默认字体替换为项目文件夹中提供的可变字体，文件夹名为 `VariableFonts`.

如果你更愿意使用其他可变字体，只需遵循以下说明。否则，请继续阅读 [第 2 步](https://www.overleaf.com/learn/latex/Articles/Step_2:_An_introduction_to_LaTeX_fonts?preview=true) 本教程。

## （可选）如何选择并上传你自己的可变字体

按照以下步骤上传你选择的可变字体。

### 1. 从 Google Fonts 获取可变字体

[Google Fonts](https://fonts.google.com/) 是一个极好的资源。它提供 [有用的文档](https://fonts.google.com/knowledge/introducing_type/introducing_variable_fonts) 以及许多 [可免费使用的可变字体](https://fonts.google.com/?vfonly=true) ，可上传到 Overleaf 并配置用于你的项目。

* **提示**：使用 Google Fonts 过滤器查找可变字体。将 *技术* 过滤器设置为 *可变* ，以仅显示可变字体。还可以使用其他过滤器，例如 *衬线*，进一步细化你的搜索。

![显示 Google Fonts 搜索过滤器的图片](/files/ab726bb9ae2dd27433a2bd62f2744faa3c19a995)

* **提示**：你可以根据可变字体支持的设计轴来浏览它们：见 <https://fonts.google.com/variablefonts>。Google 还提供了可变字体设计轴的定义：见 <https://fonts.google.com/variablefonts#axis-definitions>.
* 找到你想使用的可变字体后，将它们下载到本地设备，并重命名任何包含有问题字符的文件。
* 某些可变字体由两个 `.ttf` 文件组成：一个用于直立（常规）样式，另一个（如果提供）用于斜体样式。请务必对你下载的每种字体检查这一点。

### 2. 重命名有问题的 Google 字体文件名

Google 为可变字体文件命名的方案包含字体的设计轴，并用逗号分隔。不幸的是，这种方案会给 fontspec 带来问题，而 fontspec 是在 LuaLaTeX 中使用可变字体所必需的一个宏包。例如，Noto Serif 提供宽度和字重轴，并以两个文件提供——一个用于直立字体，一个用于斜体字体：

* NotoSerif-VariableFont\_wdth,wght.ttf
* NotoSerif-Italic-VariableFont\_wdth,wght.ttf

这些文件名中的逗号在 fontspec 的 `\setmainfont` 命令中会触发错误。例如，写入

```
\setmainfont{NotoSerif-VariableFont_wdth,wght.ttf}
[ItalicFont = NotoSerif-Italic-VariableFont_wdth,wght.ttf]
```

会产生如下错误，包括

```
! Package fontspec Error: The font "NotoSerif-Italic-VariableFont_wdth" cannot
(fontspec)                be found.

! LaTeX Error: The key 'fontspec-opentype/wght.ttf' is unknown and is being
(LaTeX)        ignored.
```

要解决此问题，你需要重命名字体文件，去掉逗号（以及下划线 '\_'），并用“安全”字符替换它们，例如连字符（'-'），同时确保不要添加空格字符。例如，你可以将：

* NotoSans-VariableFont\_wdth,wght.ttf

更改为

* 重命名为 NotoSans-VariableFont-wdth-wght.ttf

这个简单的重命名步骤有助于避免冲突，并确保可变字体在你的 LaTeX 文档中顺利集成。

### 3. 创建一个新的 Overleaf 项目

* 创建一个新的 Overleaf 项目，并 [将编译器设置为 LuaLaTeX](/latex/zh-cn/zhi-shi-ku/026-changing-compiler.md) 因为它是唯一支持可变字体的编译器。
* 添加该 `fontspec` 包到文档的导言区，写入 `\usepackage{fontspec}`.
* 选择文件上传图标（![Image displaying Overleaf file-upload icon](/files/cd73366808b502e366dc01861644734de72a8b0a)）将本地重命名的可变字体文件添加到你的新项目中：

![显示如何向 Overleaf 项目上传文件的图片](/files/d49b2984d10f64d78b6b21126e97b3a2f5c278e1)

### 4.（可选）整理你的项目字体文件

如果你想尝试多种字体，例如在测试文档设计时，使用项目文件夹来组织字体文件会很有帮助。然而，在编译时，LaTeX 不会自动在你的项目文件夹中查找字体；你需要借助一个名为 [latexmkrc](/latex/zh-cn/shen-ru-wen-zhang/28-how-to-use-latexmkrc-with-overleaf.md)的本地配置文件来帮助 LaTeX。这里不展开细节，但建议创建一个名为 `VariableFonts` 的项目文件夹，并复制 `latexmkrc` 用于 [随附项目](https://www.overleaf.com/8438931911svtqtnmfvsrp#28008d).

## 在下一步中

第 2 步介绍了 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)


---

# 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/37-step-1-setting-up-an-overleaf-project-to-use-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.
