> 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/geng-duo-zhu-ti/03-aligning-equations-with-amsmath-es.md).

# 使用 amsmath 对齐公式 es

该宏包 **amsmath** 提供了相当丰富的方程排版选项。你可以选择最适合文档的方程布局，即使方程非常大，或者需要在同一行中包含多个方程。

## 引言

LaTeX 处理方程的标准工具缺乏灵活性，有时会导致元素重叠，甚至在方程过大时截断其部分内容。所有这些问题都可以通过该宏包来解决 **amsmath**。先来看一个示例：

```latex
\begin{equation} \label{eq1}
\begin{split}
A & = \frac{\pi r^2}{2} \\
 & = \frac{1}{2} \pi r^2
\end{split}
\end{equation}
```

![Amsmath-eqn1.png](/files/7f5d9a9a59161a0042b0c1955edef9d23077f7ea)

方程应包含在环境 *equation* ；如果需要编号，可以使用 *equation\** （带星号）来表示不编号。在环境 *equation* 中使用环境 *split* 来将方程拆分为更小的部分，这些部分会自动对齐。使用双反斜杠（double backslash）可作为换行。et 符号（也以其英文名 ampersand 而为人所知） *&* 用于指定方程垂直对齐的基准点。

[在 Overleaf 中打开 amsmath 宏包示例](https://www.sharelatex.com/project/new/template?zipUrl=/project/52e1889fd3e596077700148c/download/zip\&templateName=Amsmath_Example_Project\&compiler=pdflatex)

## 导入 amsmath 宏包

这一步很简单，如果你经常使用 LaTeX，可能已经知道了。要导入宏包“*amsmath*”，请在文档导言区添加以下代码行。

`\usepackage{amsmath}`

[在 Overleaf 中打开 amsmath 宏包示例](https://www.sharelatex.com/project/new/template?zipUrl=/project/52e1889fd3e596077700148c/download/zip\&templateName=Amsmath_Example_Project\&compiler=pdflatex)

## 编写单个方程

要显示一个单独的方程，正如引言中所提到的，可以使用环境 *equation\** 用于无编号方程，或者使用环境 *equation* 用于有编号方程。此外，还可以通过命令添加标签 `\label`，该标签之后可在文档中引用。

```latex
\begin{equation} \label{eu_eqn}
e^{\pi i} - 1 = 0
\end{equation}

美丽的方程 \ref{eu_eqn} 被称为欧拉公式
```

![Amsmath-eqn2.png](/files/26ff3c563b181a8835aefd9b3939bba69bf8aee9)

[在 Overleaf 中打开 amsmath 宏包示例](https://www.sharelatex.com/project/new/template?zipUrl=/project/52e1889fd3e596077700148c/download/zip\&templateName=Amsmath_Example_Project\&compiler=pdflatex)

## 插入多行方程

要创建一个跨越多行的方程，应使用环境 *multline*。需要插入双反斜杠来确定方程的分隔点。第一部分将左对齐，而另一部分会显示在下一行并右对齐。

与其他命令一样，可以在环境名称后加上星号来决定方程是否编号。

```latex
\begin{multline*}
p(x) = 3x^6 + 14x^5y + 590x^4y^2 + 19x^3y^3\\
- 12x^2y^4 - 12xy^5 + 2y^6 - a^3b^3
\end{multline*}
```

![Amsmath-eqn3.png](/files/b4866ada4ac89925aa29fb0f29cccd23074f3766)

[在 Overleaf 中打开 amsmath 宏包示例](https://www.sharelatex.com/project/new/template?zipUrl=/project/52e1889fd3e596077700148c/download/zip\&templateName=Amsmath_Example_Project\&compiler=pdflatex)

## 拆分并对齐方程

*split* 是一个与 *multline*非常相似的环境。 *split* 用于将一个方程拆分为多个部分，然后按列对齐，就像这些方程部分放在表格中一样。这个环境必须在环境 *equation*中使用。有关示例，请参见本文开头的引言部分。

## 对齐多个方程

如果有多个方程需要垂直对齐，则环境 *align* 负责完成这一工作：

```latex
\begin{align*}
2x - 5y &=  8 \\
3x + 9y &=  -12
\end{align*}
```

![Amsmath-eqn4.png](/files/4b147bce96d43560058947eb105067385ed07787)

通常会将二元运算符（>、< 和 =）作为垂直对齐点，这样可以得到更美观的文档。

如前所述，et 符号 *&* 决定方程的对齐点。来看一个稍微复杂一点的例子：

```latex
\begin{align*}
x&=y           &  w &=z              &  a&=b+c\\
2x&=-y         &  3w&=\frac{1}{2}z   &  a&=b\\
-4 + 5x&=2+y   &  w+2&=-1+w          &  ab&=cb
\end{align*}
```

![Amsmath-eqn6.png](/files/b0c8ebe5ed4b8c7e9b708d4fda11eceb833d9508)

这里的方程被排成三列。LaTeX 假定每个方程由两部分组成，由字符 *&*；分隔；并且每个方程又由一个 *&*.

再次说明，可以使用星号 \* 来启用或禁用方程编号。当编号启用时，可以为每一行单独添加标签。

[在 Overleaf 中打开 amsmath 宏包示例](https://www.sharelatex.com/project/new/template?zipUrl=/project/52e1889fd3e596077700148c/download/zip\&templateName=Amsmath_Example_Project\&compiler=pdflatex)

## 分组并居中方程

如果你只需要显示一组连续的、居中且不做任何对齐的方程，请使用环境 *gather*。这里同样适用于使用星号来启用/禁用方程编号的技巧。

```latex
\begin{gather*}
2x - 5y =  8 \\
3x^2 + 9y =  3a + c
\end{gather*}
```

![Amsmath-eqn5.png](/files/75d81661f13056050166f3c26eff1e481ae63146)

[在 Overleaf 中打开 amsmath 宏包示例](https://www.sharelatex.com/project/new/template?zipUrl=/project/52e1889fd3e596077700148c/download/zip\&templateName=Amsmath_Example_Project\&compiler=pdflatex)

## 延伸阅读

更多信息请参阅：

* [数学表达式](/latex/zh-cn/shu-xue/01-mathematical-expressions.md)
* [方括号和圆括号](/latex/zh-cn/shu-xue/03-brackets-and-parentheses.md)
* [下标和上标](/latex/zh-cn/shu-xue/02-subscripts-and-superscripts.md)
* [数学模式中的间距](/latex/zh-cn/shu-xue/08-spacing-in-math-mode.md)
* [数学模式中的显示样式](/latex/zh-cn/shu-xue/10-display-style-in-math-mode.md)
* [数学字体](/latex/zh-cn/shu-xue/12-mathematical-fonts.md)
* [希腊字母和数学符号列表](/latex/zh-cn/shu-xue/11-list-of-greek-letters-and-math-symbols.md)
* [运算符](/latex/zh-cn/shu-xue/07-operators.md)
* [分数和二项式](/latex/zh-cn/shu-xue/05-fractions-and-binomials.md)
* [amsmath 宏包文档](http://mirrors.ctan.org/macros/latex/required/amsmath/amsmath.pdf)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ayakaleaf-pro.ayaka.space/latex/zh-cn/geng-duo-zhu-ti/03-aligning-equations-with-amsmath-es.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.
