# How to add labels in verbatim env. in Latex?

I'm writing master thesis where I need to include some parts of program code. This code needs to be commented and properly described in text below/above. I would like to add some specific labels in the verbatim environment and use this labels as references in text. How can I do that?

## migrated from stackoverflow.comDec 27 '14 at 12:33

This question came from our site for professional and enthusiast programmers.

Thanks for your help guys. Based on question Is there a possibility to make reference to the line in the lstlisting environment? I've finaly used following:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}

\begin{document}
\lstset{language=Pascal}
\lstset{escapeinside={(*@}{@*)}}
\begin{lstlisting}[frame=single, caption={Useless code}, label=useless_code, numbers=left]
for i:=maxint to 0 do begin
{ comment }(*@\label{comment}@*)
end;
\end{lstlisting}
Line \ref{comment} of Listing~\ref{useless_code} shows a comment.

\end{document}


See following output:

I know that it is not optimal solution, but for the purpose of my thesis seems to be sufficient solution.