GeneralReferring line in listing

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
yurichev
Posts: 2
Joined: Sat May 17, 2014 9:13 pm

Referring line in listing

Post by yurichev »

Maybe not a TeX question, but are there a some common way to refer specific line in listing, like:

"In listing 1.2.3:10 we see this and that..." (i.e., line 10).

And also line range, like "listing 1.2.3:10-14".

I just hate idea to write "... at line x ...", or "... at lines from x to y ...", etc.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Referring line in listing

Post by kaiserkarl13 »

I refer you to Section 7 of the listings manual, which has a heading that says, "How to reference line numbers."

Their example has something like this (for Pascal, which uses (* and *) as "digraph" comments):

Code: Select all

\lstset{escapeinside={(*@}{@*)}}
\begin{lstlisting}
for i:=maxint to 0 do
begin
  { my comment }(*@\label{comment}@*)
end;
\end{lstlisting}
Line \ref{comment} shows a comment.
Depending on the language, it should be possible to introduce something that (a) notifies the listings package not to typeset that particular comment but instead treat it as an actual comment, and (b) preserves the programming languages comment rules. For example, in C, you could use something like
\lstset{escapeinside={/*@}{*/}}
or something like
\lstset{escapeinside={#%}{\^^M}}
for shell scripts.
Post Reply