> 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/44-step-8-how-to-create-a-simple-latex-package-to-configure-your-variable-fonts.md).

# 步骤 8：如何创建一个简单的 LaTeX 宏包来配置你的可变字体

&#x20;[引言](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)&#x20;

使用 fontspec 配置可变字体时，文档导言区可能会被大量设置代码弄得很杂乱。在这一步中，我们将展示如何创建一个简单的宏包来容纳 fontspec 的配置代码——从而简化文档导言区，并使进一步修改字体更容易。

## 如何创建和使用一个简单的 LaTeX 宏包

按照以下步骤，为本教程中我们创建的 fontspec 配置代码制作一个简单的 LaTeX 宏包。

1. 选择 **新文件** 图标（![Overleaf icon representing adding a file](/files/5623f943b055888fb427a88ae16784acad4dea0b)) 以创建新的项目文件。将其命名为 `myfontsettings.sty` 并选择 **创建**.

![显示 Overleaf 的“添加文件”模态窗口的图片](/files/22283c2503bf56055581c80765c77ab1278ab2cf)

3. 将 fontspec 配置代码粘贴到文件中 `myfontsettings.sty`. 这里，我们使用的是在可变字体教程中添加的 Noto Serif、Noto Sans 和 Roboto Mono 的 fontspec 配置代码。如果你使用不同的可变字体，必须修改 fontspec 设置。

```latex
% myfontsettings.sty
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{myfontsettings}[2024/06/13 v1.0 我的自定义字体设置]

% 这个简单宏包包含你的 fontspec 配置
% 它使文档导言区保持简洁整齐

% 加载 fontspec 宏包
\RequirePackage{fontspec}

% 为文档设置正文字体
\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}
]

% 文档无衬线字体
\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}
]

% 文档等宽字体
\setmonofont{RobotoMono-VariableFont-wght.ttf}[
NFSSFamily=RobotoMono,
Weight=300,
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}
]

\endinput
```

5. 要使用此宏包，请添加以下一行 `\usepackage{myfontsetting}` 到文档导言区。


---

# 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/44-step-8-how-to-create-a-simple-latex-package-to-configure-your-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.
