> 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/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`** 环境，如下所示：

```latex
\begin{verbatim}
你应该把逐字文本包含在这里。
\end{verbatim}
```

## 常见原因

**在 \verb 命令中包含换行符：**

导致此错误的一个常见原因是你使用了一个 **`\verb`** 命令，但你包含了一个换行符，如下所示：

```latex
我们可以在 \LaTeX 中写出不同字体，如
\verb{
\textbf{粗体}
\textit{斜体}
\textsf{无衬线体}
}
```

这会产生错误，因为不允许在 **`\verb`** 命令内部使用换行符。正确的写法是使用 **`verbatim`** 环境，如下所示：

```latex
% 在导言区

\usepackage{verbatim}

% 在文档主体中

我们可以在 \LaTeX 中写出不同字体，如
\begin{verbatim}
\textbf{粗体}
\textit{斜体}
\textsf{无衬线体}
\end{verbatim}
```

![Verbatim.PNG](/files/9cc4014fd9ce5aabdf265d0d505f2b17bc0fb157)

**在 \verb 命令中省略结束字符：**

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

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

注意，这里的分隔符并不唯一，任何字符都可以。我们在这里使用 **`!`** 作为分隔符，是因为没有 **`!`** 写在 **`\frac{1}{2}`** 中。如果我们选择写成 **`\verb{\frac{1}{2}}`**，我们就会生成如下输出，这是不正确的。

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

![Delimiters.PNG](/files/75a67c324d52409074c2bc6ce563def3d1ad6c05)

这可能会导致问题，因为如果 **`\verb`** 的开头和结尾不是同一个字符，就会生成错误。因此，下面所有命令都会生成错误：

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

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

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

同样，建议直接使用 **`verbatim`** 环境来写成如下所示：

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

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

此错误出现的另一种方式是，如果一个 **`\verb`** 命令被用在另一个命令内部，如下所示

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

这在 LaTeX 中是不允许的，也是少数几种情况下， **`verbatim`** 环境也无能为力。要解决此错误，你应该加载 **`cprotect`** 宏包，如下所示：

```latex
% 在导言区

\usepackage{cprotect}

% 在文档主体中

\cprotect\section{这是一个带有逐字 \verb!\frac{1}{2}! 的章节标题}
这是一段文本
```

![Cprotect.PNG](/files/0c2906430efa79e7b4d45f6b3b2836e802a21826)


---

# 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/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.
