
在我的 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}
結果: