> 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-tw/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/15c47e6396b32b98557a7fbeda4ff50bca80634c)

你必須將方程式包在 `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/c6cf1891f6dc831ccd3a5cb981b5d4da923828a7)

你也可以 [在 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/154fe96446013de8686216be3aa1cfebfdb5a6fc)

你也可以 [在 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/e29a5660659eaf77672edb67aab7c00d86653e23)

通常二元運算子（`>`, `<` 和 `=`）會被對齊，以便文件看起來更美觀。

如前所述，與號 `&` 決定方程式要在哪裡對齊。讓我們看看更複雜的範例：

```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/3f6f9066fd52b6874359f9f32d0f8490b5838fe0)

這裡我們將方程式排成三欄。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/d70ee49d7bc125d24fcab889a1885b1878f0137f)

## 延伸閱讀

更多資訊請參見

* [數學表達式](/latex/zh-tw/shu-xue/01-mathematical-expressions.md)
* [中括號與小括號](/latex/zh-tw/shu-xue/03-brackets-and-parentheses.md)
* [下標與上標](/latex/zh-tw/shu-xue/02-subscripts-and-superscripts.md)
* [數學模式中的間距](/latex/zh-tw/shu-xue/08-spacing-in-math-mode.md)
* [數學模式中的顯示樣式](/latex/zh-tw/shu-xue/10-display-style-in-math-mode.md)
* [數學字型](/latex/zh-tw/shu-xue/12-mathematical-fonts.md)
* [希臘字母與數學符號列表](/latex/zh-tw/shu-xue/11-list-of-greek-letters-and-math-symbols.md)
* [運算子](/latex/zh-tw/shu-xue/07-operators.md)
* [分數與二項式](/latex/zh-tw/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-tw/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.
