> 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/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{\$} 字元之間的空格而產生錯誤：
兩個結尾 \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/5efc91d34d6a4676e518e0eec4686a028fd41c44)

### 範例：兩個錯誤

**注意**：在某些情況下，你也可能 *也* 看到相關錯誤 [插入了缺少的 $](/latex/zh-tw/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/3c9cbb500ccb96d6a81324ec033685d1b534998b)

## 解決方法

對於上面示範的錯誤，修正方法很直接——請確保你在顯示數學的結尾加上 `$$` 於顯示數學的末尾：

```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 *分隔符號* ，尤其是對顯示數學：

* 的 **顯示數學**：寫成 `\[ 顯示數學內容 \]` 而不是 `$$ *顯示* 數學內容...$$`
* 的 **行內數學**：寫成 `\( 行內數學內容 \)` 而不是 `$ *行內* 數學內容...$`

實際上，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-tw/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.
