TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I don't get page numbers of sections in the table of content correctly when I use the package lstlisting. I'm using the documentclass article. I need to use this class, not report.

Any ideas how this can be fixed?

share|improve this question

migrated from stackoverflow.com Jan 6 '12 at 11:31

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

Did you run latex a few times? – Kerrek SB Jan 6 '12 at 3:04
I don't understand your question. I make sure I'm not overrunning Latex. I close everything then try. – stupidity Jan 6 '12 at 4:32
Have a look at tex.stackexchange.com/questions/38802/… – lockstep Jan 6 '12 at 11:36
In order to get the correct page numbers, a couple of LaTeX runs are needed. – egreg Jan 6 '12 at 11:37
Information contained within the Table of Contents is extracted from a file that is created after you run latex. As such, it usually takes (at least) two separate runs in order for the entries to be there and correct. If this is not the case, edit your post and include a minimal working example (MWE) that illustrates your problem. – Werner Jan 6 '12 at 18:46
up vote 2 down vote accepted

Based on the comments I suspect that you are not running latex enough times. Compiling the example below the first time produces:

enter image description here

But upon subsequent run (assuming that you do not delete the temporary files that are generated upon the first run) you get:

enter image description here

\usepackage{lipsum}% for dummy text


 public int nextInt(int n) {
     if (n<=0)
        throw new IllegalArgumentException("n must be positive");

     if ((n & -n) == n)  // i.e., n is a power of 2
         return (int)((n * (long)next(31)) >> 31);

     int bits, val;
     do {
         bits = next(31);
         val = bits % n;
     } while(bits - val + (n-1) < 0);
     return val;

\section{First Section}


\section{Second Section}
share|improve this answer
Thanks all for the comments. I use Latexian for Mac. I figured that it runs 2 times by default. I changed it to 3 times, and that took care of the issue. – stupidity Jan 7 '12 at 1:24

Your Answer


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.