Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm writing a TeX-File for a beamer presentation and have a problem with the \pause-command and the listing counter:

The Listing counter increments its count with every \pause command – although I don't change the frame or anything at the code. This looks very ugly. So I need to either stop the counter from incrementing its count with every \pause command or turn off the counter at all – and I couldn't even find out how this works.

Can someone please help me?

Here some of my listings-Setting:

\definecolor{kit-gray}{RGB}{224,224,224}
\definecolor{kit-green}{RGB}{32,149,128}
\usepackage{listings}
\usepackage{courier}
\lstset{
     basicstyle=\scriptsize\ttfamily, 
     numbers=left,              
     numberstyle=\tiny,         
     %stepnumber=2,                 
     numbersep=5pt,              
     tabsize=2,                         
     extendedchars=true,         
     breaklines=true,               
     %keywordstyle=\color{red},
     frame=t,         
 %frameround=tftf, 
     keywordstyle=[1]\textbf,       
     stringstyle=\color{blue}\ttfamily,     
     showspaces=false,              
     showtabs=false,                
     xleftmargin=17pt,
     framexleftmargin=17pt,
     framexrightmargin=6pt,
     framexbottommargin=4pt,
     backgroundcolor=\color{kit-gray},
     commentstyle=\color{kit-green},
     showstringspaces=true          
     %numberbychapter=false
}
share|improve this question

migrated from stackoverflow.com Nov 8 '11 at 14:35

This question came from our site for professional and enthusiast programmers.

2  
Please add to your question a proper minimal working example illustrating the problem. –  Gonzalo Medina Nov 8 '11 at 14:42
2  
I simply wouldn't use a caption: the frame title is sufficient to tell what listing you're talking about. Members of the audience won't recall reference by numbers anyway. –  egreg Nov 8 '11 at 17:24
add comment

2 Answers 2

As you are aware, beamer will create a new page for each overlay. Each time a page containing a lstlisting environment is created the counter lstlisting is incremented. As this goes for any counter (tables, figures, etc.) beamer supports resetting the counter on overlays. Place

\resetcounteronoverlays{lstlisting}

in the preamble and your problem should be resolved.

share|improve this answer
add comment

If you typeset a regular lstlisting on a frame with a caption, this caption is rendered after every \pause command, incrementing the listing number (counter lstnumber).

The best option here is to set a label to the counter and then reference it as a manual caption made to look similar to when you're using the traditional caption={...} key-value. Rather use the title={...} key-value. Here is the idea in MWE form using the article document class (since no MWE was provided):

enter image description here

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\begin{document}
\setcounter{lstnumber}{0}\refstepcounter{lstnumber}\label{mylisting}% Correct listing reference
\begin{lstlisting}[title={Listing~\ref{mylisting}:\ This is a caption},language=C]
#include <stdio.h>

int main(int argc, char ** argv)
{
  printf("Hello world!\n");
  return 0;
}
\end{lstlisting}
\end{document}​

listings sets the starting value of lstnumber to 1 (in contrast to other environments that uses counter (initialized to 0). So, we first set it to 0 using \setcounter{lstnumber}{0}, then increment it with a correct reference using \refstepcounter{lstnumber}. This sets it to 1, and then we label it using \label{mylisting} (or something similar). Subsequently use Listing~\ref{mylisting}: ... as the title to provide the correct reference.

You may have to manually increment the counter after your listing if you use more listings later in your beamer presentation. If needed, use \stepcounter{lstnumber}. Note also that the counter setting/incrementing should be done outside (before) the frame environment to avoid multiple references.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.