tags:

views:

684

answers:

1

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}

So I wanted to create a new command or environment that will shorthand this for me. I also need to do this for blocks and theorems, since I'm using those frequently, also. 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}
A: 

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

Steve
I appreciate the answer, but since I'm putting a lot of LaTeX code in the these "Verbatim Examples", having to escape all the \, {, and } characters will be more hassle than just typeing \begin{example}\begin{verbatim}...Thanks!
Jeremy Witmer
You're welcome. Yeah, verbatim and beamer tend not to play nice. Creative solutions usually don't exist. The best solution is probably to add shortcuts to your text editor, e.g., via tab completion.
Steve