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:

  • 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

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


in the preamble and your problem should be resolved.

|improve this answer|||||

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

\usepackage{listings}% http://ctan.org/pkg/listings
\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;

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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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