> 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/latex-errors/03-display-math-should-end-with.md).

# Display math should end with $$

## Introduction

TeX engines have two ways of typesetting mathematics:

* *inline math mode* where the mathematical content is contained within a paragraph, and
* *display math mode* where mathematical material is displayed separately, with additional space above or below it.

*Traditionally*, in the early days of TeX, mathematics intended to be typeset *inline*, typically within a paragraph, was surrounded by single `$` characters: `$ inline math content...$` and mathematics destined for *display* was surrounded by double `$` characters: `$$ display math content...$$`.

## Cause of the error Display math should end with $$

The error message `Display math should end with $$` is generated by TeX engines when they try to finish typesetting some display math material but are unable to cleanly exit from display math mode due to incorrect TeX markup: as the error message indicates, the material to be typeset as display math has not been terminated with a second `$$` pair.

### Examples: single error

This error is demonstrated in the following examples:

```latex
\documentclass{article}
\usepackage[textwidth=8cm]{geometry}
\begin{document}
\noindent \verb|$$ E=mc^2$| generates an error because the math is
started by \texttt{\$\$} but terminated by a single \texttt{\$}:

$$ E=mc^2$

\noindent\verb|$$ E=mc^2$ $| also generates an error because of the space between
the terminating \texttt{\$} characters:

$$ E=mc^2$ $
\end{document}
```

[Open this **error-generating** example in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Errors+in+display+math\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5Btextwidth%3D8cm%5D%7Bgeometry%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnoindent+%5Cverb%7C%24%24+E%3Dmc%5E2%24%7C+generates+an+error+because+the+math+is+%0Astarted+by+%5Ctexttt%7B%5C%24%5C%24%7D+but+terminated+by+a+single+%5Ctexttt%7B%5C%24%7D%3A%0A%0A%24%24+E%3Dmc%5E2%24%0A%0A%5Cnoindent%5Cverb%7C%24%24+E%3Dmc%5E2%24+%24%7C+also+generates+an+error+because+of+the+space+between%0Athe+terminating+%5Ctexttt%7B%5C%24%7D+characters%3A%0A%0A%24%24+E%3Dmc%5E2%24+%24%0A%5Cend%7Bdocument%7D)

This example produces the following output (image edited to highlight both errors):

![OLdisplaymatherrors.png](/files/clT18EB6aJJ8o8aG6WJF)

### Example: two errors

**Note**: In some circumstances you may *also* see the related error [Missing $ inserted](/latex/latex-errors/25-missing-inserted.md), as the following example demonstrates by writing `$$E=mc^2`, which omits both terminating `$` characters:

```latex
\documentclass{article}
\usepackage[textwidth=8cm]{geometry}
\begin{document}
\noindent The following example omits both terminating \texttt{\$} characters, triggering the errors \texttt{Missing \$ inserted} and \texttt{Display math should end with \$\$.}

$$E=mc^2
\end{document}
```

[Open this **error-generating** example in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Two+errors+in+display+math\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5Btextwidth%3D8cm%5D%7Bgeometry%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnoindent+The+following+example+omits+both+terminating+%5Ctexttt%7B%5C%24%7D+characters%2C+triggering+the+errors+%5Ctexttt%7BMissing+%5C%24+inserted%7D+and+%5Ctexttt%7BDisplay+math+should+end+with+%5C%24%5C%24.%7D%0A%0A%24%24E%3Dmc%5E2%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![OLdisplaymathtwoerrors.png](/files/jxsS3d8s385YL5y2x8vI)

## Solution

For the errors demonstrated above, the fix is straightforward—make sure you add the closing `$$` at the end of your display math:

```latex
\documentclass{article}
\begin{document}
\noindent The solution is to ensure correct termination of the
display math by writing \verb|$$E=mc^2$$|:

$$E=mc^2$$
\end{document}
```

[Open this **corrected** example in Overleaf](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Errors+in+display+math\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnoindent+The+solution+is+to+ensure+correct+termination+of+the+%0Adisplay+math+by+writing+%5Cverb%7C%24%24E%3Dmc%5E2%24%24%7C%3A%0A%0A%24%24E%3Dmc%5E2%24%24%0A%5Cend%7Bdocument%7D)

## Avoid using $ characters to typeset mathematics

Nowadays, standard (accepted) best practice is to *avoid* using explicit `$` characters to typeset mathematics and use LaTeX *delimiters* instead, particularly for display math:

* for **display math**: write `\[ display math content \]` instead of `$$ *display* math content...$$`
* for **inline math**: write `\( inline math content \)` instead of `$ *inline* math content...$`

In reality, the LaTeX delimiters `\(`, `\)`, `\[` and `\]` are single-character *macros* which provide a sort of “insulating wrapper” around single and double `$` characters. The LaTeX definitions of those delimiters (macros) do actually contain `$` characters but with additional code that runs some tests/checks. They also generate LaTeX’s error message `Bad math environment delimiter`. Using these delimiters (macros) has additional advantages because they can be redefined, perhaps temporarily, to achieve special effects.


---

# 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/latex-errors/03-display-math-should-end-with.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.
