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

# 显示数学公式应以 $$ 结束

## 引言

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

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

*传统上*，在 TeX 的早期，意图排版的数学 *行内*，通常位于段落中，外面包围着单个 `$` 字符： `$ inline math content...$` 以及面向 *显示* 的数学则外面包围着双个 `$` 字符： `$$ display math content...$$`.

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

错误信息 `显示数学应以 $$ 结尾` 是 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 中的示例](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 中的示例](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 中的示例](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 *分隔符* ，尤其是用于陈列数学：

* 用于 **陈列数学**：写 `\[ display math content \]` 而不是 `$$ *display* math content...$$`
* 用于 **行内数学**：写 `\( inline math content \)` 而不是 `$ *inline* math content...$`

实际上，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://ayakaleaf-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.
