原 文:LaTeX Error: \verb ended by end of line
譯 者:Xovee
翻譯時間:2020年6月13日
LaTeX Error: \verb ended by end of line
當你錯誤地使用了 verbatim 命令的時候,就會遇到這個錯誤:
爲了解決這個錯誤,你最好使用 verbatim 環境:
\begin{verbatim}
You should include your verbatim text in here.
\end{verbatim}
出錯原因
在 \verb
命令中使用了換行符 \
出錯的一個原因是,如果你使用了 \verb 命令,但是你在之中包含了換行符,例如:
We can write different typefaces in \LaTeX as
\verb{
\textbf{Bold}
\textit{italics}
\textsf{sans serif}
}
這將拋出一個錯誤,因爲換行符在 \verb 命令中是不被允許出現的。解決這個錯誤的辦法是使用 verbatim 環境:
% In your preamble
\usepackage{verbatim}
% In the main body of your document
We can write different typefaces in \LaTeX as
\begin{verbatim}
\textbf{Bold}
\textit{italics}
\textsf{sans serif}
\end{verbatim}
在 \verb 命令裏忽略了結尾的符號
\verb 命令應該被這樣使用:
\verb!\frac{1}{2}!
在這個例子中,我們使用了 !
作爲分隔符。這個選擇並不是唯一的,其他的分隔符也可以使用。在這裏我們選擇感嘆號的原因是 \frac{1}{2}
中並沒有感嘆號出現。如果我們選擇 {}
作爲分隔符,那麼這個命令就會變成 \verb{\frac{1}{2}}
,如下所示:
\verb{\frac{1}{2}}
這將會導致錯誤,因爲 \verb 命令的起始分隔符和結尾分隔符並不是一樣的。因爲這個原因,下面的這些用法也都會導致錯誤:
\verb \frac{1}{2}
{\verb \frac{1}{2}}
\verb!\frac{1}{2}
所以,我們在這裏推薦你使用 verbatim 環境:
\begin{verbatim}
\frac{1}{2}
\end{verbatim}
在另外一個命令中使用 \verb
命令
如果你在另外一個命令中使用 \verb
命令,那麼這個錯誤也可能發生:
\section{\verb!\frac{1}{2}!}
這在 LaTeX 之中是不允許的,就算使用 verbatim 環境也無濟於事。爲了解決這個錯誤,你需要加載 cprotect 包:
% In your preamble
\usepackage{cprotect}
% In the main body of your document
\cprotect\section{This is a section heading with a verbatim \verb!\frac{1}{2}!}
This is some text