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

[打开此 `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} 被称为欧拉公式。
```

[打开此 `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)

你也可以 [打开一个更完整的 `amsmath` 包在 Overleaf 中的示例。](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)

你也可以 [打开一个更完整的 `amsmath` 包在 Overleaf 中的示例。](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*}
```

[打开此 `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*}
```

[打开此 `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*}
```

[打开此 `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://ayakaleaf-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.
