# Listings and multiline labels

I wish a multiline label in the lstlisting environment. I would like to reference more than one line within the same listing. Can you help me to find a solution?

I have found this link but this only helped me to refer to a certain line within the listing.

-
You want to reference "Lines 12 to 14"? That sort of thing? Can't you just do Lines \ref{first} to \ref{last}? –  Seamus Jan 2 at 12:26
Yes but I was curious to know if there is something better... –  Vincenzo Jan 2 at 14:33
In what way could something so simple be improved? –  Seamus Jan 2 at 14:53

The best way which was mentioned in the commend and which is written in the documentation is the following:

You can define escape characters. Using this way, it is also possible to put labels to be able, later, to make reference to some lines of the verbatim environment.

In the documenation of listings you can find in section 7 the following hint: How to reference line numbers (page 53).

Based on the given example here a complete MWE:

\documentclass{article}
\usepackage{listings}
\begin{document}

\lstset{escapeinside={(*@}{@*)}}
\begin{lstlisting}
if (i < 0)
i=0 (*@\label{commentI}@*)
j=1 end if
if (j < 0) j=0 (*@\label{commentII}@*)
end if
{ comment }
\end{lstlisting}
Lines \ref{commentI} to \ref{commentII}

\end{document}


-