> 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/zi-xing/03-xelatex.md).

# 使用 XƎLaTeX 支援現代字型

## 簡介

[XeTeX](https://tug.org/xetex/) 是一個排版引擎，源自 Donald Knuth 最初的 TeX 軟體。然而，與 Knuth 的原始程式不同，XeTeX 可原生讀取（輸入）以 UTF-8 編碼的 Unicode 文字，並在其內建的 [HarfBuzz](https://harfbuzz.github.io/) 字形排版引擎協助下，支援現代字型格式，例如 [OpenType (OTF)](https://docs.microsoft.com/en-us/typography/opentype/) 和 [SIL 的 Graphite 字型技術](https://scripts.sil.org/cms/scripts/page.php?site_id=projects\&item_id=graphite_home)。對應的 LaTeX [編譯器](/latex/zh-tw/geng-duo-zhu-ti/08-choosing-a-latex-compiler.md) 在 Overleaf 上稱為 `xelatex`。任何想更深入了解不同 TeX 引擎演進的人，不妨閱讀 Overleaf 文章 [名稱裡有什麼：TeX 各種口味指南](/latex/zh-tw/shen-ru-wen-zhang/55-what-s-in-a-name-a-guide-to-the-many-flavours-of-tex.md).

## 基礎：Times New Roman

Times New Roman 是常用字型，以下是在 Overleaf 上透過 XeLaTeX 編譯器使用它的範例。在這個例子中，文件字型是使用 `\usemainfont{Times New Roman}`，其中指令 `\usemainfont{...}` 由套件 `fontspec`.

```latex
\documentclass[12pt]{article}
\usepackage{fontspec}

\setmainfont{Times New Roman}
\title{Sample font document}
\author{Overleaf}
\date{\today}

\begin{document}
\maketitle

這是一份使用
\textbf{XeLaTeX} 編譯的文件 \textit{範例}。LuaLaTeX 也應該可以正常運作。

\end{document}
```

[在 Overleaf 上使用 XeLaTeX 開啟的 Times New Roman 範例](https://www.overleaf.com/docs?engine=xelatex\&snip_name=Times+New+Roman+%28via+XeLaTeX%29\&snip=%5Cdocumentclass%5B12pt%5D%7Barticle%7D%0A%5Cusepackage%7Bfontspec%7D%0A+%0A%5Csetmainfont%7BTimes+New+Roman%7D%0A%5Ctitle%7BSample+font+document%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7B%5Ctoday%7D%0A+++%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A+++++%0AThis+is+an+%5Ctextit%7Bexample%7D+of+a+document+compiled+%0Awith+%5Ctextbf%7BXeLaTeX%7D.+LuaLaTeX+should+also+work+too.%0A%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![XeLaTeXOverleaf.png](/files/c5b73a9870fd18819be9421954b411d7f9b96438)

你也可以 [使用 LuaLaTeX 執行相同的程式碼](https://www.overleaf.com/docs?engine=lualatex\&snip_name=Times+New+Roman+%28via+LuaLaTeX%29\&snip=%5Cdocumentclass%5B12pt%5D%7Barticle%7D%0A%5Cusepackage%7Bfontspec%7D%0A+%0A%5Csetmainfont%7BTimes+New+Roman%7D%0A%5Ctitle%7BSample+font+document%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7B%5Ctoday%7D%0A+++%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A+++++%0AThis+is+an+%5Ctextit%7Bexample%7D+of+a+document+compiled+%0Awith+%5Ctextbf%7BLuaLaTeX%7D.%0A%0A%5Cend%7Bdocument%7D) .

## 為不同 LaTeX 元素設定字型

LaTeX 文件中的不同元素會以不同字型排版；例如，在 `verbatim` 環境中，文字會以打字機風格顯示。可為每種情境設定不同的字型樣式：

```latex
\documentclass[12pt]{article}
\usepackage{fontspec}

%-----------------------------------------------------------------------
\setmainfont{Times New Roman}
\setsansfont{Arial}
\setmonofont[Color={0019D4}]{Courier New}
%-----------------------------------------------------------------------

\title{Sample font document}
\author{Overleaf}
\date{\today}
\begin{document}

\maketitle

這是一份使用
\textbf{XeLaTeX} 編譯器編譯的文件 \textit{範例}。如果你必須撰寫一些程式碼，可以
使用 \texttt{verbatim} 環境：

    \begin{verbatim}
    通常這個環境用來顯示程式碼，
    因此這裡有一個簡單的 C 程式。

    #include <stdio.h>
    #define NUMYAYS 10

    int main()
    {
        int i;
        for (i = 0; i <= NUMYAYS; i++)
        {
            printf("Yay! Overleaf is Awesome!\n");
        }
        return 0;
    }
    \end{verbatim}
    {\sffamily 這是一段使用 \textbf{無襯線字型樣式} 的範例文字}

\end{document}
```

[若要查看結果，請在 Overleaf 上開啟這個 XeLaTeX 範例](https://www.overleaf.com/docs?engine=xelatex\&snip_name=Document+formatted+with+different+fonts\&snip=%5Cdocumentclass%5B12pt%5D%7Barticle%7D%0A%5Cusepackage%7Bfontspec%7D%0A%0A%25-----------------------------------------------------------------------%0A%5Csetmainfont%7BTimes+New+Roman%7D%0A%5Csetsansfont%7BArial%7D%0A%5Csetmonofont%5BColor%3D%7B0019D4%7D%5D%7BCourier+New%7D%0A%25-----------------------------------------------------------------------%0A%0A%5Ctitle%7BSample+font+document%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7B%5Ctoday%7D%0A%5Cbegin%7Bdocument%7D%0A++++%0A%5Cmaketitle%0A+++++%0AThis+an+%5Ctextit%7Bexample%7D+of+document+compiled+with+the++%0A%5Ctextbf%7BXeLaTeX%7D+compiler.+If+you+have+to+write+some+code+you+can+%0Ause+the+%5Ctexttt%7Bverbatim%7D+environment%3A%0A%0A++++%5Cbegin%7Bverbatim%7D%0A++++Usually+this+environment+is+used+to+display+code%2C+%0A++++so+here%27s+a+simple+C+program.%0A%0A++++%23include+%3Cstdio.h%3E%0A++++%23define+NUMYAYS+10%0A++++++%0A++++int+main%28%29%0A++++%7B%0A++++++++int+i%3B+++%0A++++++++for+%28i+%3D+0%3B+i+%3C%3D+NUMYAYS%3B+i%2B%2B%29+%0A++++++++%7B%0A++++++++++++printf%28%22Yay%21+Overleaf+is+Awesome%21%5Cn%22%29%3B%0A++++++++%7D%0A++++++++return+0%3B%0A++++%7D%0A++++%5Cend%7Bverbatim%7D%0A++++%7B%5Csffamily+This+is+a+sample+text+in+%5Ctextbf%7BSans+Serif+Font+Typeface%7D%7D%0A+++++++%0A%5Cend%7Bdocument%7D)

在前一個範例中使用了三種不同的字型，接下來幾行決定哪些元素會使用這些字型：

**\setmainfont{Times New Roman}**

這是文件大多數部分使用的預設字型，在範例中是 Times New Roman。

**\setsansfont{Arial}**

需要無襯線字型的元素，在範例中由 \ssfamily 明確宣告，將以 Arial 字型排版。

**\setmonofont{Courier New}**

所有要以等寬（打字機風格）字型排版的內容都會使用 Courier New 字型。這個指令在大括號中還有一個額外的可選參數：

**Color={0019D4}**

這會使用十六進位 HTML 格式的 Color= 值來設定文字顏色。謹慎且選擇性地使用文字顏色，在製作簡報時會很有幫助。

## Overleaf 中的字型

有 [大量字型已安裝在 Overleaf 上](/latex/zh-tw/wen-yu-da/85-which-otf-or-ttf-fonts-are-supported-via-fontspec.md) ，你可以在專案中使用，最簡單的方式是透過 `fontspec`。以下範例展示了使用 Overleaf 伺服器上所含字型，例如 Google Noto，以及需要從 [Google Fonts](https://fonts.google.com/).

### 安裝在 Overleaf 伺服器上的字型：Google Noto 字型

因為 [TeX Live](https://www.tug.org/texlive/)——Overleaf 所使用的——包含 [Google Noto 字型](https://www.google.com/get/noto/) 你可以透過 `fontspec` 套件，在使用 XeLaTeX 或 LuaLaTeX 編譯器編譯的 Overleaf 專案中加以使用。這裡有一個示範專案，使用 [`noto` 套件](https://ctan.org/pkg/noto?lang=en) 來設定你的文件，以使用 NotoSerif、NotoSans 與 NotoSansMono 字型家族排版。關於 `noto` 套件的文件可在其 [CTAN 上的 readme 檔案](http://mirrors.ctan.org/fonts/noto/README).

```latex
\documentclass{article}
\usepackage{xcolor}
\usepackage{noto}
\usepackage{hyperref}
\title{Using Google Noto fonts}
\author{Overleaf}
\date{April 2021}

\begin{document}

\maketitle

\section{簡介}
此範例專案使用 \href{https://ctan.org/pkg/noto?lang=en}{\color{blue}\texttt{noto}} 套件，透過 Google 的 Noto 字型\footnote{\url{https://www.google.com/get/noto/}} 來排版你的文件：
\begin{itemize}
\item \verb|\textbf{bold}| 會產生 \textbf{粗體}
\item \verb|\textit{italic}| 會產生 \textit{斜體}
\item \verb|\textbf{\textit{bold italic}}| 會產生 \textbf{\textit{粗斜體}}
\item \verb|\emph{emphasis}| 會產生 \emph{強調}
\item \verb|\textbf{\emph{bold italic}}| 會產生 \textbf{\emph{粗斜體}}
\end{itemize}

\subsection{等寬字型}
你可以使用 Noto 的等寬字型來排版 \texttt{一般} 與 \texttt{\textbf{粗體}} 等寬文字。

\subsection{無襯線字型}
這裡有一些 \textsf{以無襯線字型排版的文字}，以及 \textbf{\textsf{以粗體無襯線字型排版的文字}}。

\section{延伸閱讀}
\texttt{noto} 套件的文件可在其 \href{http://mirrors.ctan.org/fonts/noto/README}{\color{blue}\texttt{CTAN 上的 readme} 檔案}中找到。

\end{document}
```

[在 Overleaf 上開啟這個 XeLaTeX 範例，探索 Noto 字型](https://www.overleaf.com/docs?engine=xelatex\&snip_name=Using+Google+Noto+fonts\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bxcolor%7D%0A%5Cusepackage%7Bnoto%7D%0A%5Cusepackage%7Bhyperref%7D%0A%5Ctitle%7BUsing+Google+Noto+fonts%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7BApril+2021%7D%0A%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cmaketitle%0A%0A%5Csection%7BIntroduction%7D%0AThis+example+project+uses+the+%5Chref%7Bhttps%3A%2F%2Fctan.org%2Fpkg%2Fnoto%3Flang%3Den%7D%7B%5Ccolor%7Bblue%7D%5Ctexttt%7Bnoto%7D%7D+package+to+typeset+your+document+using+Google%27s+Noto+fonts%5Cfootnote%7B%5Curl%7Bhttps%3A%2F%2Fwww.google.com%2Fget%2Fnoto%2F%7D%7D%3A%0A%5Cbegin%7Bitemize%7D%0A%5Citem+%5Cverb%7C%5Ctextbf%7Bbold%7D%7C+produces+%5Ctextbf%7Bbold%7D%0A%5Citem+%5Cverb%7C%5Ctextit%7Bitalic%7D%7C+produces+%5Ctextit%7Bitalic%7D%0A%5Citem+%5Cverb%7C%5Ctextbf%7B%5Ctextit%7Bbold+italic%7D%7D%7C+produces+%5Ctextbf%7B%5Ctextit%7Bbold+italic%7D%7D%0A%5Citem+%5Cverb%7C%5Cemph%7Bemphasis%7D%7C+produces+%5Cemph%7Bemphasis%7D%0A%5Citem+%5Cverb%7C%5Ctextbf%7B%5Cemph%7Bbold+italic%7D%7D%7C+produces+%5Ctextbf%7B%5Cemph%7Bbold+italic%7D%7D%0A%5Cend%7Bitemize%7D%0A%0A%5Csubsection%7BMonospaced+fonts%7D%0AYou+can+use+Noto%27s+monospaced+fonts+for+%5Ctexttt%7Bregular%7D+and+%5Ctexttt%7B%5Ctextbf%7Bbold%7D%7D+monospaced+text.%0A%0A%5Csubsection%7BSans+serif+fonts%7D%0AHere+is+some+%5Ctextsf%7Btext+is+typeset+in+a+sans+serif+font%7D+together+with+%5Ctextbf%7B%5Ctextsf%7Btext+typeset+in+bold+sans+serif%7D%7D.%0A%0A%5Csection%7BFurther+reading%7D%0ADocumentation+for+the+%5Ctexttt%7Bnoto%7D+package+can+be+found+in+its+%5Chref%7Bhttp%3A%2F%2Fmirrors.ctan.org%2Ffonts%2Fnoto%2FREADME%7D%7B%5Ccolor%7Bblue%7D%5Ctexttt%7Breadme%7D+file+on+CTAN%7D.%0A%0A%5Cend%7Bdocument%7D)

![使用 Google Noto 字型來排版 LaTeX 文件](/files/27447bea96e773686ebe780a51465fe6d2450b18)

### 使用未安裝在 Overleaf 伺服器上的字型：Google Fonts 範例

若要使用未安裝在 Overleaf 伺服器上的 OpenType 字型，你必須先將字型檔上傳到你的 Overleaf 專案——前提是字型授權允許如此。OpenType 字型上傳後，將專案編譯器設為 XƎLaTeX（或 LuaLaTeX），並使用 `fontspec` 來設定你的字型。

下方概述的範例專案說明如何建立資料夾、上傳字型，並使用 `fontspec`進行設定。以下範例將使用來自 [Google Fonts](https://fonts.google.com/):

* 襯線字型： [Brygada1918](https://fonts.google.com/specimen/Brygada+1918)
* 無襯線字型： [Asap](https://fonts.google.com/specimen/Asap)
* 等寬字型： [JetBrains Mono](https://fonts.google.com/specimen/JetBrains+Mono)

注意：我們並不是要表示這個範例提供了在排版上和諧的字體組合，它只是展示如何上傳、設定並使用你選擇的字型。

對於每一種字體，你都需要從 Google Fonts 下載字型家族：

![GoogleDownload.png](/files/8c3040cb585a94c5836eef30d774a2ef7f873cf1)

注意：某些 Google 字型有 "static" OpenType 字型與所謂的 "variable" OpenType 字型可供使用。如果下載的 ZIP 檔中有一個名為 "static" 的資料夾，你應該上傳該資料夾中的字型，而不是可變字型版本：

![OpenTypeStatic.png](/files/f6d315f806492e1799d9764434dc66bd0a503eb5)

接著，你需要將字型檔上傳到你的專案中，但由於字型家族包含多個字型檔，你可能會想建立個別的 Overleaf 專案資料夾來存放這些檔案——每個字型家族使用一個資料夾。以下影片示範如何做到。稍後，我們需要告訴 XƎLaTeX 要到哪裡尋找你專案的字型——因為它們並未預先安裝在 Overleaf 的伺服器上。

{% embed url="<https://videos.ctfassets.net/nrgyaltdicpt/4lQFkZ1Ni1FHFdHJUJSNH4/4aee7c5bafeb021a6f21b4637d0fe6ec/UploadingGoogleFonts.mp4>" %}

### 設定 fontspec 以使用這些字型

將字型家族檔案上傳到 Overleaf 專案資料夾後，我們可以透過 `fontspec`進行設定。在下列 LaTeX 程式碼中，請注意我們如何使用 `Path` 參數來告訴 `fontspec` 字型檔位於我們專案檔案系統中的本機資料夾：

```latex
\setmainfont{Brygada1918}[
    Path=./BrygadaFontFiles/,
    Extension = .ttf,
    UprightFont=*-Regular,
    BoldFont=*-Bold,
    ItalicFont=*-Italic,
    BoldItalicFont=*-BoldItalic
    ]

\setsansfont{Asap}[
    Path=./AsapFontFiles/,
    Scale=0.9,
    Extension = .ttf,
    UprightFont=*-Regular,
    BoldFont=*-Bold,
    ItalicFont=*-Italic,
    BoldItalicFont=*-BoldItalic
    ]

\setmonofont{JetBrainsMono}[
    Path=./JetbrainsFontFiles/,
    Scale=0.85,
    Extension = .ttf,
    UprightFont=*-Regular,
    BoldFont=*-Bold,
    ItalicFont=*-Italic,
    BoldItalicFont=*-BoldItalic
    ]
```

![FontSpecPathUpdated.png](/files/4dd8a2fdd08d4d40a0d9215cac1418153f552180)

[在 Overleaf 上開啟 XeLaTeX 與 Google Fonts 示範](https://www.overleaf.com/project/new/template/31226?id=835225587\&templateName=XeLaTeX+font+demo+with+Google+Fonts\&latexEngine=xelatex\&texImage=texlive-full%3A2022.1\&mainFile=)

該 [`fontspec` 套件](https://ctan.org/pkg/fontspec?lang=en) 提供大量 OpenType 字型設定選項，可供與 LuaLaTeX 或 XƎLaTeX 搭配使用——其 [優異的文件](http://mirrors.ctan.org/macros/unicodetex/latex/fontspec/fontspec.pdf) 包含許多範例，你可以深入探索，以微調文件的設計與排版。

## 延伸閱讀

詳情請見：

* [粗體、斜體與底線](/latex/zh-tw/latex-ji-chu/03-bold-italics-and-underlining.md)
* [字型大小、字族與樣式](/latex/zh-tw/zi-xing/01-font-sizes-families-and-styles.md)
* [字型字體](/latex/zh-tw/zi-xing/02-font-typefaces.md)
* [段落格式設定](/latex/zh-tw/ge-shi-hua/04-articles-how-to-change-paragraph-spacing-in-latex.md)
* [換行與空白](/latex/zh-tw/ge-shi-hua/05-line-breaks-and-blank-spaces.md)
* [文字對齊](/latex/zh-tw/ge-shi-hua/06-text-alignment.md)
* [多欄排版](/latex/zh-tw/ge-shi-hua/09-multiple-columns.md)
* [大型專案中的管理](/latex/zh-tw/wen-jian-jie-gou/07-management-in-a-large-project.md)
* [多檔案 LaTeX 專案](/latex/zh-tw/wen-jian-jie-gou/08-multi-file-latex-projects.md)
* [國際語言支援](/latex/zh-tw/yu-yan/03-international-language-support.md)
* [選擇 LaTeX 編譯器](/latex/zh-tw/geng-duo-zhu-ti/08-choosing-a-latex-compiler.md)
* [Overleaf 上安裝的 OTF 與 TTF 字型列表](/latex/zh-tw/wen-yu-da/85-which-otf-or-ttf-fonts-are-supported-via-fontspec.md)
* [Google 的字型集合](http://www.google.com/fonts/)
* [`fontspec` 套件手冊](http://tug.ctan.org/tex-archive/macros/latex/contrib/fontspec/fontspec.pdf)


---

# 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/zi-xing/03-xelatex.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.
