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?
Based on the comments I suspect that you are not running latex enough times. Compiling the example below the first time produces:
But upon subsequent run (assuming that you do not delete the temporary files that are generated upon the first run) you get:
\documentclass{article}
\usepackage{listings}
\usepackage{lipsum}% for dummy text
\usepackage{filecontents}
\begin{filecontents*}{foo.java}
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;
}
\end{filecontents*}
\begin{document}
\tableofcontents\newpage
\section{First Section}
\lipsum[1-7]
\lstinputlisting{foo.java}
\section{Second Section}
\lipsum[8-12]
\end{document}
latex
a few times?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.