[Tex/LaTex] Reference to a listing returns wrong number


I'm using the following code to create a listing with sourcecode:

// Code...

Unfortunately, a \ref{lst:a_label} somewhere in my text does not return the correct number, or at least not the number I expected. The caption itself shows the correct number. How can I fix that?

Best Answer

I think the lstlisting environment prefers the label to be inserted as a normal parameter:

Adapt the following code to your needs and see if you obtain what you want:


\begin{lstlisting}[caption={Some Java code},label={lst:label},language=Java]
// Code...
See code~\ref{lst:label}.
Related Question