> ## Documentation Index
> Fetch the complete documentation index at: https://ayakaleaf-pro.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 30 分钟学会 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 中新建一个项目。

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

```latex theme={null}
\documentclass{article}
\begin{document}
First document. This is a simple example, with no 
extra parameters or packages included.
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=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)

该示例生成的输出如下：

![示例文档](https://images.ctfassets.net/nrgyaltdicpt/5oBbZABsWEnHZpwhfvV2PD/95ba7d5496616b4f10dffe6fb90f578a/LL30Fig1-plain.svg)

可以看到，LaTeX 自动缩进了段落的第一行，替你处理好了这一格式。下面我们来仔细看看代码的每个部分分别起什么作用。

第一行代码 `\documentclass{article}` 声明了文档的类型，称为文档\_类\_（class），它控制着文档的整体外观。不同类型的文档需要不同的文档类；例如，简历所需的文档类与科学论文不同，后者可能使用标准的 LaTeX `article` 类。你正在处理的其他类型文档可能需要不同的文档类，例如 `book` 或 `report`。要了解可用的众多 LaTeX 文档类，请[访问 CTAN（Comprehensive TeX Archive Network）上的相关页面](https://www.ctan.org/topic/class)。

设置好文档类之后，我们的内容（称为文档的\_正文\_）写在 `\begin{document}` 和 `\end{document}` 标签之间。打开上面的示例后，你可以修改文本，完成后通过\_重新编译文档\_来查看生成的排版 PDF。在 Overleaf 中，只需点击 **Recompile** 即可，如以下简短视频片段所示：

任何 Overleaf 项目都可以配置为每次编辑后自动重新编译：点击 **Recompile** 按钮旁边的小箭头，将 **Auto Compile** 设置为 **On**，如下方截图所示：

![展示如何将 Auto Compile 设置为 On 或 Off 的截图](https://mintcdn.com/ayakaleaf-pro/GmaXa-Cu4QQRFT4C/images/texlive/ll30autocompile.png?fit=max\&auto=format\&n=GmaXa-Cu4QQRFT4C\&q=85\&s=a109144b1a0939f182ce202207687428)

了解了如何向文档添加内容之后，下一步是为文档添加标题。为此，我们需要简单介绍一下**导言区**（preamble）。

## 文档的导言区

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

前面的示例展示了如何在 `\begin{document}` 命令之后输入文档内容；而 `.tex` 文件中出现在该位置\_之前\_的所有内容称为\_导言区\_，它相当于文档的"设置"部分。在导言区中，你可以定义文档类（类型）以及诸如撰写文档所用语言等具体设置；加载你想使用的\_宏包\_（[稍后](/zh-CN/texlive/latex-basics/learn-latex-in-30-minutes#Finding%5Fand%5Fusing%5FLaTeX%5Fpackages)将详细介绍），也可以在这里进行其他类型的配置。

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

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

其中 `\documentclass[12pt, letterpaper]{article}` 定义了文档的整体类（类型）。附加参数写在方括号（`[...]`）中，必须用逗号分隔，用于配置该 article 类的这一实例，即我们希望为这个基于 `article` 类的特定文档使用的设置。

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

* `12pt` 设置字号
* `letterpaper` 设置纸张大小

当然，也可以使用 `9pt`、`11pt`、`12pt` 等其他字号；如果未指定，默认字号为 `10pt`。至于纸张大小，其他可选值有 `a4paper` 和 `legalpaper`。更多信息请参阅关于页面尺寸和页边距的文章。

导言区中的这一行

```latex theme={null}
\usepackage{graphicx}
```

是加载外部宏包（这里是 [graphicx](https://ctan.org/pkg/graphicx?lang=en)）以扩展 LaTeX 功能的一个示例，它使 LaTeX 能够导入外部图形文件。LaTeX 宏包将在[查找和使用 LaTeX 宏包](/zh-CN/texlive/latex-basics/learn-latex-in-30-minutes#Finding%5Fand%5Fusing%5FLaTeX%5Fpackages)一节中讨论。

## 添加标题、作者和日期信息

要为文档添加标题、作者和日期，需要在\_导言区\_（而\_不是\_文档正文）中再添加三行。这三行是：

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

添加这些行之后，你的导言区应该类似于：

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

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

```latex theme={null}
\begin{document}
\maketitle
We have now added a title, author and date to our first \LaTeX{} document!
\end{document}
```

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

```latex theme={null}
\documentclass[12pt, letterpaper]{article}
\title{My first LaTeX document}
\author{Hubert Farnsworth\thanks{Funded by the Overleaf team.}}
\date{August 2022}
\begin{document}
\maketitle
We have now added a title, author and date to our first \LaTeX{} document!
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=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 生成的简单文档图片](https://images.ctfassets.net/nrgyaltdicpt/3WzMd0bx2zlj3zLTKS8SxT/dc2b7c16adbc96bdd750b6d2c8b4227e/LL30Fig2r-plain.svg)

## 添加注释

LaTeX 是一种"程序代码"，只不过专门用于文档排版；因此，与用任何其他编程语言编写的代码一样，在文档中添加注释会非常有用。LaTeX 注释是一段不会被排版、也不会以任何方式影响文档的文本——通常用于添加"待办"笔记、加入说明性注解、对复杂宏进行行内解释，或在调试时注释掉某些 LaTeX 代码行/段落。

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

```latex theme={null}
\documentclass[12pt, letterpaper]{article}
\title{My first LaTeX document}
\author{Hubert Farnsworth\thanks{Funded by the Overleaf team.}}
\date{August 2022}
\begin{document}
\maketitle
We have now added a title, author and date to our first \LaTeX{} document!

% This line here is a comment. It will not be typeset in the document.
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=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 theme={null}
Some of the \textbf{greatest}
discoveries in \underline{science} 
were made by \textbf{\textit{accident}}.
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=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 文档中格式化文本的图片](https://images.ctfassets.net/nrgyaltdicpt/6vo15r0tLCByTpcvn1w2E8/8f14edc7ece1f10ce4ba2c76e1e58a56/LL30Fig3r-plain.svg)

另一个非常有用的命令是 `\emph{_argument_}`，它对 `_argument_` 的效果取决于上下文。在普通文本中，强调的文本会以斜体显示；但如果在斜体文本中使用，则效果相反——请看下一个示例：

```latex theme={null}
Some of the greatest \emph{discoveries} in science 
were made by accident.

\textit{Some of the greatest \emph{discoveries} 
in science were made by accident.}

\textbf{Some of the greatest \emph{discoveries} 
in science were made by accident.}
```

[在 Overleaf 中打开此 **\emph** 示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=Using+the+emph+command\&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%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 命令用法的图片](https://images.ctfassets.net/nrgyaltdicpt/1K0A8RXXOomakJBAr2usmp/62d0e8b5275092dd0b1b4a59c745b632/LL30Fig4r-plain.svg)

* **注意：** 某些宏包（例如 Beamer）会改变 `\emph` 命令的行为。

## 添加图片

本节将介绍如何向 LaTeX 文档添加图片。Overleaf 支持三种插入图片的方式：

1. 使用编辑器工具栏上的 [**Insert Figure** 按钮](https://learn.overleaf.com/learn/Kb/How_to_insert_figures_in_Overleaf#Using%5FInsert%5FFigure%5Fto%5Fadd%5Fa%5Ffigure%5Fto%5Fyour%5Fproject)（![编辑器工具栏上的 Insert Figure 按钮](https://sharelatex-wiki-cdn-671420.cdn77.org/learn-scripts/images/a/a8/InsertFigureButton.png)），将图片插入 **Visual Editor** 或 **Code Editor**。
2. 将图片复制并粘贴到 **Visual Editor** 或 **Code Editor** 中。
3. 使用 **Code Editor** 编写插入图形的 LaTeX 代码。

方式 1 和 2 会自动生成插入图片所需的 LaTeX 代码，这里我们介绍方式 3——请注意，你需要将这些图片[上传](https://docs.overleaf.com/writing-and-editing/inserting-images)到你的 Overleaf 项目中。以下示例演示了如何插入图片：

```latex theme={null}
\documentclass{article}
\usepackage{graphicx} %LaTeX package to import graphics
\graphicspath{{images/}} %configuring the graphicx package
 
\begin{document}
The universe is immense and it seems to be homogeneous, 
on a large scale, everywhere we look.

% The \includegraphics command is 
% provided (implemented) by the 
% graphicx package
\includegraphics{universe}  
 
There's a picture of a galaxy above.
\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 文档中的图片的图形](https://images.ctfassets.net/nrgyaltdicpt/4p5toat08FUgehv4yuNwXS/efc9e75e4342c9caeec94f26627fa337/LL30Fig5v2-plain.svg)

向 LaTeX 文档导入图形需要一个[附加\_宏包\_](/zh-CN/texlive/latex-basics/learn-latex-in-30-minutes#Finding%5Fand%5Fusing%5FLaTeX%5Fpackages)，它提供插入外部图形文件所需的命令和功能。上例加载了 [graphicx 宏包](https://ctan.org/pkg/graphicx?lang=en)，该宏包提供了众多命令，其中包括用于导入图形的 `\includegraphics{...}` 和用于告知 LaTeX 图形所在位置的 `\graphicspath{...}`。

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

```latex theme={null}
\usepackage{graphicx}
```

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

![展示 LaTeX 访问文件夹中所存图片的图片](https://sharelatex-wiki-cdn-671420.cdn77.org/learn-scripts/images/1/17/ImagesInAFolder.png)

`\includegraphics{universe}` 命令负责实际将图片插入文档。这里，`universe` 是图片文件的名称，但不带扩展名。

**注意**：

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

有关 LaTeX 宏包的更多信息，请参阅本教程末尾的[查找和使用 LaTeX 宏包](/zh-CN/texlive/latex-basics/learn-latex-in-30-minutes#Finding%5Fand%5Fusing%5FLaTeX%5Fpackages)一节。

## 图注、标签和引用

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

```latex theme={null}
\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}
 
As you can see in figure \ref{fig:mesh1}, the function grows near the origin. This example is on page \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 中使用图注的图形](https://images.ctfassets.net/nrgyaltdicpt/4DkAWWkQqZP6QHU2EErogQ/9aa2371f2cde0f3f8bf22aa1c0b91789/LL30Fig6r-plain.svg)

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

* **`\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 帮助文章：

* 图片的定位
* [插入图片](https://docs.overleaf.com/writing-and-editing/inserting-images)

## 在 LaTeX 中创建列表

你可以使用\_环境\_创建不同类型的列表，环境用于封装实现特定排版功能所需的 LaTeX 代码。环境以 `\begin{_environment-name_}` 开始，以 `\end{_environment-name_}` 结束，其中 `_environment-name_` 可以是 `figure`、`tabular`，或者是某种列表类型：用于无序列表的 `itemize` 或用于有序列表的 `enumerate`。

### 无序列表

无序列表由 `itemize` 环境生成。每个列表项之前都必须加上 `\item` 命令，如下所示：

```latex theme={null}
\documentclass{article}
\begin{document}
\begin{itemize}
  \item The individual entries are indicated with a black dot, a so-called bullet.
  \item The text in the entries may be of any length.
\end{itemize}
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=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 中项目符号列表的示例](https://images.ctfassets.net/nrgyaltdicpt/1lDj29Mg9ZuLNHXwZrTJew/cd7d950c65fd05b251c4aa55746f4eb7/LL30Fig7-plain.svg)

你还可以打开这个[更大的 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 theme={null}
\documentclass{article}
\begin{document}
\begin{enumerate}
  \item This is the first entry in our list.
  \item The list numbers increase with each entry we add.
\end{enumerate}
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=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 生成的编号列表示例](https://images.ctfassets.net/nrgyaltdicpt/2vkfa7RiAIJxv5wIZbg9UK/530c384f159d091a587de279d13fefec/LL30Fig8-plain.svg)

与无序列表一样，每个列表项之前都必须加上 `\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 中添加数学公式

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

* \_行内\_数学模式，用于书写作为段落一部分的公式
* *行间*（display）数学模式，用于书写不属于文本或段落、而是单独成行排版的表达式

### 行内数学模式

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

```latex theme={null}
\documentclass[12pt, letterpaper]{article}
\begin{document}
In physics, the mass-energy equivalence is stated 
by the equation $E=mc^2$, discovered in 1905 by Albert Einstein.
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=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)

该示例生成的输出如下：

![展示以行内数学模式排版的公式的图片](https://images.ctfassets.net/nrgyaltdicpt/77oK8pfew1B1VvxiBtALag/15714f2da4e469bbb8cf70a0d3733e5d/LL30Fig9-plain.svg)

要排版行内模式的数学公式，可以使用以下任一对定界符：`\( ... \)`、`$ ... $` 或 `\begin{math} ... \end{math}`，如以下示例所示：

```latex theme={null}
\documentclass[12pt, letterpaper]{article}
\begin{document}
\begin{math}
E=mc^2
\end{math} is typeset in a paragraph using inline math mode---as is $E=mc^2$, and so too is \(E=mc^2\).
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=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 行内数学模式生成的内容的图片](https://images.ctfassets.net/nrgyaltdicpt/46VPfEGxYJ3di18TLzaLo9/ed67df07532c52b6543fd2bae7cb7c04/LL30Fig10r-plain.svg)

### 行间数学模式

以行间模式排版的公式可以带编号，也可以不带编号，如以下示例所示：

```latex theme={null}
\documentclass[12pt, letterpaper]{article}
\begin{document}
The mass-energy equivalence is described by the famous equation
\[ E=mc^2 \] discovered in 1905 by Albert Einstein. 

In natural units ($c = 1$), the formula expresses the identity
\begin{equation}
E=m
\end{equation}
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=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 中行间模式数学公式的图片](https://images.ctfassets.net/nrgyaltdicpt/6n1FPYFzmzCHPpWoEoYQZM/79b33f1096be06326bff7d2e1976c67c/LL30Fig11-plain.svg)

要排版行间模式的数学公式，可以使用以下任一对定界符：`\[ ... \]`、`\begin{displaymath} ... \end{displaymath}` 或 `\begin{equation} ... \end{equation}`。过去，排版行间模式数学公式需要使用 `$$` 字符作为定界符，例如 `$$ _... display math here ..._$$`，但这种方法[已不再推荐](https://texfaq.org/FAQ-dolldoll)：请改用 LaTeX 的定界符 `\[ ... \]`。

### 更完整的示例

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

```latex theme={null}
\documentclass{article}
\begin{document}
Subscripts in math mode are written as $a_b$ and superscripts are written as $a^b$. These can be combined and nested to write expressions such as

\[ 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}) \]
 
We write integrals using $\int$ and fractions using $\frac{a}{b}$. Limits are placed on integrals using superscripts and subscripts:

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

Lower case Greek letters are written as $\omega$ $\delta$ etc. while upper case Greek letters are written as $\Omega$ $\Delta$.

Mathematical operators are prefixed with a backslash as $\sin(\beta)$, $\cos(\alpha)$, $\log(x)$ etc.
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=More+advanced+LaTeX+math+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0ASubscripts+in+math+mode+are+written+as+%24a%5Fb%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%5F1+i%5F2+%5Cdots+i%5Fp%7D%5F%7Bj%5F1+j%5F2+%5Cdots+j%5Fq%7D+%3D+T%28x%5E%7Bi%5F1%7D%2C%5Cdots%2Cx%5E%7Bi%5Fp%7D%2Ce%5F%7Bj%5F1%7D%2C%5Cdots%2Ce%5F%7Bj%5Fq%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%5F0%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 排版的各种数学公式的图片](https://images.ctfassets.net/nrgyaltdicpt/5xfm4NybWckHL5ndpdnBRq/bc0b5e27044820ea5cdd11648a68f543/LL30Fig12r-plain.svg)

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

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

有关使用 `amsmath` 的更多信息，请参阅我们的帮助文章。

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

The well-known Pythagorean theorem \(x^2 + y^2 = z^2\) was proved to be invalid for other exponents, meaning the next equation has no integer solutions for \(n>2\):

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

\section{Second example}

This is a simple math expression \(\sqrt{x^2+1}\) inside text. 
And this is also the same: 
\begin{math}
\sqrt{x^2+1}
\end{math}
but by using another command.

This is a simple math expression without numbering
\[\sqrt{x^2+1}\] 
separated from text.

This is also the same:
\begin{displaymath}
\sqrt{x^2+1}
\end{displaymath}

\ldots and this:
\begin{equation*}
\sqrt{x^2+1}
\end{equation*}
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=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 排版的各种数学公式的图片](https://images.ctfassets.net/nrgyaltdicpt/hT85gkewCDlxCHeNU5slE/9256c129b25bec7a93dcfe9e44b0654a/LL30Fig13r-plain.svg)

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

* 数学表达式
* 下标和上标
* 方括号和圆括号
* 分数和二项式系数
* 公式对齐
* 运算符
* 数学模式中的间距
* 积分、求和与极限
* 数学模式中的行间样式
* 希腊字母和数学符号列表
* 数学字体

## 基本文档结构

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

### 摘要

科学论文通常会提供一段\_摘要\_，即对其核心主题或论点的简要概述/总结。下一个示例演示了如何使用 LaTeX 的 `abstract` 环境排版摘要：

```latex theme={null}
\documentclass{article}
\begin{document}
\begin{abstract}
This is a simple paragraph at the beginning of the 
document. A brief introduction about the main subject.
\end{abstract}
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=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 排版的摘要的图片](https://images.ctfassets.net/nrgyaltdicpt/4cPu8x8ZxuDVF6efcZ03yi/91aa6aa84719613b0e25c725b70aa0b8/LL30Fig14-plain.svg)

### 段落和换行

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

* 如何通过按两次"回车"键（结束当前行并插入一个空行）来创建新段落；
* 如何在不开始新段落的情况下另起一行：使用 `\\` 命令（即双反斜杠）插入手动换行；也可以使用 `\newline` 命令。

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

```latex theme={null}
\documentclass{article}
\begin{document}

\begin{abstract}
This is a simple paragraph at the beginning of the 
document. A brief introduction about the main subject.
\end{abstract}

After our abstract we can begin the first paragraph, then press ``enter'' twice to start the second one.

This line will start a second paragraph.

I will start the third paragraph and then add \\ a manual line break which causes this text to start on a new line but remains part of the same paragraph. Alternatively, I can use the \verb|\newline|\newline command to start a new line, which is also part of the same paragraph.
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=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 中创建段落的图片](https://images.ctfassets.net/nrgyaltdicpt/5DdSH06hFli8k4lR0dxw4m/a55cddf9aa10b64302ae7b72ba0c6447/LL30Fig15r-plain.svg)

请注意 LaTeX 会自动缩进段落——但紧跟在节、小节等文档标题之后的段落除外——[我们稍后会看到](/zh-CN/texlive/latex-basics/learn-latex-in-30-minutes#Chapters%5Fand%5Fsections)。

建议新用户不要使用多个 `\\` 或 `\newline` 来"模拟"段间距更大的段落，因为这可能会干扰 LaTeX 的排版算法。推荐的做法是继续使用空行来创建新段落，不使用任何 `\\`，并通过在导言区添加 `\usepackage{parskip}` 来加载 [parskip 宏包](https://ctan.org/pkg/parskip?lang=en)。

有关段落的更多信息，请参阅以下文章：

* 段落和换行
* 如何在 LaTeX 中更改段落间距
* LaTeX Error: There's no line here to end 提供了有关使用 `\\` 的更多建议和指导。

### 章和节

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

下一个示例演示了用于组织基于 `book` 类的文档结构的命令：

```latex theme={null}
\documentclass{book}
\begin{document}

\chapter{First Chapter}

\section{Introduction}

This is the first 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{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%5Fname=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 文档中各节的图片](https://images.ctfassets.net/nrgyaltdicpt/1A1T7YsurQdM058OwmLqd8/7f6e962c31356bafa702bab9062f6061/LL30Fig16-plain.svg)

章节命令的名称大多不言自明；例如，`\chapter{First Chapter}` 创建一个标题为 `First Chapter` 的新章，`\section{Introduction}` 生成一个标题为 `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 关于节和章的文章。

## 创建表格

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

1. 使用 **Visual Editor**（或 **Code Editor**）工具栏中的 [**Insert Table** 按钮](https://www.overleaf.com/learn/how-to/How_to_insert_tables_in_Overleaf%23inserttable#Using%5FInsert%5FTable%5Fto%5Fcreate%5Fa%5Ftable%5Fin%5Fyour%5Fproject)。
2. 在使用 **Visual Editor** 时，从其他文档中[复制并粘贴表格](https://www.overleaf.com/learn/how-to/How_to_paste_formatted_content_into_Overleaf%23pastingtables)。
3. 在 Code Editor 中编写表格的 LaTeX 代码。

如果你是 LaTeX 新手，使用 **Visual Editor** 中的工具栏（方式 1）是一个很好的入门方法——你可以在 **Visual Editor** 和 **Code Editor** 之间切换，查看表格背后的代码。

这里我们重点介绍方式 3——创建表格最灵活的方法——并通过示例演示如何在 LaTeX 中创建表格，包括添加线条（表线）和表注。随着经验的积累，你可以参阅我们关于[如何使用 LaTeX 创建表格](https://www.overleaf.com/learn/latex/Tables)的详细指南。

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

我们从一个展示如何排版基本表格的示例开始：

```latex theme={null}
\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%5Fname=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 排版的表格的图形](https://images.ctfassets.net/nrgyaltdicpt/5K46QavbKv9vF4RI1GbUf2/f22d4f8a06d00251166f86d2c891321e/LL30Fig17-plain.svg)

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

### 添加边框

`tabular` 环境支持在表格中使用水平线和垂直线（表线）：

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

在本例中，参数为 `{|c|c|c|}`，它声明了三个（居中的）列，各列之间以垂直线（表线）分隔；此外，我们使用 `\hline` 在第一行上方和最后一行下方各放置一条水平线：

```latex theme={null}
\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%5Fname=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)

该示例生成的输出如下：

![Graphic displaying a table typeset in LaTeX containing horizontal and vertical rules](https://images.ctfassets.net/nrgyaltdicpt/5uWYGUhK1XJJJaWz5R1EmX/3ee88bf6b2eaf8d0644b57fa8064800b/LL30Fig18-plain.svg)

下面是另一个示例：

```latex theme={null}
\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%5Fname=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)

该示例生成的输出如下：

![Graphic displaying a table typeset in LaTeX containing horizontal and vertical rules](https://images.ctfassets.net/nrgyaltdicpt/1Cy9FcRnDVDRUPQdCgaKq6/c21e5f7d9a53514756c6e1f2996e51e0/LL30Fig19-plain.svg)

### 表注、标签和引用

你可以用与图片几乎相同的方式为表格添加表注和引用。唯一的区别是使用 **`table`** 环境，而不是 **`figure`** 环境。

表 \ref\{table:data} 展示了如何添加表注以及如何引用表格。

```latex theme={null}
\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%5Fname=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 表格](https://images.ctfassets.net/nrgyaltdicpt/1f5w74UIaTCkpApaPdVeWf/580a87c2413ddc9d6bdef3bf462689fc/LL30Fig20-plain.svg)

## 添加目录

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

```latex theme={null}
\documentclass{article}
\title{Sections and Chapters}
\author{Gubert Farnsworth}
\date{August 2022}
\begin{document}
  
\maketitle
  
\tableofcontents

\section{Introduction}
   
This is the first 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*{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%5Fname=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 生成的目录的图形](https://images.ctfassets.net/nrgyaltdicpt/MxY592Q6EGsDKMA2mZw0t/fffa41a0a0d30770526f73aff72a7be1/LL30Fig21r-plain.svg)

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

## 下载完成的文档

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

更多信息请参阅 Overleaf 帮助文章[从 Overleaf 导出你的作品](https://docs.overleaf.com/managing-projects-and-files/downloading-a-project)。

## 查找和使用 LaTeX 宏包

LaTeX 不仅具备强大的排版能力，还通过附加\_宏包\_提供了一个\_可扩展框架\_。LaTeX 并不试图提供"包揽一切"的命令和功能，而是被设计为\_可扩展\_的，允许用户加载外部代码集（宏包），以提供更专业的排版功能，或扩展 LaTeX 的内置功能（例如表格排版）。正如在[添加图片](/zh-CN/texlive/latex-basics/learn-latex-in-30-minutes#Adding%5Fimages)一节中所见，`graphicx` 宏包通过提供导入图形文件的命令来扩展 LaTeX，并通过在导言区中写入以下语句来加载

```latex theme={null}
\usepackage{graphicx}
```

### 加载宏包

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

```latex theme={null}
\usepackage[options]{somepackage}
```

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

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

`geometry` 宏包是由全球 LaTeX 社区成员编写并贡献、免费提供给任何想使用它的人的宏包之一。

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

```latex theme={null}
\usepackage{somepackage}
```

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

```latex theme={null}
\documentclass[12pt, letterpaper]{article}
\usepackage{somepackage}% a NON-EXISTENT package
\begin{document}
This will fail!
\end{document}
```

[在 Overleaf 上打开此\_**会产生错误的示例**\_](https://www.overleaf.com/docs?engine=pdflatex\&snip%5Fname=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)

该示例生成的输出如下：

![展示缺少宏包所导致错误的图片](https://sharelatex-wiki-cdn-671420.cdn77.org/learn-scripts/images/6/60/LL30packagefail.png)

### 查找宏包信息：CTAN

宏包通过 [Comprehensive TeX Archive Network](https://www.ctan.org/)（通常简称 CTAN）分发，在撰写本文时，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](https://www.overleaf.com/learn/latex/Overleaf_and_TeX_Live)，并会在 TeX Live 新版本发布时进行更新。Overleaf 的 TeX Live 更新并不是即时的，而是在发布后几个月进行，这让我们有时间使用[模板库中的数千个模板](https://www.overleaf.com/gallery)对新版 TeX Live 进行兼容性测试。例如，这是我们的 [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 上的最新版本，但一般来说，这不太可能造成问题。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.