包含代码清单的可重述示例

包含代码清单的可重述示例

我正在创建一个 beamer 演示文稿,并使用了thmtoolsthm-restate和包listings。在我的一个\frames 中,我想创建一个example包含代码列表的块。稍后,我想重述整个块。

不幸的是,在 之前,lstlistings内部restatable块会导致以下错误\end{frame}

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


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

我该如何解决这个问题?

由于(在 beamer 文档中)example是一种预定义的定理类型,因此我不会用自己创建一个\newtheorem

梅威瑟:

\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

这可以通过将列表的内容保存在一个框中然后在您的restatable环境中使用该框来解决。

要保存列表框,请添加以下行:

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

要使用盒子,只需发出

\usebox{\listbox}

梅威瑟:

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

输出:

在此处输入图片描述

相关内容