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

# 陈列数学应以 $$ 结束

## 引言

TeX 引擎有两种处理数学排版的方式：

* *行内数学模式* 其中数学内容包含在段落中，并且
* *显示数学模式* 其中数学内容单独显示，并在其上方或下方留有额外空白。

*传统上*，在 TeX 的早期，打算排版的数学内容 *行内*，通常位于段落中，会被单个 `$` 字符： `$ 行内数学内容...$` 以及要排版为 *显示* 的数学内容会被双重 `$` 字符： `$$ 显示数学内容...$$`.

## 错误原因：显示数学应以 $$ 结束

错误信息 `显示数学应以 $$ 结束` 是 TeX 引擎在试图完成某些显示数学内容的排版，但由于 TeX 标记不正确而无法顺利退出显示数学模式时生成的：如错误信息所示，要排版为显示数学的内容并未以第二个 `$$` 成对符号。

### 示例：单个错误

以下示例演示了这个错误：

```latex
\documentclass{article}
\usepackage[textwidth=8cm]{geometry}
\begin{document}
\noindent \verb|$$ E=mc^2$| 会产生错误，因为数学内容是
由 \texttt{\$\$} 开始，但以单个 \texttt{\$} 结束：

$$ E=mc^2$

\noindent\verb|$$ E=mc^2$ $| 也会产生错误，因为
终止 \texttt{\$} 字符之间存在空格：

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

[在 Overleaf 中打开此 **会产生错误的** 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)

此示例会生成如下输出（图像已编辑以突出显示这两个错误）：

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

### 示例：两个错误

**注意**：在某些情况下，你可能 *也* 看到相关错误 [插入了缺少的 $](/latex/zh-cn/latex-cuo-wu/25-missing-inserted.md)，如下例通过写入 `$$E=mc^2`，其省略了两个终止 `$` 字符：

```latex
\documentclass{article}
\usepackage[textwidth=8cm]{geometry}
\begin{document}
\noindent 以下示例省略了两个终止的 \texttt{\$} 字符，从而触发 \texttt{Missing \$ inserted} 和 \texttt{Display math should end with \$\$} 错误。

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

[在 Overleaf 中打开此 **会产生错误的** 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)

此示例生成以下输出：

![OLdisplaymathtwoerrors.png](/files/5b648094e19020903a32c2fda1fc2875fc368340)

## 解决方案

对于上面演示的错误，修复很直接——确保在显示数学的末尾添加闭合的 `$$` 在显示数学的末尾：

```latex
\documentclass{article}
\begin{document}
\noindent 解决办法是通过写入 \verb|$$E=mc^2$$| 来确保显示数学正确结束：
显示数学

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

[在 Overleaf 中打开此 **已更正** 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)

## 避免使用 $ 字符来排版数学

如今，标准（公认）的最佳实践是 *避免* 使用显式 `$` 字符来排版数学，并改用 LaTeX *定界符* ，尤其是用于显示数学：

* 用于 **显示数学**：写 `\[ 显示数学内容 \]` 而不是 `$$ *显示* 数学内容...$$`
* 用于 **行内数学**：写 `\( 行内数学内容 \)` 而不是 `$ *行内* 数学内容...$`

实际上，LaTeX 定界符 `\(`, `\)`, `\[` 和 `\]` 是单字符 *宏* ，它们在单个和双个 `$` 字符周围提供了一种“隔离包装”。这些定界符（宏）的 LaTeX 定义确实包含 `$` 字符，但附加了执行一些测试/检查的代码。它们还会生成 LaTeX 的错误信息 `数学环境定界符错误`。使用这些定界符（宏）还有额外的优点，因为它们可以被重新定义，或许是临时地，以实现特殊效果。


---

# 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/latex-cuo-wu/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.
