> 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-tw/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/9c86eae1be030928eef83af409915b1659233a0c)）以建立新的專案檔案。將其命名為 `myfontsettings.sty` ，並選擇 **建立**.

![顯示 Overleaf「新增檔案」彈出視窗的圖片](/files/87e8b84bbc7356671ababb7c6370747498cdc342)

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-tw/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.
