> For the complete documentation index, see [llms.txt](https://ayakaleaf-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://ayakaleaf-pro.ayaka.space/latex/zh-cn/zi-ti/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-cn/geng-duo-zhu-ti/08-choosing-a-latex-compiler.md) 在 Overleaf 上被称为 `xelatex`。任何希望更好理解不同 TeX 引擎演进的人，可能都想阅读 Overleaf 文章 [《名称里有什么：TeX 多种风味指南》](/latex/zh-cn/shen-du-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

这是一个使用 \textit{示例} 的文档，经过
\textbf{XeLaTeX} 编译。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/db20fbd754beca8488fb17f7725e026417ba8450)

你也可以 [使用 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-cn/wen-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/) ，你可以在使用 XeLaTeX 或 LuaLaTeX 编译器编译的 Overleaf 项目中通过 `fontspec` 包来使用它们。下面是一个示例项目，其中使用了 [`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{Introduction}
这个示例项目使用 \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/91b40faef3e808fb23740840ec651e1b1248f296)

### 使用 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/589a27ecd6c793118bfbf90950b057c29d6a740b)

注意：有些 Google 字体提供“静态” OpenType 字体和所谓“可变” OpenType 字体。如果下载的 ZIP 文件中有一个名为“static”的文件夹，你应该上传该文件夹中的字体，而不是可变字体版本：

![OpenTypeStatic.png](/files/75a97a1552ea0781ab98683763f8b11ab324ded9)

接下来，你需要将字体文件上传到你的项目中，但由于字体家族包含多个字体文件，你可能希望创建单独的 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/0c58ef02eab50bee843a02e2d2c6e59225840e95)

[在 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-cn/latex-ji-chu/03-bold-italics-and-underlining.md)
* [字体大小、字体族和样式](/latex/zh-cn/zi-ti/01-font-sizes-families-and-styles.md)
* [字体类型](/latex/zh-cn/zi-ti/02-font-typefaces.md)
* [段落格式](/latex/zh-cn/ge-shi-hua/04-articles-how-to-change-paragraph-spacing-in-latex.md)
* [换行和空格](/latex/zh-cn/ge-shi-hua/05-line-breaks-and-blank-spaces.md)
* [文本对齐](/latex/zh-cn/ge-shi-hua/06-text-alignment.md)
* [多栏排版](/latex/zh-cn/ge-shi-hua/09-multiple-columns.md)
* [大型项目中的管理](/latex/zh-cn/wen-dang-jie-gou/07-management-in-a-large-project.md)
* [多文件 LaTeX 项目](/latex/zh-cn/wen-dang-jie-gou/08-multi-file-latex-projects.md)
* [国际语言支持](/latex/zh-cn/yu-yan/03-international-language-support.md)
* [选择 LaTeX 编译器](/latex/zh-cn/geng-duo-zhu-ti/08-choosing-a-latex-compiler.md)
* [Overleaf 上安装的 OTF 和 TTF 字体列表](/latex/zh-cn/wen-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://ayakaleaf-pro.ayaka.space/latex/zh-cn/zi-ti/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.
