> 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/12-latex-error-verb-ended-by-end-of-line.md).

# LaTeX 錯誤：\verb 於行尾結束

當你使用了某個 **`verbatim`** 命令方式不正確時會出現此錯誤。如果你這樣做，將會產生以下錯誤：

main.tex，第 5 行

LaTeX 錯誤：\verb 在行尾結束。

請參閱 LaTeX 手冊或 LaTeX Companion 以取得說明。輸入 H 可立即獲得幫助。... l.7 \unbrace{\verb+\error+} 嘗試輸入以繼續。如果那沒有用，輸入 X 以結束。

要解決此錯誤，最好改用 **`verbatim`** 如下所示的 environment：

```latex
\begin{verbatim}
你應該把原文逐字內容放在這裡。
\end{verbatim}
```

## 常見原因

**在 \verb 命令中包含換行：**

導致此錯誤的一個常見原因是你使用了 **`\verb`** 命令，但你在其中加入了換行，如下所示：

```latex
我們可以在 \LaTeX 中寫出不同字體，例如
\verb{
\textbf{粗體}
\textit{斜體}
\textsf{無襯線體}
}
```

這會產生錯誤，因為不允許在其中使用換行， **`\verb`** 正確的寫法是使用 **`verbatim`** 如下所示的 environment：

```latex
% 在你的前言區

\usepackage{verbatim}

% 在文件正文中

我們可以在 \LaTeX 中寫出不同字體，例如
\begin{verbatim}
\textbf{粗體}
\textit{斜體}
\textsf{無襯線體}
\end{verbatim}
```

![Verbatim.PNG](/files/bee42a921bec3733098e0839c2bb02ca42b914db)

**在 \verb 命令中省略結尾字元：**

\verb 命令應如下所示使用：

```latex
\verb!\frac{1}{2}!
```

請注意，這裡的分隔符號並非唯一，任何字元都可以。之所以我們使用 **`!`** 作為這裡的分隔符號，是因為沒有 **`!`** 以 **`\frac{1}{2}`**。如果我們選擇寫成 **`\verb{\frac{1}{2}}`**，就會產生以下輸出，這是不正確的。

```latex
\verb{\frac{1}{2}}
```

![Delimiters.PNG](/files/55b66a4de2d72ca75d79346ec07bff51650c4e02)

這可能會導致問題，因為如果該分隔符號的開頭與結尾 **`\verb`** 不是同一個字元，就會產生錯誤。因此，以下所有命令都會產生錯誤：

```latex
\verb \frac{1}{2}
```

```latex
{\verb \frac{1}{2}}
```

```latex
\verb!\frac{1}{2}
```

再次建議直接使用 **`verbatim`** 以下方所示的 environment 來撰寫

```latex
\begin{verbatim}
\frac{1}{2}
\end{verbatim}
```

**在另一個命令中使用 \verb 命令：**

此錯誤還可能以另一種方式出現：如果 **`\verb`** 在另一個命令中使用了一個命令，如下所示

```latex
\section{\verb!\frac{1}{2}!}
```

這在 LaTeX 中是不允許的，而且是少數幾種情況之一，在這種情況下 **`verbatim`** environment 也無法幫上忙。要解決此錯誤，你應該載入 **`cprotect`** 套件，如下所示：

```latex
% 在你的前言區

\usepackage{cprotect}

% 在文件正文中

\cprotect\section{這是一個包含逐字 \verb!\frac{1}{2}! 的章節標題}
這是一段文字
```

![Cprotect.PNG](/files/e443438b5aefdbe2be41da53e7ea1ad88b4af830)


---

# 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/12-latex-error-verb-ended-by-end-of-line.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.
