我正在创建一个 Beamer 演示文稿,其中包含很多示例 LaTeX,它必须在逐字环境中进行。我厌倦了打字
\begin{example}
\begin{verbatim}
Verbatim Text
\end{verbatim}
\end{example}
我希望创建一个新的命令或环境来为我简化它。对于块和定理,我也需要它,因为我也经常使用它们。但如果我能举例说明,应该很容易翻译成另一个例子。
我无法仅使用 \begin{verbatim} 创建新环境或命令,因为它会切断命令的其余部分。所以我改用了 fancyvrb 包,并尝试了以下方法:
\DefineVerbatimEnvironment
{MyVerbatim}{Verbatim}{}
\newcommand{\makeexample}[1]{
\begin{example}
\begin{MyVerbatim}
#1
\end{MyVerbatim}
\end{example}
}
\makeenvironment{VerbExample}{\begin{example}
\begin{MyVerbatim}}{\end{MyVerbatim}\end{example}}
这给了我 \makeexample{Example Text} 命令和 \begin{VerbExample}...\end{VerbExample} 环境,但它们仍然在编译时抛出错误。我试图在其中使用它们的框架看起来像这样(我在框架上有 [fragile] 选项,所以不是那样)。
\begin{frame}[fragile]
\frametitle{Why Doesn't Verbatim Work?}
\makeexample{Verbatim Text}
\begin{VerbExample}
Verbatim Text
\end{VerbExample}
\end{frame}