How do I create a new Beamer environment with a verbatim environment?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-22 18:21:32

问题


I'm creating a Beamer presentation that has a lot of example LaTeX in it, which has to go in a verbatim environment. I'm getting tired of typing

\begin{example}
  \begin{verbatim}
  Verbatim Text
  \end{verbatim}
\end{example}

I wish to create a new command or environment that will shorthand this for me. I also need this for blocks and theorems, since I'm using those frequently as well. But if I can figure it out for examples, it should be easy to translate to another example.

I can't create a new environment or command using just \begin{verbatim}, since it cuts off the rest of the command. So I switched to using the fancyvrb package, and tried the following:

\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}}

That gives me the \makeexample{Example Text} command, and the \begin{VerbExample}...\end{VerbExample} environment, but they both still throw errors on compile. The frame I'm trying to use them in looks like this (I've got the [fragile] option on the frame, so it's not that).

\begin{frame}[fragile]
    \frametitle{Why Doesn't Verbatim Work?}

    \makeexample{Verbatim Text}

    \begin{VerbExample}
        Verbatim Text
    \end{VerbExample}
\end{frame}

回答1:


Environment definition:

\newenvironment{VerbExample}
{\example\semiverbatim}
{\endsemiverbatim\endexample}

Frame definition:

\begin{frame}[fragile]
\frametitle{Title}
\begin{VerbExample}
test test test $t$ $\\omega$
test test
\end{VerbExample}
\end{frame}

Verbatim cannot go inside \newcommand. Semiverbatim is defined by Beamer and works well with it. The three characters \ { } must be escaped as \\ \{ \}.

Source: Beamer user guide, pp. 119-120 http://www.ctan.org/tex-archive/macros/latex/contrib/beamer/doc/beameruserguide.pdf



来源:https://stackoverflow.com/questions/2121035/how-do-i-create-a-new-beamer-environment-with-a-verbatim-environment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!