> 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/39-step-3-replacing-latex-s-default-fonts-with-variable-fonts.md).

# 步驟 3：以可變字型取代 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)

在這個步驟中，我們會介紹 fontspec 套件中的三個指令，這些指令將用來以可變字型取代 LaTeX 的預設字型。

## 使用 fontspec 變更 LaTeX 的預設字型

我們將使用以下 fontspec 套件指令，將 LaTeX 的預設字型替換為我們選擇的可變字型：

* `\setmainfont{⟨font⟩}[⟨font features⟩]` 設定用於文件主要內文的字型。
* `\setsansfont{⟨font⟩}[⟨font features⟩]` 設定無襯線字型。
* `\setmonofont{⟨font⟩}[⟨font features⟩]` 設定等寬字型。

這些指令各有兩個部分：

* `⟨font⟩`：一個必要引數，位於大括號中 `{...}`，用來指定我們要使用的字型，可寫成字型名稱或字型檔案名稱。
* `⟨font features⟩`：一個可選引數，位於方括號中 `[...]`，用來啟用字型特定功能，包括設定可變字型。

這些指令稍後在本教學中會有更詳細的說明。

## 如何啟用教學專案的可變字型

請依照以下步驟啟用並使用專案的可變字型。

1. 開啟初始教學專案，並將下列程式碼貼入文件前言中，更新它——請將其貼在以下這一行之後 `\usepackage{fontspec}`.

```latex
% 文件家族主要內文字型
\setmainfont{NotoSerif-VariableFont-wdth-wght.ttf}
% 文件主系列無襯線字型
\setsansfont{NotoSans-VariableFont-wdth-wght.ttf}
% 文件主系列等寬字型
\setmonofont{RobotoMono-VariableFont-wght.ttf}
```

![顯示在文件前言中應貼上 LaTeX 程式碼位置的圖片](/files/e906f294dfdef69cfad2ea9f703f5f391e1052f3)

4. 重新編譯更新後的專案，以使用可變字型排版。你應該會注意到以下問題：

* 排版後的文件不再有任何粗體或斜體文字。
* 共有三個「未定義字形」警告：

![顯示 LaTeX 缺少字型警告的圖片。](/files/26f3823c526bfa120bb8268dee4fc26a0defdbc3)

## 為什麼我會看到「未定義字形」警告？

我們對可變字型的初始設定是 *非常* 基礎的，只啟用了正立、中等字重的字型。因此，LaTeX 會發出「未定義字形」警告，提醒你它已無法存取用來排版文件中粗體、粗斜體和一般斜體文字的字型。

## 理解 LaTeX 的「未定義字形」警告

如第 2 步所述，LaTeX 會依據五個屬性來分類每一種文字字型：編碼、字型家族、系列、字形與大小。若你嘗試使用某個字型，而其屬性組合——編碼、字型家族、系列與字形——無法對應到已知的「真實」字型，LaTeX 就會發出「缺少字形」警告。透過設定具有所需屬性的字型，可以避免這些警告。

### 這些警告的結構

每個警告的結構為

``字型字形 `*編碼/字型家族名稱/系列/字形*' 未定義``

如下圖所示：

![顯示 LaTeX 缺少字形警告結構的圖片](/files/9738cad18a646c5809b5c19c1f096222c6fd7146)

### 這些警告在告訴我們什麼？

以下將逐一說明這些警告。

![顯示 LaTeX 缺少字型警告的圖片。](/files/26f3823c526bfa120bb8268dee4fc26a0defdbc3)

1. **``字型字形 `TU/NotoSerif-VariableFont-wdth-wght.ttf(0)/b/n' 未定義``**

LaTeX 無法存取具有以下屬性組合的字型：

* *編碼*: **`TU`** （Unicode）。
* *家族*: `**NotoSerif-VariableFont-wdth-wght.ttf(0)**`——這是我們使用 \setmainfont 指令時，fontspec 套件建立的一個冗長字型家族名稱。我們會在本系列稍後探討字型家族名稱。
* *系列*: **`b`** → **b**粗體 *重量*, **m**中等 *width* （其中 **m** 被省略）。
* 字形： **`n`** → **n**正常（正立） *字形。*

LaTeX 缺少一個 **b**粗體字重， **m**中等寬度（系列），以及 **n**正常（正立）字形的字型，位於 `NotoSerif-VariableFont-wdth-wght.ttf(0)` 字型家族中。

4. **``字型字形 `TU/NotoSerif-VariableFont-wdth-wght.ttf(0)/b/it' 未定義``**

LaTeX 無法存取具有以下屬性組合的字型：

* *編碼*: **`TU`** （Unicode）。
* *家族*: `**NotoSerif-VariableFont-wdth-wght.ttf(0)**`*.*
* *系列*: `**b**` → **b**粗體字重， **m**中等寬度（其中 **m** 被省略）。
* *字形*：it → 斜體字形。

LaTeX 缺少一個 **b**粗體字重， **m**中等寬度（系列） **it**斜體（字形）字型，位於 `NotoSerif-VariableFont-wdth-wght.ttf(0)` 字型家族中。

8. ``**字型字形 `TU/NotoSerif-VariableFont-wdth-wght.ttf(0)/m/it' 未定義**``

LaTeX 無法存取具有以下參數組合的字型：

* 編碼：TU（Unicode）
* 字型家族：NotoSerif-VariableFont-wdth-wght.ttf(0)
* 系列：m → 中等字重、中等寬度（請注意只使用單一個 m）
* 字形：it → 斜體字形

LaTeX 在 NotoSerif-VariableFont-wdth-wght.ttf(0) 字型家族中，缺少一個中等字重、中等寬度（系列）的斜體（字形）字型。

## 在下一步

在第 4 到第 6 步中，我們將透過設定可變字型來提供缺少的字型字形，從而修正這些警告。

[簡介](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-tw/shen-ru-wen-zhang/39-step-3-replacing-latex-s-default-fonts-with-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.
