1

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.com Dec 27 '14 at 12:33

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

3

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:

Output

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

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.