> 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/shu-xue/06-aligning-equations-with-amsmath.md).

# 对齐方程

## 引言

该 `amsmath` 该宏包提供了一些用于显示公式的选项。即使公式非常长，或者你必须在同一行中包含多个公式，你也可以选择更适合文档的排版方式。

用于公式的标准 LaTeX 工具可能缺乏一些灵活性，当公式太长时，可能会导致重叠，甚至裁掉公式的一部分。我们可以通过使用 `amsmath` 宏包来克服这些困难，该宏包可以使用 `\usepackage{amsmath}`.

让我们从一个基本示例开始：

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

[在 Overleaf 中打开此 `amsmath` 在 Overleaf 中的片段添加到文档前导区](https://www.overleaf.com/docs?engine=\&snip_name=amsmath+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamsmath%7D%0A%5Ctitle%7BAmsmath+example%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7BMay+2021%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A%5Csection%7BIntroduction%7D%0A%5Cbegin%7Bequation%7D+%5Clabel%7Beq1%7D%0A%5Cbegin%7Bsplit%7D%0AA+%26+%3D+%5Cfrac%7B%5Cpi+r%5E2%7D%7B2%7D+%5C%5C%0A+%26+%3D+%5Cfrac%7B1%7D%7B2%7D+%5Cpi+r%5E2%0A%5Cend%7Bsplit%7D%0A%5Cend%7Bequation%7D%0A%5Cend%7Bdocument%7D)

下图展示了由 LaTeX 代码生成的输出：

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

如果你想让公式带编号，就必须把公式包裹在 `equation` 环境中，否则请使用 `equation*` （带星号）。在 `equation` 环境中，使用 `split` 环境将公式拆分成更小的部分，这些更小的部分会相应对齐。双反斜杠充当换行符。使用与号 `&`来设置公式在垂直方向上对齐的位置。

## 编写单个公式

如前言所述，要显示单个公式，你必须使用 `equation*` 或 `equation` 环境，具体取决于你是否希望该公式带编号。此外，你还可以添加标签，以便在文档中将来引用。

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

优美的方程 \ref{eu_eqn} 被称为欧拉方程。
```

[在 Overleaf 中打开此 `amsmath` 在 Overleaf 中的片段添加到文档前导区](https://www.overleaf.com/docs?engine=\&snip_name=amsmath+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamsmath%7D%0A%5Ctitle%7BAmsmath+example%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7BMay+2021%7D%0A%5Cbegin%7Bdocument%7D%0A%25%5Cmaketitle%0A%25%5Csection%7BIntroduction%7D%0A%5Cbegin%7Bequation%7D+%5Clabel%7Beu_eqn%7D%0Ae%5E%7B%5Cpi+i%7D+%2B+1+%3D+0%0A%5Cend%7Bequation%7D%0A%0AThe+beautiful+equation+%5Cref%7Beu_eqn%7D+is+known+as+the+Euler+equation.%0A%5Cend%7Bdocument%7D)

下图展示了由 LaTeX 代码生成的输出：

![Neweuler1.png](/files/a636eb6c9c4ef79b6d8a98ab1c0cc89729be8eef)

你也可以 [打开 Overleaf 中该 `amsmath` 宏包的一个更完整示例。](https://www.overleaf.com/project/new/template/19716?id=66719097\&templateName=Amsmath+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## 显示长公式

对于长于一行的公式，请使用 `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` 在 Overleaf 中的片段添加到文档前导区](https://www.overleaf.com/docs?engine=\&snip_name=amsmath+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamsmath%7D%0A%5Ctitle%7BAmsmath+example%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7BMay+2021%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A%5Csection%7BIntroduction%7D%0A%5Cbegin%7Bmultline%2A%7D%0Ap%28x%29+%3D+3x%5E6+%2B+14x%5E5y+%2B+590x%5E4y%5E2+%2B+19x%5E3y%5E3%5C%5C+%0A-+12x%5E2y%5E4+-+12xy%5E5+%2B+2y%5E6+-+a%5E3b%5E3%0A%5Cend%7Bmultline%2A%7D%0A%5Cend%7Bdocument%7D)

下图展示了由 LaTeX 代码生成的输出：

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

你也可以 [打开 Overleaf 中该 `amsmath` 宏包的一个更完整示例。](https://www.overleaf.com/project/new/template/19716?id=66719097\&templateName=Amsmath+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## 拆分并对齐公式

*拆分* 与 *multline*非常相似。使用 *split* 环境来拆分公式并按列对齐，就像公式各部分位于表格中一样。这个环境必须在一个 *equation* 环境内部使用。有关示例，请查看本文档的前言。

## 对齐多个公式

如果有多个公式需要垂直对齐， *align* 环境就能做到：

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

[在 Overleaf 中打开此 `amsmath` 在 Overleaf 中的片段添加到文档前导区](https://www.overleaf.com/docs?engine=\&snip_name=amsmath+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamsmath%7D%0A%5Ctitle%7BAmsmath+example%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7BMay+2021%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A%5Csection%7BIntroduction%7D%0A%5Cbegin%7Balign%2A%7D+%0A2x+-+5y+%26%3D++8+%5C%5C+%0A3x+%2B+9y+%26%3D++-12%0A%5Cend%7Balign%2A%7D%0A%5Cend%7Bdocument%7D)

下图展示了由 LaTeX 代码生成的输出：

![Amsmatheqn4OLV2.png](/files/f4e206e87ed5cd91c06a316c1bcdea64db291065)

通常，二元运算符（`>`, `<` 和 `=`）会被对齐，以便文档更美观。

如前所述，与号 `&` 决定公式在哪些位置对齐。让我们来看一个更复杂的例子：

```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*}
```

[在 Overleaf 中打开此 `amsmath` 在 Overleaf 中的片段添加到文档前导区](https://www.overleaf.com/docs?engine=\&snip_name=amsmath+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamsmath%7D%0A%5Ctitle%7BAmsmath+example%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7BMay+2021%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A%5Csection%7BIntroduction%7D%0A%5Cbegin%7Balign%2A%7D%0Ax%26%3Dy+++++++++++%26++w+%26%3Dz++++++++++++++%26++a%26%3Db%2Bc%5C%5C%0A2x%26%3D-y+++++++++%26++3w%26%3D%5Cfrac%7B1%7D%7B2%7Dz+++%26++a%26%3Db%5C%5C%0A-4+%2B+5x%26%3D2%2By+++%26++w%2B2%26%3D-1%2Bw++++++++++%26++ab%26%3Dcb%0A%5Cend%7Balign%2A%7D%0A%5Cend%7Bdocument%7D)

下图展示了由 LaTeX 代码生成的输出：

![Amsmath-eqn6OLV2.png](/files/962d0c690fd723fd87b9f6fff47b44196c80acf9)

这里我们把公式排列成三列。LaTeX 假定每个公式由两部分组成，这两部分由一个 `&` 分隔，并且每个公式都与前一个公式之间由一个 `&`.

同样，使用 \* 可以切换公式编号。允许编号时，你可以分别为每一行添加标签。

## 分组并居中显示公式

如果你只需要显示一组连续的公式，居中且完全不做对齐，请使用 `gather` 环境。这里同样适用用于打开或关闭公式编号的星号技巧。

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

[在 Overleaf 中打开此 `amsmath` 在 Overleaf 中的片段添加到文档前导区](https://www.overleaf.com/docs?engine=\&snip_name=amsmath+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamsmath%7D%0A%5Ctitle%7BAmsmath+example%7D%0A%5Cauthor%7BOverleaf%7D%0A%5Cdate%7BMay+2021%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cmaketitle%0A%5Csection%7BIntroduction%7D%0A%5Cbegin%7Bgather%2A%7D+%0A2x+-+5y+%3D++8+%5C%5C+%0A3x%5E2+%2B+9y+%3D++3a+%2B+c%0A%5Cend%7Bgather%2A%7D%0A%5Cend%7Bdocument%7D)

下图展示了由 LaTeX 代码生成的输出：

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

## 进一步阅读

更多信息请参见

* [数学表达式](/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://overleaf-pro.ayaka.space/latex/zh-cn/shu-xue/06-aligning-equations-with-amsmath.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.
