> 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/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://ayakaleaf-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.
