> 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/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-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{字体示例文档}
\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{字体示例文档}
\author{Overleaf}
\date{\today}
\begin{document}

\maketitle

这是一段使用 \textit{示例} 的文档，编译时采用了
\textbf{XeLaTeX} 编译器。如果你需要写一些代码，可以
使用 \texttt{verbatim} 环境：

    \begin{verbatim}
    通常该环境用于显示代码，
    所以这里有一个简单的 C 程序。

    #include <stdio.h>
    #define NUMYAYS 10

    int main()
    {
        int i;
        for (i = 0; i <= NUMYAYS; i++)
        {
            printf("耶！Overleaf 太棒了！\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/) 你可以通过 `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{使用 Google Noto 字体}
\author{Overleaf}
\date{2021 年 4 月}

\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{bold}
\item \verb|\textit{italic}| 会生成 \textit{italic}
\item \verb|\textbf{\textit{bold italic}}| 会生成 \textbf{\textit{bold italic}}
\item \verb|\emph{emphasis}| 会生成 \emph{emphasis}
\item \verb|\textbf{\emph{bold italic}}| 会生成 \textbf{\emph{bold italic}}
\end{itemize}

\subsection{等宽字体}
你可以使用 Noto 的等宽字体来排版 \texttt{regular} 和 \texttt{\textbf{bold}} 等宽文本。

\subsection{无衬线字体}
这里有一些 \textsf{以无衬线字体排版的文本}，以及 \textbf{\textsf{以粗体无衬线字体排版的文本}}。

\section{进一步阅读}
\texttt{noto} 宏包的文档可在其 \href{http://mirrors.ctan.org/fonts/noto/README}{CTAN 上的 \color{blue}\texttt{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) 为与 LuaLaTeX 或 XƎLaTeX 配合使用而配置 OpenType 字体提供了丰富的选项——其 [出色的文档](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://overleaf-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.
