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

# Aligning equations

## Introduction

The `amsmath` package provides a handful of options for displaying equations. You can choose the layout that better suits your document, even if the equations are really long, or if you have to include several equations in the same line.

The standard LaTeX tools for equations may lack some flexibility, causing overlapping or even trimming part of the equation when it's too long. We can surpass such difficulties by using the `amsmath` package, which can be added to preamble of your document using `\usepackage{amsmath}`.

Let's start with a basic example:

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

[Open this `amsmath` fragment in 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)

The following graphic shows the output produced by the LaTeX code:

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

You have to wrap your equation in the `equation` environment if you want it to be numbered, use `equation*` (with an asterisk) otherwise. Inside the `equation` environment, use the `split` environment to split the equations into smaller pieces, these smaller pieces will be aligned accordingly. The double backslash works as a newline character. Use the ampersand character `&`, to set the points where the equations are vertically aligned.

## Writing a single equation

To display a single equation, as mentioned in the introduction, you have to use the `equation*` or `equation` environment, depending on whether you want the equation to be numbered or not. Additionally, you might add a label for future reference within the document.

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

The beautiful equation \ref{eu_eqn} is known as the Euler equation.
```

[Open this `amsmath` fragment in 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)

The following graphic shows the output produced by the LaTeX code:

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

You can also [open a more complete example of the `amsmath` package in Overleaf.](https://www.overleaf.com/project/new/template/19716?id=66719097\&templateName=Amsmath+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## Displaying long equations

For equations longer than a line use the `multline` environment. Insert a double backslash to set a point for the equation to be broken. The first part will be aligned to the left and the second part will be displayed in the next line and aligned to the right.

Again, the use of an asterisk \* in the environment name determines whether the equation is numbered or not.

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

[Open this multiline equation `amsmath` fragment in 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)

The following graphic shows the output produced by the LaTeX code:

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

You can also [open a more complete example of the `amsmath` package in Overleaf.](https://www.overleaf.com/project/new/template/19716?id=66719097\&templateName=Amsmath+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## Splitting and aligning an equation

*Split* is very similar to *multline*. Use the *split* environment to break an equation and to align it in columns, just as if the parts of the equation were in a table. This environment must be used inside an *equation* environment. For an example check the introduction of this document.

## Aligning several equations

If there are several equations that you need to align vertically, the *align* environment will do it:

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

[Open this `amsmath` fragment in 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)

The following graphic shows the output produced by the LaTeX code:

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

Usually the binary operators (`>`, `<` and `=`) are the ones aligned for a nice-looking document.

As mentioned before, the ampersand character `&` determines where the equations align. Let's check a more complex example:

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

[Open this `amsmath` fragment in 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)

The following graphic shows the output produced by the LaTeX code:

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

Here we arrange the equations in three columns. LaTeX assumes that each equation consists of two parts separated by an `&` and that each equation is separated from the one before by an `&`.

Again, use \* to toggle the equation numbering. When numbering is allowed, you can label each row individually.

## Grouping and centering equations

If you just need to display a set of consecutive equations, centered and with no alignment whatsoever, use the `gather` environment. The asterisk trick to set/unset the numbering of equations also works here.

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

[Open this `amsmath` fragment in 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)

The following graphic shows the output produced by the LaTeX code:

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

## Further reading

For more information see

* [Mathematical expressions](/latex/mathematics/01-mathematical-expressions.md)
* [Brackets and Parentheses](/latex/mathematics/03-brackets-and-parentheses.md)
* [Subscripts and superscripts](/latex/mathematics/02-subscripts-and-superscripts.md)
* [Spacing in math mode](/latex/mathematics/08-spacing-in-math-mode.md)
* [Display style in math mode](/latex/mathematics/10-display-style-in-math-mode.md)
* [Mathematical fonts](/latex/mathematics/12-mathematical-fonts.md)
* [List of Greek letters and math symbols](/latex/mathematics/11-list-of-greek-letters-and-math-symbols.md)
* [Operators](/latex/mathematics/07-operators.md)
* [Fractions and Binomials](/latex/mathematics/05-fractions-and-binomials.md)
* [amsmath package documentation](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/mathematics/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.
