# lstinputlisting shows wrong number in reference

I'm pretty new to LaTeX, so please be a little patiently with me.

So here is my problem:

I got some text and there i want to reference a little bit code shown in the next line.

What i tried so far:

Variante 1:

Im Quellcodausschnitt~\ref{lst:product_xml} ist zu sehen, dass drei Attribute definiert wurden.

\lstinputlisting
[caption={Übergeordneter Knoten Product}
\label{lst:product_xml},
firstline=2, lastline=2,
language=XML]
{code/product_xml.xml}


the output looks just fine with one exception, there is the wrong number behind Quellcodausschnitt

Quellcodausschnitt 2

but the caption says

Code 4.1: Übergeordneter Knoten Product

Variante 2

\lstinputlisting
[caption={Übergeordneter Knoten Product}
label={lst:product_xml},
firstline=2, lastline=2,
language=XML]{code/product_xml.xml}


i found that here

Outcome was:

Quellcodausschnitt ??

i don't want to use

\begin{lstlisting}
\end{lstlisting}


cause i want to load some more lines from that file. so the solution here is not an option for me

full document:

\documentclass{article}

\usepackage[ngerman]{babel}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{color}

\usepackage[savemem]{listings}

\renewcommand{\lstlistingname}{Code}

\definecolor{lbcolor}{rgb}{0.85,0.85,0.85}
\lstset{language=[LaTeX]TeX,
numbers=left,
stepnumber=1,
numbersep=5pt,
numberstyle=\tiny,
breaklines=true,
captionpos=b,
breakautoindent=true,
postbreak=\space,
tabsize=2,
basicstyle=\ttfamily\footnotesize,
showspaces=false,
showstringspaces=false,
extendedchars=true,
backgroundcolor=\color{lbcolor}}

\definecolor{maroon}{rgb}{0.5,0,0}
\definecolor{darkgreen}{rgb}{0,0.5,0}
\lstdefinelanguage{XML}
{
basicstyle=\ttfamily,
morestring=[s]{"}{"},
morecomment=[s]{?}{?},
morecomment=[s]{!--}{--},
moredelim=[s][\color{black}]{>}{<},
moredelim=[s][\color{red}]{\ }{=},
stringstyle=\color{blue},
identifierstyle=\color{maroon}
}

\begin{document}

%  \lstlistoflistings

Im Quellcodausschnitt~\ref{lst:product_xml} ist zu sehen, dass drei Attribute definiert wurden

\lstinputlisting
[caption={Übergeordneter Knoten Product} %!!!! i did a great mistakte here i forgot to set a comma at the end of the line
label={lst:product_xml},
firstline=2, lastline=2,
language=XML]{product_xml.xml}

\end{document}


is there a way to fix this, or what am i doing wrong?

• Did you compile twice? If this does not solve the problem, post the complete code, not just fragments only? I also think it should be label={...} here for listings – user31729 May 25 '16 at 14:37
• what do you mean by not just fragments? the only thing that i left out was the header.tex, cause it's a bit to long / i let kile compile, should i compile via terminal? – Mofty May 25 '16 at 14:45
• Casting the fragments into something compilable it provides the correct reference number after two compilations – user31729 May 25 '16 at 14:45
• It's a fragment: I only could check for myself because I have a scratch document on my disk here posting your fragment and running. Other users might don't have this and new users do not profit from such lines only. The usual way to post here is to reduce the document to the absolute minium version that still shows the issue. We also don't have your .xml file at hand, that it's read by the lstinputlisting command, so compilation would fail again – user31729 May 25 '16 at 14:46

This works: Compile twice and the reference is showing up correctly.

Use the label= key of the \lstinputlisting command to provide the label name, not something like \caption...\label.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}

\usepackage{listings}

\begin{document}

Im Quellcodausschnitt~\ref{lst:product_xml} ist zu sehen, dass drei Attribute definiert wurden

\lstinputlisting[caption={Übergeordneter Knoten Product},
label={lst:product_xml},
%firstline=2, lastline=2,
language=C]
{helloworld.c}
%  {code/product_xml.xml}

\end{document}


And the helloworld.c file:

#include<stdio.h>

int main(int argc,char **argv)
{
printf("Hello World!\n");
return(0);
}


• thanks, the comma after the caption did the trick, but still, why can't i use the \label{lst:...} command? it works for figures just fine – Mofty May 25 '16 at 16:00
• @Mofty: This is a different setup -- listings is a floating environment, but uses a different \caption mechanism. That's why it is caption=... and label=... – user31729 May 25 '16 at 16:13