beamer + lstlistings + overlay -> 标题编号错误

beamer + lstlistings + overlay -> 标题编号错误

在我的 tex 文件中,我有以下代码。它导致列表的编号增加索引。有没有办法防止这种情况发生?(见截图:第二张幻灯片也应该说“列表 1:函数”)平均能量损失

\documentclass{beamer}
\usetheme{Goettingen}
%\usecolortheme{whale}

\setbeamertemplate{frametitle continuation}[from second] % (cont.) from second slide
\setbeamertemplate{bibliography item}{[\theenumiv]} % numbers instead of silly pictures
\setbeamertemplate{footline}[frame number]
\beamertemplatenavigationsymbolsempty % remove navigationbar

\usepackage[font=bf,skip=\baselineskip]{caption}
\captionsetup[lstlisting]{font={footnotesize}}

\usepackage{color}
\definecolor{light-gray}{gray}{0.85}
\definecolor{darkgreen}{rgb}{0.0, 0.5, 0.0} % for green colour in code
\usepackage{listings}
\usepackage[T1]{fontenc}
\lstset{
    basicstyle=\tiny,
    escapeinside={<@}{@>},
    numbers=left, 
    language=C, 
    morekeywords={throw, assert},
    frame=single,
    captionpos=b,
    commentstyle=\color{darkgreen}}


\begin{document}
\begin{frame}[fragile,containsverbatim]{Example}
    \begin{lstlisting}[caption={Function}]
functionA(x){
  a = functionB(x)
  return a
}

functionB(x){
  b =  <@\only<1>{GLOBAL\_VAR}\only<2>{\textcolor{darkgreen}{GLOBAL\_VAR}}@>
  b++
  return b
}
    \end{lstlisting}
\end{frame}

\end{document}

答案1

你需要的是

\resetcounteronoverlays{lstlisting}

以防止使用覆盖时计数器增加。完整代码:

\documentclass{beamer}
\usetheme{Goettingen}
%\usecolortheme{whale}

\setbeamertemplate{frametitle continuation}[from second] % (cont.) from second slide
\setbeamertemplate{bibliography item}{[\theenumiv]} % numbers instead of silly pictures
\setbeamertemplate{footline}[frame number]
\beamertemplatenavigationsymbolsempty % remove navigationbar

\usepackage[font=bf,skip=\baselineskip]{caption}
\captionsetup[lstlisting]{font={footnotesize}}

\usepackage{color}
\definecolor{light-gray}{gray}{0.85}
\definecolor{darkgreen}{rgb}{0.0, 0.5, 0.0} % for green colour in code
\usepackage{listings}
\usepackage[T1]{fontenc}
\lstset{
    basicstyle=\tiny,
    escapeinside={<@}{@>},
    numbers=left, 
    language=C, 
    morekeywords={throw, assert},
    frame=single,
    captionpos=b,
    commentstyle=\color{darkgreen}}

\resetcounteronoverlays{lstlisting}

\begin{document}
\begin{frame}[fragile,containsverbatim]{Example}
    \begin{lstlisting}[caption={Function}]
functionA(x){
  a = functionB(x)
  return a
}

functionB(x){
  b =  <@\only<1>{GLOBAL\_VAR}\only<2>{\textcolor{darkgreen}{GLOBAL\_VAR}}@>
  b++
  return b
}
    \end{lstlisting}
\end{frame}

\end{document}

结果:

在此处输入图片描述

相关内容