# How to use correctly the reference on lstinputlisting command?

In my tex I have this command:

\lstinputlisting[breaklines=true,label={lst:use_case_ip}]{ch5/code/get.txt}
\captionof{lstlisting}{HTTP Request where string was found}


which generate 1.2 value

when I use ref command, like

The listings \ref{lst:use_case_ip}


I've got 1.4.1 value... that's completely wrong

where is my mistake?

Your mistake is that \caption is responsible from generating the anchor that the \label, \ref mechanism will pick for cross-referencing and you are using label before assigning a caption.

Instead of \captionof, use the caption key; otherwise. the wrong string will be picked for cross-referencing:

\lstinputlisting[breaklines=true,caption={HTTP Request where string was found},label={lst:use_case_ip}]{ch5/code/get.txt}


You can control the position of the caption using the captionpos key (possible values: b for bottom, t for top):

\lstset{
captionpos=b
}

• nice, it works. but It put the caption above the listing... is there an attribute to put it after the listing? – CDominik Oct 10 '15 at 14:51
• @DomenicoChiarito Sure: captionpos=b. – Gonzalo Medina Oct 10 '15 at 14:53