5

I am creating a beamer presentation, and I'm using the packages thmtools, thm-restate and listings. Inside one of my \frames, I want to create an example block containing a code listing. Later, I want to restate this entire block.

Unfortunately, lstlistings inside restatable blocks causes the following error, right before the \end{frame}:

Package Listings Warning: Text dropped after begin of listing on input line 18.


)
! Missing } inserted.
<inserted text>
                }
l.58 \end{frame}

How can I resolve this?

Since (in beamer documents) example is a pre-defined theorem type, I am not creating one myself with \newtheorem.

MWE:

\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{textcomp}
\usepackage{thmtools,thm-restate}
\usepackage{listings}
\lstset{language=[Sharp]C,
    captionpos=b,
    showspaces=false,
    showtabs=false,
    linewidth=\textwidth{},
    breaklines=true,
    showstringspaces=false,
    breakatwhitespace=true,
    escapeinside={(*@}{@*)},
    basicstyle=\ttfamily\fontseries{c},
    morekeywords=[1]{partial},
    literate={\\\-}{}{0\discretionary{{\normalfont \texttildelow}}{}{}}
}

\title{Presentation}
%\subtitle{}
\institute[UoF]{University of Foo}
\author{derabbink}
\date{\today}

\begin{document}

\begin{frame}[fragile]
    \frametitle{Code}
    \begin{restatable}[Human readable code]{example}{humanCodeI}
    \begin{itemize}
        \item \emph{Wait} for `Click Me!' button to become enabled
        \item \emph{Click} `Click Me!' button
        \item \emph{Verify} that `Click Me!' button is disabled
    \end{itemize}
    \end{restatable}

    % this causes an error
    \begin{restatable}[C\# code]{example}{csCodeI}
        \begin{lstlisting}
            Button btn = GuiHelper.FindButton("Click Me!");
            GuiHelper.WaitUntilEnabled(btn, Is.True);
            GuiHelper.Click(btn);
            Assert.That(GuiHelper.IsEnabled(ping), Is.False);
        \end{lstlisting}
    \end{restatable}

    % this doesn't cause errors
%   \begin{example}[C\# code]
%       \begin{lstlisting}
%           Button btn = GuiHelper.FindButton("Click Me!");
%           GuiHelper.WaitUntilEnabled(btn, Is.True);
%           GuiHelper.Click(btn);
%           Assert.That(GuiHelper.IsEnabled(ping), Is.False);
%       \end{lstlisting}
%   \end{example}
\end{frame}

\end{document}

1 Answer 1

5

This can be resolved saving the contents of the listing in a box and then use that box inside your restatable environment.

To save the listing box add the lines:

\newsavebox{\listbox}

\begin{lrbox}{\listbox}
\begin{lstlisting}
  Button btn = GuiHelper.FindButton("Click Me!");
  GuiHelper.WaitUntilEnabled(btn, Is.True);
  GuiHelper.Click(btn);
  Assert.That(GuiHelper.IsEnabled(ping), Is.False);
\end{lstlisting}
\end{lrbox}

while to use the box, simply issue

\usebox{\listbox}

MWE:

\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{textcomp}
\usepackage{thmtools,thm-restate}
\usepackage{listings}
\lstset{language=[Sharp]C,
    captionpos=b,
    showspaces=false,
    showtabs=false,
    linewidth=\textwidth{},
    breaklines=true,
    showstringspaces=false,
    breakatwhitespace=true,
    escapeinside={(*@}{@*)},
    basicstyle=\ttfamily\fontseries{c},
    morekeywords=[1]{partial},
    literate={\\\-}{}{0\discretionary{{\normalfont \texttildelow}}{}{}}
}

\title{Presentation}
%\subtitle{}
\institute[UoF]{University of Foo}
\author{derabbink}
\date{\today}

\begin{document}

\newsavebox{\listbox}

\begin{lrbox}{\listbox}
\begin{lstlisting}
  Button btn = GuiHelper.FindButton("Click Me!");
  GuiHelper.WaitUntilEnabled(btn, Is.True);
  GuiHelper.Click(btn);
  Assert.That(GuiHelper.IsEnabled(ping), Is.False);
\end{lstlisting}
\end{lrbox}

\begin{frame}[fragile]
    \frametitle{Code}
    \begin{restatable}[Human readable code]{example}{humanCodeI}
    \begin{itemize}
        \item \emph{Wait} for `Click Me!' button to become enabled
        \item \emph{Click} `Click Me!' button
        \item \emph{Verify} that `Click Me!' button is disabled
    \end{itemize}
    \end{restatable}

    \begin{restatable}[C\# code]{example}{csCodeI}
        \usebox{\listbox} % insert the box here
    \end{restatable}

\end{frame}

\end{document} 

Output:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .