14

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

\ref{list:mat.h}

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?

1 Answer 1

15

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.

\documentclass{article}

\usepackage{listings}
\begin{document}

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

In \ref{foo} we saw that \dots
\end{document}

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

#include<stdio.h>

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

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .