> 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/latex-ji-chu/01-learn-latex-in-30-minutes.md).

# 创建你的第一个 LaTeX 文档

**新增：访问全新的** [**Overleaf 学习中心**](https://learn.overleaf.com/) **，探索免费的在线课程和直播网络研讨会——掌握 LaTeX 并充分利用 Overleaf 所需的一切都在这里。**

本入门教程不要求你事先具备任何 LaTeX 经验，但希望在你完成之后，你不仅能写出自己的第一份 LaTeX 文档，还能获得足够的知识和信心，迈出通往 LaTeX 熟练使用的下一步。

## 什么是 LaTeX？

LaTeX（发音为“*LAY*-tek”或“*LAH*-tek”）是一种用于排版专业外观文档的工具。然而，LaTeX 的工作方式与许多其他文档制作应用程序（如 Microsoft Word 或 LibreOffice Writer）截然不同：这些“[WYSIWYG](https://en.wikipedia.org/wiki/WYSIWYG)”工具为用户提供一个交互式页面，用户可以在其中输入和编辑文本，并应用各种样式。LaTeX 的工作方式则大不相同：它的文档是一个纯文本文件，其中穿插着 LaTeX *命令* 命令，用于表达所需的（排版后的）结果。要生成可见的排版文档，LaTeX 文件会由一种称为 *TeX 引擎* 的软件进行处理，该软件使用嵌入在文本文件中的命令来引导和控制排版过程，将 LaTeX 命令和文档文本转换为专业排版的 PDF 文件。这意味着你只需关注文档的 *内容* 内容，计算机通过 LaTeX 命令和 TeX 引擎会负责 *外观* （格式化）。

## 为什么要学习 LaTeX？

关于是否应当学习使用 LaTeX 而不是其他文档编写应用程序，可以提出各种支持或反对的理由；但归根结底，这是一种基于偏好、倾向和文档需求的个人选择。

支持 LaTeX 的理由包括：

* 支持排版极其复杂的数学、表格以及物理科学中的技术内容；
* 脚注、交叉引用和参考文献管理功能；
* 轻松生成复杂或繁琐的文档元素，如索引、术语表、目录、图表列表；
* 由于其内在的可编程性以及通过成千上万的 [免费附加包](https://www.ctan.org/pkg).

，LaTeX 为用户提供了对文档制作的极大控制，这些文档能够以极高标准排版。当然，也有一些文档或出版物类型并不适合 LaTeX，包括许多杂志类出版物中常见的“自由版式”页面设计。

LaTeX 的一个重要优点是将文档内容与文档样式分离：一旦写好了文档内容，其外观就可以轻松更改。同样，你可以创建一个定义特定文档类型版式/样式的 LaTeX 文件，并将其用作 *模板* ，以标准化该类型其他文档的撰写/制作；例如，这使得科学出版商可以在 LaTeX 中创建文章模板，作者使用这些模板撰写投稿期刊的论文。Overleaf 有一个 [包含数千个模板的图库](https://www.overleaf.com/gallery)，涵盖范围极其广泛的文档类型——从科学论文、报告和书籍到简历和演示文稿，应有尽有。由于这些模板定义了文档的版式和样式，作者只需在 Overleaf 中打开它们——创建一个新项目——然后开始写作并添加内容。

## 编写你的第一段 LaTeX

第一步是创建一个新的 LaTeX 项目。你可以在自己的电脑上创建一个新的 `.tex` 文件；或者，你也可以 [在 Overleaf 中启动一个新项目](/latex/zh-cn/zhi-shi-ku/031-creating-a-document-in-overleaf.md).

让我们从最简单的可运行示例开始，它可以直接在 Overleaf 中打开：

```latex
\documentclass{article}
\begin{document}
第一份文档。这是一个简单示例，不包含任何
额外参数或包。
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Simplest+working+example+LaTeX+document\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0AFirst+document.+This+is+a+simple+example%2C+with+no+%0Aextra+parameters+or+packages+included.%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![示例文档](/files/de666ffbf69d28df177fd52cdb6bee2a99aeaf95)

你可以看到，LaTeX 已经自动缩进了段落的第一行，替你处理了这部分格式。让我们仔细看看代码的每一部分在做什么。

代码的第一行 `\documentclass{article}`，声明了文档类型，即其 *类*类，用于控制文档的整体外观。不同类型的文档需要不同的类；例如，简历/履历所需的类与科学论文不同，后者可能使用标准 LaTeX `article` 类。你可能处理的其他类型文档也可能需要不同的类，例如 `book` 或 `report`。想了解可用的众多 LaTeX 类类型，可 [访问 CTAN（综合 TeX 归档网络）上的相关页面](https://www.ctan.org/topic/class).

在设置好文档类后，我们的内容，即文档的 *正文* ，会写在 `\begin{document}` 和 `\end{document}` 标签之间。打开上面的示例后，你可以修改文本；完成后，通过 *重新编译文档*来查看生成的排版 PDF。要在 Overleaf 中这样做，只需按下 **重新编译**，如下短视频所示：

{% embed url="<https://videos.ctfassets.net/nrgyaltdicpt/6yo2PA5aMV3OEZl5NtZKWu/54395f569b830b8183b5e0058d5bc0cc/LL30recompile.mp4>" %}

任何 Overleaf 项目都可以配置为在每次编辑时自动重新编译：点击 **重新编译** 按钮旁边的小箭头并设置 **Auto Compile** 更改为 **开启**，如下截图所示：

![显示如何将 Auto Compile 设为开或关的截图](/files/8202096498caf6116abc1f18ef700bec4c028fbb)

了解如何向文档添加内容后，下一步是给它添加标题。为此，我们需要简要谈谈 **导言区**.

## 文档的导言区

上面的截图显示 Overleaf 将一个 LaTeX 文档保存为名为 `main.tex`： `.tex` 文件扩展名按照惯例用于命名包含文档 LaTeX 代码的文件。

前面的示例展示了如何在 `\begin{document}` 命令 `.tex` 之前的文件中输入文档内容；然而，你的 *在* 在该位置 *导言区*之前的所有内容都称为 *导言区* ，它作为文档的“设置”部分。在导言区中，你可以定义文档类（类型）以及撰写文档时要使用的语言等细节；加载你想使用的 [宏包](#finding-and-using-latex-packages)（稍后会更详细地介绍这一点），并在这里应用其他类型的配置。

一个最小的文档导言区可能如下所示：

```latex
\documentclass[12pt, letterpaper]{article}
\usepackage{graphicx}
```

其中 `\documentclass[12pt, letterpaper]{article}` 定义文档的整体类（类型）。附加参数必须用逗号分隔，并放在方括号（`[...]`）中，用于配置 article 类的这个实例；也就是说，是我们希望用于这个特定 `article`-类文档的设置。

在此示例中，这两个参数的作用如下：

* `12pt` 设置字号
* `letterpaper` 设置纸张尺寸

当然也可以使用其他字号， `9pt`, `11pt`, `12pt`，但如果未指定，默认字号是 `10pt`。至于纸张尺寸，其他可能的值包括 `a4paper` 和 `legalpaper`。更多信息请参阅关于 [页面大小和页边距](/latex/zh-cn/ge-shi-hua/07-page-size-and-margins.md).

导言区中的这一行是加载外部包的一个示例（这里是

```latex
\usepackage{graphicx}
```

，用于扩展 LaTeX 的功能，使其能够导入外部图形文件。LaTeX 宏包将在 [`graphicx`](https://ctan.org/pkg/graphicx?lang=en)）这一节中讨论。 [查找和使用 LaTeX 宏包](#finding-and-using-latex-packages).

## 包含标题、作者和日期信息

要为文档添加标题、作者和日期，需要在 *导言区* (*不* （即文档主体）中再添加三行。它们是：

* `\title{My first LaTeX document}`：文档标题
* `\author{Hubert Farnsworth}`：在这里写作者姓名，并可选地使用 `\thanks` 命令：
  * `\thanks{Funded by the Overleaf team.}`：可以添加在作者姓名后面，位于 `作者` 命令的花括号中。它会添加一个上标和一个脚注，脚注文本为花括号中的内容。如果你需要在文章中感谢某个机构，这会很有用。
* `\date{August 2022}`：你可以手动输入日期，也可以使用命令 `\today` 在每次编译文档时排版当前日期

添加这些行后，你的导言区应如下所示：

```latex
\documentclass[12pt, letterpaper]{article}
\title{My first LaTeX document}
\author{Hubert Farnsworth\thanks{Funded by the Overleaf team.}}
\date{August 2022}
```

要排版标题、作者和日期，请在文档的 `\maketitle` 中使用 *正文* 命令：

```latex
\begin{document}
\maketitle
现在我们已经为第一个 \LaTeX{} 文档添加了标题、作者和日期！
\end{document}
```

现在可以将导言区和正文合并，生成一个完整的文档，并在 Overleaf 中打开：

```latex
\documentclass[12pt, letterpaper]{article}
\title{My first LaTeX document}
\author{Hubert Farnsworth\thanks{Funded by the Overleaf team.}}
\date{August 2022}
\begin{document}
\maketitle
现在我们已经为第一个 \LaTeX{} 文档添加了标题、作者和日期！
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=My+first+LaTeX+document\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%5D%7Barticle%7D%0A%5Ctitle%7BMy+first+LaTeX+document%7D%0A%5Cauthor%7BHubert+Farnsworth%5Cthanks%7BFunded+by+the+Overleaf+team.%7D%7D%0A%5Cdate%7BAugust+2022%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0AWe+have+now+added+a+title%2C+author+and+date+to+our+first+%5CLaTeX%7B%7D+document%21%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![LaTeX 生成的简单文档图片](/files/1fcee7facf1bdc1bb4a06421835a062d09960fbf)

## 添加注释

LaTeX 是一种“程序代码”，但它专注于文档排版；因此，与用其他任何编程语言编写的代码一样，在文档中加入注释会非常有用。LaTeX 注释是一段不会被排版或以任何方式影响文档的文本——通常用于添加“待办”备注；加入说明性注释；对棘手的宏提供行内解释，或者在调试时将 LaTeX 代码的某些行/部分注释掉。

在 LaTeX 中添加注释，只需在行首写一个 `%` 符号，如下面使用上面示例的代码所示：

```latex
\documentclass[12pt, letterpaper]{article}
\title{My first LaTeX document}
\author{Hubert Farnsworth\thanks{Funded by the Overleaf team.}}
\date{August 2022}
\begin{document}
\maketitle
现在我们已经为第一个 \LaTeX{} 文档添加了标题、作者和日期！

% 这一行是注释。它不会在文档中排版。
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=My+first+LaTeX+document+with+a+comment\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%5D%7Barticle%7D%0A%5Ctitle%7BMy+first+LaTeX+document%7D%0A%5Cauthor%7BHubert+Farnsworth%5Cthanks%7BFunded+by+the+Overleaf+team.%7D%7D%0A%5Cdate%7BAugust+2022%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0AWe+have+now+added+a+title%2C+author+and+date+to+our+first+%5CLaTeX%7B%7D+document%21%0A%0A%25+This+line+here+is+a+comment.+It+will+not+be+typeset+in+the+document.%0A%5Cend%7Bdocument%7D)

这个示例生成的输出与前一个不包含注释的 LaTeX 代码完全相同。

## 粗体、斜体和下划线

接下来，我们来看看一些文本格式化命令：

* **粗体**：在 LaTeX 中，粗体文本使用 `\textbf{...}` 该命令。
* *斜体*：斜体文本使用 `\textit{...}` 该命令。
* 下划线：要给文本加下划线，请使用 `\underline{...}` 该命令。

下一个示例演示这些命令：

```latex
一些\textbf{最伟大的}
在\underline{科学}中的发现
是由\textbf{\textit{偶然}}发现的。
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=First+example+with+formatted+text\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%5D%7Barticle%7D%0A%5Ctitle%7BMy+first+LaTeX+document%7D%0A%5Cauthor%7BHubert+Farnsworth%5Cthanks%7BFunded+by+the+Overleaf+team.%7D%7D%0A%5Cdate%7BAugust+2022%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0ASome+of+the+%5Ctextbf%7Bgreatest%7D%0Adiscoveries+in+%5Cunderline%7Bscience%7D+%0Awere+made+by+%5Ctextbf%7B%5Ctextit%7Baccident%7D%7D.%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![显示 LaTeX 文档中文本格式化效果的图片](/files/3bb42d412ee26bfb68af4455feb88ec8469ff62b)

另一个非常有用的命令是 `\emph{*argument*}`，它对其 `*argument*` 的效果取决于上下文。在普通文本中，强调文本会以斜体显示，但如果在斜体文本中使用，这种行为会反转——见下一个示例：

```latex
科学史上一些最伟大的 \emph{发现}
是偶然发现的。

\textit{一些最伟大的 \emph{发现}
都是偶然产生的。}

\textbf{一些最伟大的 \emph{发现}
都是偶然产生的。}
```

[打开此 `**\emph**` Overleaf 中的示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+the+emph+command\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%2C+twoside%5D%7Barticle%7D%0A%5Ctitle%7BMy+first+LaTeX+document%7D%0A%5Cauthor%7BHubert+Farnsworth%5Cthanks%7BFunded+by+the+Overleaf+team.%7D%7D%0A%5Cdate%7BAugust+2022%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cparindent0pt%25+To+remove+the+paragraph+indentation%0A%5Cmaketitle%0ASome+of+the+greatest+%5Cemph%7Bdiscoveries%7D+in+science+%0Awere+made+by+accident.%0A%0A%5Ctextit%7BSome+of+the+greatest+%5Cemph%7Bdiscoveries%7D+%0Ain+science+were+made+by+accident.%7D%0A%0A%5Ctextbf%7BSome+of+the+greatest+%5Cemph%7Bdiscoveries%7D+%0Ain+science+were+made+by+accident.%7D%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![演示 LaTeX \emph 命令用法的图片](/files/932abbeb15102eb7ee2da14d50cfb42742416212)

* **注意：** 某些宏包，例如 [Beamer](/latex/zh-cn/yan-shi-wen-gao/01-beamer.md)，会改变 `\emph` 该命令。

## 添加图片

在本节中，我们将看看如何向 LaTeX 文档添加图片。Overleaf 支持三种插入图片的方式：

1. 使用 [**插入图形** 按钮](https://learn.overleaf.com/learn/Kb/How_to_insert_figures_in_Overleaf#Using_Insert_Figure_to_add_a_figure_to_your_project)(![The Insert Figure button on the editor toolbar](/files/fadba5f0c0afa0cc0db889566e193b156d7724cf)），位于编辑器工具栏上，用于将图片插入到 **可视化编辑器** 或 **代码编辑器**.
2. [复制并粘贴图片](/latex/zh-cn/zhi-shi-ku/075-how-to-paste-formatted-content-into-overleaf.md#pasting-images-into-your-overleaf-project) 输入到 **可视化编辑器** 或 **代码编辑器**.
3. 使用 **代码编辑器** 以编写插入图形的 LaTeX 代码。

选项 1 和 2 会自动生成插入图片所需的 LaTeX 代码，但这里我们介绍选项 3——请注意，你需要将这些图片 [上传到](/latex/zh-cn/zhi-shi-ku/088-including-images-on-overleaf.md) 你的 Overleaf 项目中。下面的示例演示如何包含一张图片：

```latex
\documentclass{article}
\usepackage{graphicx} %LaTeX package to import graphics
\graphicspath{{images/}} %configuring the graphicx package

\begin{document}
宇宙是浩瀚的，并且似乎是均匀的，
在大尺度上，我们所见之处皆如此。

% \includegraphics 命令是
% 由
% graphicx 宏包提供（实现）的
\includegraphics{universe}

上面是一张星系图片。
\end{document}
```

[在 Overleaf 中打开这个图片示例。](https://www.overleaf.com/project/new/template/25686?id=107971142\&templateName=Example+using+images+in+LaTeX\&latexEngine=pdflatex\&texImage=texlive-full%3A2022.1\&mainFile=)

此示例生成如下输出：

![显示导入到 LaTeX 文档中的图片的示意图](/files/75866a76853f6cfbb3998fddbc7c86c96dca3526)

将图形导入 LaTeX 文档需要 [一个附加组件 *宏包*](#finding-and-using-latex-packages) ，它提供包含外部图形文件所需的命令和功能。上面的示例加载了 [`graphicx` 宏包](https://ctan.org/pkg/graphicx?lang=en) ，它除了许多其他命令外，还提供了 `\includegraphics{...}` 用于导入图形，以及 `\graphicspath{...}` 用来告诉 LaTeX 图形文件所在的位置。

要使用 `graphicx` 要使用该宏包，请在 Overleaf 文档的导言区加入以下一行：

```latex
\usepackage{graphicx}
```

在我们的示例中，命令 `\graphicspath{{images/}}` 告诉 LaTeX 图片保存在名为 `images`的文件夹中，该文件夹位于当前目录内：

![显示 LaTeX 访问存储在文件夹中的图片的示意图](/files/844a09213a6852be6217d6912f8c8c7ca8b9e213)

该 `\includegraphics{universe}` 命令负责在文档中实际插入图片。这里， `universe` 是图片文件名，但不包含扩展名。

**注意**:

* 虽然在 `\includegraphics` 命令中允许使用包含扩展名的完整文件名，但最佳实践是省略文件扩展名，因为这样会促使 LaTeX 搜索所有受支持的格式。
* 通常，图形文件名不应包含空格或多个点；在向 Overleaf 上传图片文件时，也建议将文件扩展名使用小写字母。

更多关于 LaTeX 宏包的信息可在本教程末尾的 [查找和使用 LaTeX 宏包](#finding-and-using-latex-packages).

## 图题、标签和引用

图片可以通过 `figure` 环境添加题注、标签并进行引用，如下所示：

```latex
\documentclass{article}
\usepackage{graphicx}
\graphicspath{{images/}}

\begin{document}

\begin{figure}[h]
    \centering
    \includegraphics[width=0.75\textwidth]{mesh}
    \caption{A nice plot.}
    \label{fig:mesh1}
\end{figure}

正如你在图 \ref{fig:mesh1} 中看到的，函数在原点附近增长。这个示例位于第 \pageref{fig:mesh1} 页。

\end{document}
```

[在 Overleaf 中打开这个图片示例。](https://www.overleaf.com/project/new/template/25519?id=107980830\&templateName=Example+using+image+captions+in+LaTeX\&latexEngine=pdflatex\&texImage=texlive-full%3A2022.1\&mainFile=)

此示例生成如下输出：

![显示 LaTeX 中图题用法的示意图](/files/00093f1841b218683e99d6efbe05377c3c46aee2)

该示例中有几个值得注意的命令：

* **`\includegraphics[width=0.75\textwidth]{mesh}`**：这种形式的 `\includegraphics` 指示 LaTeX 将图形宽度设为文本宽度的 75%——其值存储在 `\textwidth` 该命令。
* **`\caption{A nice plot.}`**：顾名思义，此命令设置图题，可放在图形上方或下方。如果你创建图表目录，这个题注将用于该目录。
* **`\label{fig:mesh1}`**：要在文档中引用这张图片，你可以使用 `\label` 命令为其添加标签。该标签用于生成图片编号，并与下一个命令结合后可让你对其进行引用。
* **`\ref{fig:mesh1}`**：这段代码会被替换为对应被引用图形的编号。

嵌入 LaTeX 文档中的图片应放在 `figure` 环境或类似环境中，以便 LaTeX 能自动将图片放在文档中的合适位置。

更多指导见以下 Overleaf 帮助文章：

* [图像位置](/latex/zh-cn/tu-xing-he-biao-ge/02-positioning-images-and-tables.md)
* [插入图片](/latex/zh-cn/geng-duo-zhu-ti/27-inserting-images.md)

## 在 LaTeX 中创建列表

你可以使用 *环境创建不同类型的列表，它们用于封装实现特定排版功能所需的 LaTeX 代码。一个环境以*开始，以 `\begin{*environment-name*}` 并以 `\end{*environment-name*}` 其中 `*environment-name*` 可以是 `figure`, `tabular` 或以下列表类型之一： `itemize` 用于无序列表，或 `enumerate` 用于有序列表。

### 无序列表

无序列表由 `itemize` 环境生成。每个列表条目都必须以前面的 `\item` 命令开头，如下所示：

```latex
\documentclass{article}
\begin{document}
\begin{itemize}
  \item 各个条目用黑点表示，这就是所谓的项目符号。
  条目中的文本可以是任意长度。
\end{itemize}
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+a+LaTeX+unordered+list\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bitemize%7D%0A++%5Citem+The+individual+entries+are+indicated+with+a+black+dot%2C+a+so-called+bullet.%0A++%5Citem+The+text+in+the+entries+may+be+of+any+length.%0A%5Cend%7Bitemize%7D%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![LaTeX 中项目符号列表示例](/files/42e0b4a908285a45068abc9555ada073e0dd72ac)

你还可以打开这个 [更大的 Overleaf 项目](https://www.overleaf.com/project/new/template/25521?id=107987258\&templateName=Demonstrating+various+types+of+LaTeX+list\&latexEngine=pdflatex\&texImage=texlive-full%3A2022.1\&mainFile=) ，其中演示了多种 LaTeX 列表。

### 有序列表

有序列表使用与无序列表相同的语法，但通过 `enumerate` 环境：

```latex
\documentclass{article}
\begin{document}
\begin{enumerate}
  \item 这是列表中的第一项。
  \item 随着我们添加每一项，列表编号都会递增。
\end{enumerate}
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+the+enumerate+environment\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Benumerate%7D%0A++%5Citem+This+is+the+first+entry+in+our+list.%0A++%5Citem+The+list+numbers+increase+with+each+entry+we+add.%0A%5Cend%7Benumerate%7D%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![LaTeX 生成的编号列表示例](/files/302945565cbc85f94dd0b19363a2870b0cf0c2b2)

与 `无序` 列表一样，每个条目都必须以前面的 `\item` 命令开头，这里该命令会自动生成有序列表的数字标签值，从 1 开始。

欲了解更多信息，你可以打开这个 [更大的 Overleaf 项目](https://www.overleaf.com/project/new/template/25521?id=107987258\&templateName=Demonstrating+various+types+of+LaTeX+list\&latexEngine=pdflatex\&texImage=texlive-full%3A2022.1\&mainFile=) 其中演示了多种 LaTeX 列表，或者访问我们专门的 [LaTeX 列表帮助文章](/latex/zh-cn/latex-ji-chu/04-lists.md)，其中提供了更多示例，并展示了如何创建自定义列表。

## 向 LaTeX 中添加数学内容

LaTeX 的主要优势之一是数学表达式易于编写。LaTeX 提供两种用于排版数学的书写模式：

* *行内* 用于编写作为段落一部分的公式的数学模式
* *显示* 用于编写不属于正文或段落、并以独立行排版的表达式的数学模式

### 行内数学模式

让我们看看行内数学模式的一个示例：

```latex
\documentclass[12pt, letterpaper]{article}
\begin{document}
在物理学中，质量-能量等价关系表述为
由方程 $E=mc^2$ 表示，该式由阿尔伯特·爱因斯坦于 1905 年发现。
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Inline+LaTeX+maths+example\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%5D%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0AIn+physics%2C+the+mass-energy+equivalence+is+stated+%0Aby+the+equation+%24E%3Dmc%5E2%24%2C+discovered+in+1905+by+Albert+Einstein.%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![显示行内数学模式排版效果的图片](/files/110c2bb946188641db71f9f8e31e7e3ee68c0598)

要排版行内数学，可以使用以下任一分隔符对： `\( ... \)`, `$ ... $` 或 `\begin{math} ... \end{math}`，如下例所示：

```latex
\documentclass[12pt, letterpaper]{article}
\begin{document}
\begin{math}
E=mc^2
\end{math} 在段落中使用行内数学模式进行排版——$E=mc^2$ 也是如此，\(E=mc^2\) 同样如此。
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Demonstrating+inline+math+mode\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%5D%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bmath%7D%0AE%3Dmc%5E2%0A%5Cend%7Bmath%7D+is+typeset+in+a+paragraph+using+inline+math+mode---as+is+%24E%3Dmc%5E2%24%2C+and+so+too+is+%5C%28E%3Dmc%5E2%5C%29.%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![演示使用 LaTeX 行内数学模式生成内容的图片](/files/d11f27009b7e5e63acb9315a006cfa8ecfbe28bb)

### 显示数学模式

显示模式排版的公式可以编号或不编号，如下例所示：

```latex
\documentclass[12pt, letterpaper]{article}
\begin{document}
质量-能量等价关系由著名方程描述
\[ E=mc^2 \] 由阿尔伯特·爱因斯坦于 1905 年发现。

在自然单位制下（$c = 1$），该公式表示恒等式
\begin{equation}
E=m
\end{equation}
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Display+math+mode+example\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%5D%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0AThe+mass-energy+equivalence+is+described+by+the+famous+equation%0A%5C%5B+E%3Dmc%5E2+%5C%5D+discovered+in+1905+by+Albert+Einstein.+%0A%0AIn+natural+units+%28%24c+%3D+1%24%29%2C+the+formula+expresses+the+identity%0A%5Cbegin%7Bequation%7D%0AE%3Dm%0A%5Cend%7Bequation%7D%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![显示 LaTeX 中显示模式数学的图片](/files/0ae2a207369337970c100ea2d6017606617be37b)

要排版显示模式数学，可以使用以下任一分隔符对： `\[ ... \]`, `\begin{displaymath} ... \end{displaymath}` 或 `\begin{equation} ... \end{equation}`。历史上，排版显示模式数学需要使用 `$$` 字符分隔符，如 `$$ *... display math here ...*$$`，但这种方法 [不再推荐](https://texfaq.org/FAQ-dolldoll)：请使用 LaTeX 的分隔符 `\[ ... \]` 代替。

### 更完整的示例

以下示例展示了使用 LaTeX 排版的一系列数学内容。

```latex
\documentclass{article}
\begin{document}
数学模式中的下标写作 $a_b$，上标写作 $a^b$。它们可以组合并嵌套，以写出如下表达式：

\[ T^{i_1 i_2 \dots i_p}_{j_1 j_2 \dots j_q} = T(x^{i_1},\dots,x^{i_p},e_{j_1},\dots,e_{j_q}) \]

积分用 $\int$ 表示，分数用 $\frac{a}{b}$ 表示。积分的上下限使用上标和下标表示：

\[ \int_0^1 \frac{dx}{e^x} =  \frac{e-1}{e} \]

小写希腊字母写作 $\omega$ $\delta$ 等，而大写希腊字母写作 $\Omega$ $\Delta$。

数学运算符前面加反斜杠，如 $\sin(\beta)$、$\cos(\alpha)$、$\log(x)$ 等。
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=More+advanced+LaTeX+math+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0ASubscripts+in+math+mode+are+written+as+%24a_b%24+and+superscripts+are+written+as+%24a%5Eb%24.+These+can+be+combined+and+nested+to+write+expressions+such+as%0A%0A%5C%5B+T%5E%7Bi_1+i_2+%5Cdots+i_p%7D_%7Bj_1+j_2+%5Cdots+j_q%7D+%3D+T%28x%5E%7Bi_1%7D%2C%5Cdots%2Cx%5E%7Bi_p%7D%2Ce_%7Bj_1%7D%2C%5Cdots%2Ce_%7Bj_q%7D%29+%5C%5D%0A+%0AWe+write+integrals+using+%24%5Cint%24+and+fractions+using+%24%5Cfrac%7Ba%7D%7Bb%7D%24.+Limits+are+placed+on+integrals+using+superscripts+and+subscripts%3A%0A%0A%5C%5B+%5Cint_0%5E1+%5Cfrac%7Bdx%7D%7Be%5Ex%7D+%3D++%5Cfrac%7Be-1%7D%7Be%7D+%5C%5D%0A%0ALower+case+Greek+letters+are+written+as+%24%5Comega%24+%24%5Cdelta%24+etc.+while+upper+case+Greek+letters+are+written+as+%24%5COmega%24+%24%5CDelta%24.%0A%0AMathematical+operators+are+prefixed+with+a+backslash+as+%24%5Csin%28%5Cbeta%29%24%2C+%24%5Ccos%28%5Calpha%29%24%2C+%24%5Clog%28x%29%24+etc.%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![显示 LaTeX 中多种数学排版效果的图片](/files/dcb47f5f85e2c17872d1fed0fdfa5811a0365cc4)

下一个示例使用了 `equation*` 由该 `amsmath` 宏包，因此我们需要在文档导言区添加以下一行：

```latex
\usepackage{amsmath}% For the equation* environment
```

有关使用 `amsmath` 参见 [我们的帮助文章](/latex/zh-cn/shu-xue/06-aligning-equations-with-amsmath.md).

```latex
\documentclass{article}
\usepackage{amsmath}% For the equation* environment
\begin{document}
\section{First example}

众所周知的毕达哥拉斯定理 \(x^2 + y^2 = z^2\) 已被证明对其他指数并不成立，这意味着下面的方程在 \(n>2\) 时没有整数解：

\[ x^n + y^n = z^n \]

\section{Second example}

这是一条文本中的简单数学表达式 \(\sqrt{x^2+1}\)。
这也是同样的：
\begin{math}
\sqrt{x^2+1}
\end{math}
但使用了另一个命令。

这是一个不带编号的简单数学表达式
\[\sqrt{x^2+1}\]
与文本分离。

这也一样：
\begin{displaymath}
\sqrt{x^2+1}
\end{displaymath}

……而这个也是：
\begin{equation*}
\sqrt{x^2+1}
\end{equation*}
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=More+math+content+typeset+in+LaTeX\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamsmath%7D%25+For+the+equation%2A+environment%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BFirst+example%7D%0A%0AThe+well-known+Pythagorean+theorem+%5C%28x%5E2+%2B+y%5E2+%3D+z%5E2%5C%29+was+proved+to+be+invalid+for+other+exponents%2C+meaning+the+next+equation+has+no+integer+solutions+for+%5C%28n%3E2%5C%29%3A%0A%0A%5C%5B+x%5En+%2B+y%5En+%3D+z%5En+%5C%5D%0A%0A%5Csection%7BSecond+example%7D%0A%0AThis+is+a+simple+math+expression+%5C%28%5Csqrt%7Bx%5E2%2B1%7D%5C%29+inside+text.+%0AAnd+this+is+also+the+same%3A+%0A%5Cbegin%7Bmath%7D%0A%5Csqrt%7Bx%5E2%2B1%7D%0A%5Cend%7Bmath%7D%0Abut+by+using+another+command.%0A%0AThis+is+a+simple+math+expression+without+numbering%0A%5C%5B%5Csqrt%7Bx%5E2%2B1%7D%5C%5D+%0Aseparated+from+text.%0A%0AThis+is+also+the+same%3A%0A%5Cbegin%7Bdisplaymath%7D%0A%5Csqrt%7Bx%5E2%2B1%7D%0A%5Cend%7Bdisplaymath%7D%0A%0A%5Cldots+and+this%3A%0A%5Cbegin%7Bequation%2A%7D%0A%5Csqrt%7Bx%5E2%2B1%7D%0A%5Cend%7Bequation%2A%7D%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![显示 LaTeX 中多种数学排版效果的图片](/files/b086508e92c0f66330b7860df758fe675becf854)

LaTeX 中数学的可能性是无穷无尽的，因此务必访问我们的帮助页面，获取关于特定主题的建议和示例：

* [数学表达式](/latex/zh-cn/shu-xue/01-mathematical-expressions.md)
* [下标和上标](/latex/zh-cn/shu-xue/02-subscripts-and-superscripts.md)
* [方括号和圆括号](/latex/zh-cn/shu-xue/03-brackets-and-parentheses.md)
* [分数和二项式](/latex/zh-cn/shu-xue/05-fractions-and-binomials.md)
* [对齐公式](/latex/zh-cn/shu-xue/06-aligning-equations-with-amsmath.md)
* [运算符](/latex/zh-cn/shu-xue/07-operators.md)
* [数学模式中的间距](/latex/zh-cn/shu-xue/08-spacing-in-math-mode.md)
* [积分、求和和极限](/latex/zh-cn/shu-xue/09-integrals-sums-and-limits.md)
* [数学模式中的显示样式](/latex/zh-cn/shu-xue/10-display-style-in-math-mode.md)
* [希腊字母和数学符号列表](/latex/zh-cn/shu-xue/11-list-of-greek-letters-and-math-symbols.md)
* [数学字体](/latex/zh-cn/shu-xue/12-mathematical-fonts.md)

## 基本文档结构

接下来，我们将探讨摘要，以及如何将 LaTeX 文档划分为不同的章、节和段落。

### 摘要

科学文章通常会提供一个 *摘要* ，它是对其核心主题或论点的简短概述/摘要。下一个示例演示如何使用 LaTeX 的 `摘要` 环境：

```latex
\documentclass{article}
\begin{document}
\begin{abstract}
这是位于
文档开头的一段简单段落。对主要主题的简短介绍。
\end{abstract}
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=How+to+create+a+document+abstract\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Babstract%7D%0AThis+is+a+simple+paragraph+at+the+beginning+of+the+%0Adocument.+A+brief+introduction+about+the+main+subject.%0A%5Cend%7Babstract%7D%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![显示 LaTeX 排版摘要的图片](/files/cda4f55248a3a5b1d34730992d3814290342312e)

### 段落和新行

有了摘要之后，我们就可以开始写第一段了。下一个示例演示：

* 如何通过连续按两次“enter”键创建新段落，从而结束当前行并插入一个空行；
* 如何通过使用 `\\` 命令插入手动换行来在不开始新段落的情况下另起一行，该命令是双反斜杠；或者使用 `\newline` 该命令。

本示例中的第三段演示了以下命令的用法： `\\` 和 `\newline`:

```latex
\documentclass{article}
\begin{document}

\begin{abstract}
这是位于
文档开头的一段简单段落。对主要主题的简短介绍。
\end{abstract}

在摘要之后，我们可以开始第一段，然后按两次 ``enter'' 开始第二段。

这一行将开始第二段。

我将开始第三段，然后添加 \\ 一个手动换行，这会使这段文字从新行开始，但仍属于同一段。或者，我可以使用 \verb|\newline|\newline 命令开始新行，这也属于同一段。
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+to+create+a+new+paragraph\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cbegin%7Babstract%7D%0AThis+is+a+simple+paragraph+at+the+beginning+of+the+%0Adocument.+A+brief+introduction+about+the+main+subject.%0A%5Cend%7Babstract%7D%0A%0AAfter+our+abstract+we+can+begin+the+first+paragraph%2C+then+press+%60%60enter%27%27+twice+to+start+the+second+one.%0A%0AThis+line+will+start+a+second+paragraph.%0A%0AI+will+start+the+third+paragraph+and+then+add+%5C%5C+a+manual+line+break+which+causes+this+text+to+start+on+a+new+line+but+remains+part+of+the+same+paragraph.+Alternatively%2C+I+can+use+the+%5Cverb%7C%5Cnewline%7C%5Cnewline+command+to+start+a+new+line%2C+which+is+also+part+of+the+same+paragraph.%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![显示 LaTeX 中段落创建的图片](/files/758623695162dca8816162c9ecafe6ea186f4215)

请注意 LaTeX 会自动缩进段落——但在诸如 section 和 subsection 之类的文档标题之后除外——[正如我们将看到的](#chapters-and-sections).

新用户应注意，多个 `\\` 或 `\newline`空格不应被用于“模拟”段落，并在它们之间留出更大的间距，因为这可能会干扰 LaTeX 的排版算法。推荐的方法是继续使用空白行来创建新段落，而不使用任何 `\\`，并加载 [`parskip` 宏包](https://ctan.org/pkg/parskip?lang=en) 通过添加 `\usepackage{parskip}` 到导言区。

关于段落的更多信息可见以下文章：

* [段落和新行](/latex/zh-cn/latex-ji-chu/02-paragraphs-and-new-lines.md)
* [如何在 LaTeX 中更改段落间距](/latex/zh-cn/ge-shi-hua/04-articles-how-to-change-paragraph-spacing-in-latex.md)
* [LaTeX 错误：这里没有可结束的行](/latex/zh-cn/latex-cuo-wu/18-latex-error-there-s-no-line-here-to-end.md) 提供了关于使用的更多建议和指导 `\\`.

### 章和节

较长的文档，无论使用何种写作软件，通常都会划分为部分、章、节、小节等等。LaTeX 也提供了文档结构化命令，但可用的命令及其实现方式（它们的作用）可能取决于所使用的文档类。举例来说，使用 `book` 类创建的文档可以被划分为部分、章、节、小节等等，但 `letter` 类并不提供（支持）任何用于执行此操作的命令。

下面的示例演示用于基于……来构建文档结构的命令 `book` 类：

```latex
\documentclass{book}
\begin{document}

\chapter{第一章}

\section{Introduction}

这是第一节。

Lorem  ipsum  dolor  sit  amet,  consectetuer  adipiscing
elit. Etiam  lobortisfacilisis sem.  Nullam nec mi et
neque pharetra sollicitudin.  Praesent imperdietmi nec ante.
Donec ullamcorper, felis non sodales...

\section{Second Section}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisissem.  Nullam nec mi et neque pharetra
sollicitudin.  Praesent imperdiet mi necante...

\subsection{First Subsection}
Praesent imperdietmi nec ante. Donec ullamcorper, felis non sodales...

\section*{Unnumbered Section}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisissem...
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Book+class+example+document\&snip=%5Cdocumentclass%7Bbook%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cchapter%7BFirst+Chapter%7D%0A%0A%5Csection%7BIntroduction%7D%0A%0AThis+is+the+first+section.%0A%0ALorem++ipsum++dolor++sit++amet%2C++consectetuer++adipiscing++%0Aelit.+Etiam++lobortisfacilisis+sem.++Nullam+nec+mi+et+%0Aneque+pharetra+sollicitudin.++Praesent+imperdietmi+nec+ante.+%0ADonec+ullamcorper%2C+felis+non+sodales...%0A%0A%5Csection%7BSecond+Section%7D%0A%0ALorem+ipsum+dolor+sit+amet%2C+consectetuer+adipiscing+elit.++%0AEtiam+lobortis+facilisissem.++Nullam+nec+mi+et+neque+pharetra+%0Asollicitudin.++Praesent+imperdiet+mi+necante...%0A%0A%5Csubsection%7BFirst+Subsection%7D%0APraesent+imperdietmi+nec+ante.+Donec+ullamcorper%2C+felis+non+sodales...%0A%0A%5Csection%2A%7BUnnumbered+Section%7D%0ALorem+ipsum+dolor+sit+amet%2C+consectetuer+adipiscing+elit.++%0AEtiam+lobortis+facilisissem...%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![显示 LaTeX book 文档中各节的图片](/files/c8207444f0c89ba2dff32256af53bc01467516e1)

分节命令的名称大多不言自明；例如， `\chapter{第一章}` 会创建一个名为 `第一章`, `\section{Introduction}` 会生成一个标题为 `引言`，依此类推。各节还可以进一步划分为 `\subsection{...}` 甚至 `\subsubsection{...}`。各节、小节等的编号是自动生成的，但可以通过使用所谓的 *带星号的版本* 来关闭，即在相应命令末尾加上星号（\*），例如 `\section*{...}` 和 `\subsection*{...}`.

*总的来说*，LaTeX 文档类提供了以下分节命令，不同的具体类只支持其中相关的子集：

* `\part{part}`
* `\chapter{chapter}`
* `\section{section}`
* `\subsection{subsection}`
* `\subsubsection{subsubsection}`
* `\paragraph{paragraph}`
* `\subparagraph{subparagraph}`

特别是， `\part` 和 `\chapter` 这些命令只在 `report` 和 `book` 文档类中可用。

请访问 Overleaf 文章 [关于节和章的文章](/latex/zh-cn/wen-dang-jie-gou/01-sections-and-chapters.md) 以获取有关文档结构命令的更多信息。

## 创建表格

Overleaf 提供三种创建表格的方式：

1. 使用 [**插入表格** 按钮](/latex/zh-cn/zhi-shi-ku/067-how-to-generate-and-insert-latex-tables-in-overleaf.md#inserttable) 位于 **可视化编辑器** （或者 **代码编辑器**) 工具栏。
2. [复制并粘贴表格](/latex/zh-cn/zhi-shi-ku/075-how-to-paste-formatted-content-into-overleaf.md#pastingtables) 从另一份文档中，同时使用 **可视化编辑器**.
3. 在代码编辑器中编写该表格的 LaTeX 代码。

如果你是 LaTeX 新手，使用其中的工具栏 **可视化编辑器** (选项 1) 是一个很好的入门方式——你可以在……之间切换 **可视化编辑器** 和 **代码编辑器** 以查看表格背后的代码。

这里我们重点介绍选项 3——创建表格最灵活的方法——并提供示例，演示如何在 LaTeX 中创建表格，包括添加线条（规则）和标题。随着经验的积累，请查看我们关于……的详细指南 [如何使用 LaTeX 创建表格](/latex/zh-cn/tu-xing-he-biao-ge/01-tables.md).

### 在 LaTeX 中创建基本表格

我们先来看一个展示如何排版基本表格的示例：

```latex
\begin{center}
\begin{tabular}{c c c}
 cell1 & cell2 & cell3 \\
 cell4 & cell5 & cell6 \\
 cell7 & cell8 & cell9
\end{tabular}
\end{center}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=An+example+of+a+basic+table\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bcenter%7D%0A%5Cbegin%7Btabular%7D%7Bc+c+c%7D%0A+cell1+%26+cell2+%26+cell3+%5C%5C+%0A+cell4+%26+cell5+%26+cell6+%5C%5C++%0A+cell7+%26+cell8+%26+cell9++++%0A%5Cend%7Btabular%7D%0A%5Cend%7Bcenter%7D%0A%5Cend%7Bdocument%7D%0A%3C%2Fsource%3E)

此示例生成如下输出：

![显示用 LaTeX 排版的表格的图示](/files/08992d3a82adb4b7d61d488073e0ac366ebe4aeb)

该 `tabular` 环境是 LaTeX 创建表格的默认方法。你必须为该环境指定一个参数，在本例中是 `{c c c}` 这表示告知 LaTeX 这里将有三列，并且每一列中的文本都必须居中。你也可以使用 `r` 使文本右对齐，以及 `l` 使其左对齐。对齐符号 `&` 用于标示表格行中的各个单元格。要结束一个表格行，请使用 *换行* 命令 `\\`。我们的表格包含在一个 `center` 环境中，以使其在页面正文宽度内居中。

### 添加边框

该 `tabular` 环境支持表格中的水平线和垂直线（规则）：

* 要添加水平线，即在行的上方和下方添加线，请使用 `\hline` 命令
* 要添加垂直线，即在列之间添加线，请使用竖线参数 `|`

在此示例中，参数是 `{|c|c|c|}` 这声明了三列（居中），每列之间都由一条竖线（规则）分隔；此外，我们使用 `\hline` 在第一行上方和最后一行下方放置一条水平线：

```latex
\begin{center}
\begin{tabular}{|c|c|c|}
 \hline
 cell1 & cell2 & cell3 \\
 cell4 & cell5 & cell6 \\
 cell7 & cell8 & cell9 \\
 \hline
\end{tabular}
\end{center}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Table+example+with+rules\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bcenter%7D%0A%5Cbegin%7Btabular%7D%7B%7Cc%7Cc%7Cc%7C%7D+%0A+%5Chline%0A+cell1+%26+cell2+%26+cell3+%5C%5C+%0A+cell4+%26+cell5+%26+cell6+%5C%5C+%0A+cell7+%26+cell8+%26+cell9+%5C%5C+%0A+%5Chline%0A%5Cend%7Btabular%7D%0A%5Cend%7Bcenter%7D%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![显示包含水平线和垂直线的 LaTeX 排版表格的图示](/files/67ba944a17c0cabf132d0c63ad07bd762f189f91)

下面是另一个示例：

```latex
\begin{center}
 \begin{tabular}{||c c c c||}
 \hline
 Col1 & Col2 & Col2 & Col3 \\ [0.5ex]
 \hline\hline
 1 & 6 & 87837 & 787 \\
 \hline
 2 & 7 & 78 & 5415 \\
 \hline
 3 & 545 & 778 & 7507 \\
 \hline
 4 & 545 & 18744 & 7560 \\
 \hline
 5 & 88 & 788 & 6344 \\ [1ex]
 \hline
\end{tabular}
\end{center}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Another+table+example+containing+rules\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bcenter%7D%0A+%5Cbegin%7Btabular%7D%7B%7C%7Cc+c+c+c%7C%7C%7D+%0A+%5Chline%0A+Col1+%26+Col2+%26+Col2+%26+Col3+%5C%5C+%5B0.5ex%5D+%0A+%5Chline%5Chline%0A+1+%26+6+%26+87837+%26+787+%5C%5C+%0A+%5Chline%0A+2+%26+7+%26+78+%26+5415+%5C%5C%0A+%5Chline%0A+3+%26+545+%26+778+%26+7507+%5C%5C%0A+%5Chline%0A+4+%26+545+%26+18744+%26+7560+%5C%5C%0A+%5Chline%0A+5+%26+88+%26+788+%26+6344+%5C%5C+%5B1ex%5D+%0A+%5Chline%0A%5Cend%7Btabular%7D%0A%5Cend%7Bcenter%7D%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![显示包含水平线和垂直线的 LaTeX 排版表格的图示](/files/dc98b48282c119da952e8f1446d392d1dda3e5f1)

### 图题、标签和引用

你可以像处理图片一样为表格添加标题并进行引用。唯一的区别是，不是使用 **`figure`** 环境，而是使用 **`table`** 环境中。

```latex
表 \ref{table:data} 展示了如何添加表格标题并引用表格。
\begin{table}[h!]
\centering
\begin{tabular}{||c c c c||}
 \hline
 Col1 & Col2 & Col2 & Col3 \\ [0.5ex]
 \hline\hline
 1 & 6 & 87837 & 787 \\
 2 & 7 & 78 & 5415 \\
 3 & 545 & 778 & 7507 \\
 4 & 545 & 18744 & 7560 \\
 5 & 88 & 788 & 6344 \\ [1ex]
 \hline
\end{tabular}
\caption{Table to test captions and labels.}
\label{table:data}
\end{table}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Table+captions+and+references\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0ATable+%5Cref%7Btable%3Adata%7D+shows+how+to+add+a+table+caption+and+reference+a+table.%0A%5Cbegin%7Btable%7D%5Bh%21%5D%0A%5Ccentering%0A%5Cbegin%7Btabular%7D%7B%7C%7Cc+c+c+c%7C%7C%7D+%0A+%5Chline%0A+Col1+%26+Col2+%26+Col2+%26+Col3+%5C%5C+%5B0.5ex%5D+%0A+%5Chline%5Chline%0A+1+%26+6+%26+87837+%26+787+%5C%5C+%0A+2+%26+7+%26+78+%26+5415+%5C%5C%0A+3+%26+545+%26+778+%26+7507+%5C%5C%0A+4+%26+545+%26+18744+%26+7560+%5C%5C%0A+5+%26+88+%26+788+%26+6344+%5C%5C+%5B1ex%5D+%0A+%5Chline%0A%5Cend%7Btabular%7D%0A%5Ccaption%7BTable+to+test+captions+and+labels.%7D%0A%5Clabel%7Btable%3Adata%7D%0A%5Cend%7Btable%7D%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![带标题的 LaTeX 表格](/files/d4634d78032864a71a40f13eca58c8d4411a2de7)

## 添加目录

创建目录很简单，因为命令 `\tableofcontents` 几乎替你完成了所有工作：

```latex
\documentclass{article}
\title{Sections and Chapters}
\author{Gubert Farnsworth}
\date{August 2022}
\begin{document}

\maketitle

\tableofcontents

\section{Introduction}

这是第一节。

Lorem  ipsum  dolor  sit  amet,  consectetuer  adipiscing
elit.   Etiam  lobortisfacilisis sem.  Nullam nec mi et
neque pharetra sollicitudin.  Praesent imperdietmi nec ante.
Donec ullamcorper, felis non sodales...

\section*{Unnumbered Section}
\addcontentsline{toc}{section}{Unnumbered Section}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisissem.  Nullam nec mi et neque pharetra
sollicitudin.  Praesent imperdiet mi necante...

\section{Second Section}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisissem.  Nullam nec mi et neque pharetra
sollicitudin.  Praesent imperdiet mi necante...
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Generating+a+table+of+contents\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Ctitle%7BSections+and+Chapters%7D%0A%5Cauthor%7BGubert+Farnsworth%7D%0A%5Cdate%7BAugust+2022%7D%0A%5Cbegin%7Bdocument%7D%0A++%0A%5Cmaketitle%0A++%0A%5Ctableofcontents%0A%0A%5Csection%7BIntroduction%7D%0A+++%0AThis+is+the+first+section.%0A++++++%0ALorem++ipsum++dolor++sit++amet%2C++consectetuer++adipiscing++%0Aelit.+++Etiam++lobortisfacilisis+sem.++Nullam+nec+mi+et+%0Aneque+pharetra+sollicitudin.++Praesent+imperdietmi+nec+ante.+%0ADonec+ullamcorper%2C+felis+non+sodales...%0A+++++++%0A%5Csection%2A%7BUnnumbered+Section%7D%0A%5Caddcontentsline%7Btoc%7D%7Bsection%7D%7BUnnumbered+Section%7D%0A%0ALorem+ipsum+dolor+sit+amet%2C+consectetuer+adipiscing+elit.++%0AEtiam+lobortis+facilisissem.++Nullam+nec+mi+et+neque+pharetra+%0Asollicitudin.++Praesent+imperdiet+mi+necante...%0A%0A%5Csection%7BSecond+Section%7D%0A+++++++%0ALorem+ipsum+dolor+sit+amet%2C+consectetuer+adipiscing+elit.++%0AEtiam+lobortis+facilisissem.++Nullam+nec+mi+et+neque+pharetra+%0Asollicitudin.++Praesent+imperdiet+mi+necante...%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![显示由 LaTeX 生成的目录的图示](/files/bd4c7b917ee600bc26cf2699580a5e7258926127)

节、小节和章会自动包含在目录中。若要手动添加条目，例如无编号节，请使用命令 `\addcontentsline` 如示例所示。

## 下载完成的文档

以下简短视频演示如何下载项目的源代码或排版后的 PDF 文件：

{% embed url="<https://videos.ctfassets.net/nrgyaltdicpt/2n6iw0AUELNtABCuva0oV5/cf27bff3a6099da94362d64645e69dfe/LL30download.mp4>" %}

更多信息请参见 Overleaf 帮助文章 [从 Overleaf 导出你的作品](/latex/zh-cn/zhi-shi-ku/036-exporting-your-work-from-overleaf.md).

## 查找和使用 LaTeX 宏包

LaTeX 不仅提供强大的排版能力，还提供了一个 *可扩展性的框架* 通过使用附加 *导言区*。LaTeX 的设计并不是试图提供“包罗万象”的命令和功能，而是旨在成为 *可扩展的*，允许用户加载外部代码体（宏包），以提供更专业的排版功能或扩展 LaTeX 内置功能——例如表格排版。正如在该部分中所观察到的 [添加图片](#adding-images)， `graphicx` 宏包通过提供导入图像文件的命令来扩展 LaTeX，并通过在导言区写入

```latex
\usepackage{graphicx}
```

### 加载宏包

如上所述，宏包通过以下命令在文档导言区加载： `\usepackage` 命令，但由于（许多）LaTeX 宏包提供一组 *选项*，可用于配置其行为，因此 `\usepackage` 命令通常如下所示：

```latex
\usepackage[options]{somepackage}
```

方括号“`[...]`”会告诉 LaTeX 在加载时应应用哪组选项 `somepackage`。在用户请求的选项集中，各个选项或设置通常用逗号分隔；例如， [`geometry` 宏包](https://ctan.org/pkg/geometry) 提供了许多用于配置 LaTeX 页面布局的选项，因此其典型用法 `geometry` 可能如下所示：

```latex
\usepackage[total={6.5in,8.75in},
top=1.2in, left=0.9in, includefoot]{geometry}
```

该 `geometry` 宏包就是由全球 LaTeX 社区成员编写并贡献的一个示例，并且免费提供给任何想使用它的人。

如果 LaTeX 宏包不提供任何选项，或者用户想使用宏包选项的默认值，则可以这样加载：

```latex
\usepackage{somepackage}
```

当你在 `\usepackage[...]{somepackage}` LaTeX 会查找一个名为 `*somepackage*.sty`的对应文件，它需要加载并处理该文件——以使宏包命令可用并执行该宏包提供的其他代码。如果 LaTeX 找不到 `*somepackage*.sty` 它，就会报错终止，如下面的 Overleaf 示例所示：

```latex
\documentclass[12pt, letterpaper]{article}
\usepackage{somepackage}% a NON-EXISTENT package
\begin{document}
这将失败！
\end{document}
```

[打开此 ***会产生错误的示例*** 在 Overleaf 中](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Error+due+to+missing+package\&snip=%5Cdocumentclass%5B12pt%2C+letterpaper%5D%7Barticle%7D%0A%5Cusepackage%7Bsomepackage%7D%25+a+NON-EXISTENT+package%0A%5Cbegin%7Bdocument%7D%0AThis+will+fail%21%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![显示由缺失宏包引起的错误的图片](/files/dd2853e94938ef2adddae72ec30713dd89e633c8)

### 查找宏包信息：CTAN

宏包通过 [Comprehensive TeX Archive Network](https://www.ctan.org/)，通常简称为 CTAN；截至撰写时，它托管了来自 2881 位贡献者的 6287 个宏包。CTAN [将自己描述为](https://www.ctan.org/ctan) as

> ……一个遍布全球、提供 TEX 相关材料下载的网站集合。

你可以浏览 CTAN 寻找有用的宏包；例如：

* [按主题](https://www.ctan.org/topics/cloud)
* [按字母顺序](https://www.ctan.org/pkg) （如果你知道宏包名称，这很有用）

你还可以使用 [搜索功能](https://www.ctan.org/pkg) （位于页面顶部）。

### Overleaf 上可用的宏包：介绍 TeX Live

每年一次，一个（大型） *子集* 由 CTAN 托管的宏包，以及与 LaTeX 相关的字体和其他软件，会被汇总并作为一个名为 [TeX Live](https://tug.org/texlive/)的系统发布，它可用于安装你自己的（本地）LaTeX 环境。事实上， [Overleaf 的服务器也使用 TeX Live](/latex/zh-cn/lei-wen-jian/02-overleaf-and-tex-live.md) 并会在 TeX Live 发布新版本时更新。Overleaf 的 TeX Live 更新不会立即进行，而是在发布后几个月才进行，这样我们就有时间对新 TeX Live 版本与 [我们图库中成千上万的模板](https://www.overleaf.com/gallery)进行兼容性测试。例如，这是我们的 [TeX Live 2025 升级公告](https://www.overleaf.com/blog/tex-live-2025-is-now-available).

尽管 TeX Live 包含一个（庞大的） *子集* CTAN 宏包集合，但仍然可能找到一个有趣的宏包，例如 [`igo` 用于排版围棋图](https://ctan.org/pkg/igo?lang=en)，它托管在 CTAN 上，但未包含在（由）TeX Live 中，因此在 Overleaf 上不可用。CTAN 上托管的一些宏包并不是 TeX Live 的一部分，原因多种多样：也许某个宏包已经过时、存在许可问题、非常新（刚上传不久），或者有平台依赖，例如能在 Windows 上运行但不能在 Linux 上运行。

新宏包以及现有宏包的更新会全年上传到 CTAN，而 TeX Live 的更新则按年发布；因此，当前版本 TeX Live 中包含的宏包不会像 CTAN 上托管的那些那样最新。由于 Overleaf 的服务器使用 TeX Live，我们服务器上安装的宏包——即用户可用的那些——可能并不是 CTAN 上可获得的最新版本，但通常这不太可能造成问题。


---

# 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/latex-ji-chu/01-learn-latex-in-30-minutes.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.
