> 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/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）大不相同：那些“[所见即所得](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` 或 `报告`。要了解可用的许多 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 项目都可以配置为在每次编辑时自动重新编译：点击 **重新编译** 按钮旁边的小箭头，并设置 **自动编译** 更改为 **开启**，如下图所示：

![显示如何将自动编译设置为开启或关闭的截图](/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}
```

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

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

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

* `\title{我的第一份 LaTeX 文档}`：文档标题
* `\author{Hubert Farnsworth}`：这里写作者姓名，并可选地写 `\thanks` 命令，位于花括号中：
  * `\thanks{由 Overleaf 团队资助。}`：可以在作者姓名后添加，放在 `author` 命令的花括号内。它会添加一个上标和一个脚注，脚注文字就是花括号中的内容。如果你需要在文章中感谢某个机构，这很有用。
* `\date{2022 年 8 月}`：你可以手动输入日期，也可以使用命令 `\today` ，以便在每次编译文档时排版当前日期

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

```latex
\documentclass[12pt, letterpaper]{article}
\title{我的第一份 LaTeX 文档}
\author{Hubert Farnsworth\thanks{由 Overleaf 团队资助。}}
\date{2022 年 8 月}
```

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

```latex
\begin{document}
\maketitle
命令：
\end{document}
```

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

```latex
\documentclass[12pt, letterpaper]{article}
\title{我的第一份 LaTeX 文档}
\author{Hubert Farnsworth\thanks{由 Overleaf 团队资助。}}
\date{2022 年 8 月}
\begin{document}
\maketitle
命令：
\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{我的第一份 LaTeX 文档}
\author{Hubert Farnsworth\thanks{由 Overleaf 团队资助。}}
\date{2022 年 8 月}
\begin{document}
\maketitle
命令：

% 这里这一行是注释。它不会被排版到文档中。
\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{发现}
是偶然发现的。}
```

[在 Overleaf 中打开此 `**\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 宏包
\graphicspath{{images/}} %配置 graphicx 宏包

\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{一幅不错的图。}
    \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{一幅不错的图。}`**：顾名思义，此命令设置图注，可放在图形上方或下方。如果你创建图表清单，此图注将用于该清单。
* **`\label{fig:mesh1}`**：要在文档中引用此图像，请使用 `\label` 命令为其指定一个标签。该标签用于为图像生成编号，并结合下一个命令即可对其进行引用。
* **`\ref{fig:mesh1}`**：这段代码会被替换为与所引用图形对应的编号。

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

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

* [图形定位](/latex/zh-cn/tu-biao-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 各个条目用黑点表示，即所谓的项目符号。
  \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}`。从历史上看，排版显示模式数学需要使用 `$$` 字符分隔符，如 `$$ *... 这里是显示数学 ... *$$`，但这种方法 [已不再推荐](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}% 用于 equation* 环境
```

欲了解更多关于使用 `amsmath` 参见 [的信息，请参阅我们的帮助文章](/latex/zh-cn/shu-xue/06-aligning-equations-with-amsmath.md).

```latex
\documentclass{article}
\usepackage{amsmath}% 用于 equation* 环境
\begin{document}
\section{第一个示例}

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

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

\section{第二个示例}

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

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

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

\ldots 还有这个：
\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 文档划分为不同的章节、节和段落。

### 摘要

科学文章通常会提供一个 *abstract* 摘要，这是一份对其核心主题或论点的简要概述/总结。下面的示例演示了使用 LaTeX 的 `abstract` 环境：

```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)

### 段落和新行

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

* 通过连续按两次“回车”键创建新段落，从而结束当前行并插入后续空行；
* 如何通过使用 `\\` 命令来插入手动换行，从而在不开始新段落的情况下换到新行；它是一个双反斜杠；或者，使用 `\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{引言}

这是第一节。

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{第二节}

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

\subsection{第一小节}
Praesent imperdietmi nec ante. Donec ullamcorper, felis non sodales...

\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 书籍文档中各级章节的图片](/files/c8207444f0c89ba2dff32256af53bc01467516e1)

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

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

* `\part{部分}`
* `\chapter{章}`
* `\section{节}`
* `\subsection{小节}`
* `\subsubsection{子小节}`
* `\paragraph{段落}`
* `\subparagraph{子段落}`

尤其是， `\part` 和 `\chapter` 命令仅在 `报告` 和 `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-biao-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{用于测试标题和标签的表格。}
\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{章节与分节}
\author{Gubert Farnsworth}
\date{2022 年 8 月}
\begin{document}

\maketitle

\tableofcontents

\section{引言}

这是第一节。

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*{未编号节}
\addcontentsline{toc}{section}{未编号节}

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

\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
\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}% 一个不存在的宏包
\begin{document}
这会失败！
\end{document}
```

[在 Overleaf 中打开此 ***产生错误的示例*** 在 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

宏包通过 [综合 TeX 归档网络](https://www.ctan.org/)，通常简称为 CTAN；截至撰写时，它托管着来自 2881 位贡献者的 6287 个宏包。CTAN [自我描述为](https://www.ctan.org/ctan) 的通常用法写成

> ... 一个遍布全球的互联网站点集合，提供与 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://overleaf-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.
