I am using the listingspackage to include source code in my .tex-document. Now I would like to reference to these lstinputlistings. Therrefore, I am using this

lstinputlisting[label=lst:mat.h, title=\textbf{src:} mat.h]{src/mat.h}

to include the file and


to reference to it. Now I do not get an error message, but instead of a number to this listing I only get the number of the (sub)section. Is there any way to solve this problem?


Without using the caption option, the \label applies the \@currentlabel settings which has been modified by the last \refstepcounter, apparently in the OP this was some \subsection operation.

Solution: Apply caption=... as an option in the optional argument of \lstinputlisting -- this will print the listing number as well and adds the listing to the List of Listings.



\lstinputlisting[label={foo},caption={A difficult example in C}, language={C}]{helloworldexample.c}

In \ref{foo} we saw that \dots

The hellowordexample.c is very short, but sufficient for testing:


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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.