6

This code fits quite nicely my needs:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{lipsum}
\usepackage{hyperref}

\usepackage{lstlinebgrd}
\definecolor{myComment}{rgb}{0.0,0.5,0.0}
\definecolor{myKeyword}{rgb}{0.0,0.0,0.5}


\lstset
{
  aboveskip=\bigskipamount,
  belowskip=\bigskipamount,
  frame=tb,
  framesep=2pt,
  basicstyle=\scriptsize\ttfamily,
  keywordstyle=\color{myKeyword},
  commentstyle=\color{myComment}\itshape,
  captionpos=b,
  showstringspaces=false,
  fontadjust=true,
  language=Delphi,
  breaklines=true,
  numbers=left,
  numberstyle=\tiny,
  linebackgroundcolor={\ifodd\value{lstnumber}\color{gray!25}\fi},
  numbersep=5pt,
  escapeinside={(*@}{@*)},
}

\newcommand\coderef[1]{%
  $\Rightarrow$~\ref{#1}:~~%%
}%

\usepackage{hyperref}

\begin{document}

\begin{lstlisting}
program FileTest;

var
  myFile: file of Integer;

begin
  Assign(myFile, 'c:/test.ext'); (*@\label{FileTest-1}@*)
  Rewrite (myFile); (*@\label{FileTest-2}@*)
  Write(myFile, 1); (*@\label{FileTest-3}@*)
  Write(myFile, 2);
  Close(myFile); (*@\label{FileTest-4}@*)
end.
\end{lstlisting}


\coderef{FileTest-1} \lipsum[1]
\coderef{FileTest-2} \lipsum[2]
\coderef{FileTest-3} \lipsum[3]
\coderef{FileTest-4} \lipsum[4]

\end{document}

But in some cases it could be nice to repeat the whole line of code instead of the line number. I think that it's somewhat feasible with external source files, by using

\lstinputlisting[firstline=x,lastline=x] 

though I haven't investigated further. But is it feasible with sources in a lstlisting environment? And how?

0

3 Answers 3

4
+100

@FloDD, I have changed your solution a little bit to get the right numbers for you.

Your macro \lst@PlaceNumber is not neccessary, I commented it in the following MWE.

To get the right numbers for each line you have to add firstnumber=\thelstlinereffirst in your macro call of \lstinputlisting.

The result now are the right numbers.

The new MWE with a little bit pretty printing:

\RequirePackage{filecontents}
\begin{filecontents*}{source.pas}
program FileTest;

var
  myFile: file of Integer;

begin
  Assign(myFile, 'c:/test.ext'); (*@\label{FileTest-1}@*)
  Rewrite (myFile); (*@\label{FileTest-2}@*)
  Write(myFile, 1); (*@\label{FileTest-3}@*)
  Write(myFile, 2);
  Close(myFile); (*@\label{FileTest-4}@*)
end.
\end{filecontents*}


\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{lstlinebgrd}
\usepackage{refcount,lipsum}
\usepackage{hyperref}

\definecolor{myComment}{rgb}{0.0,0.5,0.0}
\definecolor{myKeyword}{rgb}{0.0,0.0,0.5}


\lstset
{
  aboveskip=\bigskipamount,
  belowskip=\bigskipamount,
  frame=tb,
  framesep=2pt,
  basicstyle=\scriptsize\ttfamily,
  keywordstyle=\color{myKeyword},
  commentstyle=\color{myComment}\itshape,
  captionpos=b,
  showstringspaces=false,
  fontadjust=true,
  language=Delphi,
  breaklines=true,
  numbers=left,
  numberstyle=\tiny,
  linebackgroundcolor={\ifodd\value{lstnumber}\color{gray!25}\fi},
  numbersep=5pt,
  escapeinside={(*@}{@*)},
}

\newcommand\coderef[1]{%
  $\Rightarrow$~\ref{#1}:~~%%
}%

\newcounter{lstlinereffirst}
\newcounter{lstlinereflast}
\makeatletter
\let\rc@refused\refused% See http://tex.stackexchange.com/q/32807/5764
\newcommand{\lineref}[3]{{%
  \let\label\@gobble%
  \setcounterref{lstlinereffirst}{#1}%
  \setcounterref{lstlinereflast}{#2}%
  \lstinputlisting[%
    firstline=\thelstlinereffirst,%
    lastline=\thelstlinereflast,%
    firstnumber=\thelstlinereffirst%
  ]{#3}
}}

%\def\lst@PlaceNumber{\llap{\normalfont% http://tex.stackexchange.com/q/110187/5764
                %\lst@numberstyle{\the\lst@lineno}\kern\lst@numbersep}}%\lst@linebgrd}

\makeatother

\begin{document}

\lstinputlisting{source.pas}

\coderef{FileTest-1} \lipsum[1]
\coderef{FileTest-2} \lipsum[2]
\lineref{FileTest-1}{FileTest-3}{source.pas} \lipsum[3]
\coderef{FileTest-4} \lipsum[4]

\end{document}

Result: enter image description here

0
8

I'm not entirely sure whether this is what you're after exactly, but it provides reasonable output.

enter image description here

\documentclass{article}
\usepackage{xcolor,listings,hyperref}% http://ctan.org/pkg/{xcolor,listings,hyperref}
\usepackage{filecontents,refcount,lipsum}% http://ctan.org/pkg/{filecontents,refcount,lipsum}
\lstset
{
  aboveskip=\bigskipamount,
  belowskip=\bigskipamount,
  frame=tb,
  framesep=2pt,
  basicstyle=\scriptsize\ttfamily,
  keywordstyle=\color{myKeyword},
  commentstyle=\color{myComment}\itshape,
  captionpos=b,
  showstringspaces=false,
  fontadjust=true,
  language=Delphi,
  breaklines=true,
  numbers=left,
  numberstyle=\tiny,
  numbersep=5pt,
  escapeinside={(*@}{@*)},
}
\definecolor{myComment}{rgb}{0.0,0.5,0.0}
\definecolor{myKeyword}{rgb}{0.0,0.0,0.5}

\newcommand{\coderef}[1]{%
  $\Rightarrow$~\ref{#1}:~~%%
}%
\newcounter{lstlineref}
\makeatletter
\let\rc@refused\refused% See https://tex.stackexchange.com/q/32807/5764
\newcommand{\lineref}[2]{{%
  \let\label\@gobble%
  \setcounterref{lstlineref}{#1}%
  \lstinputlisting[firstline=\thelstlineref,lastline=\thelstlineref]{#2}
}}
\def\lst@PlaceNumber{\llap{\normalfont% https://tex.stackexchange.com/q/110187/5764
                \lst@numberstyle{\the\lst@lineno}\kern\lst@numbersep}}
\makeatother


\begin{document}

\begin{filecontents*}{source.p}
program FileTest;

var
  myFile: file of Integer;

begin
  Assign(myFile, 'c:/test.ext'); (*@\label{FileTest-1}@*)
  Rewrite (myFile); (*@\label{FileTest-2}@*)
  Write(myFile, 1); (*@\label{FileTest-3}@*)
  Write(myFile, 2);
  Close(myFile); (*@\label{FileTest-4}@*)
end.
\end{filecontents*}
\lstinputlisting{source.p}

\coderef{FileTest-1} \lipsum[1]
\coderef{FileTest-2} \lipsum[2]
\lineref{FileTest-3}{source.p} \lipsum[3]
\coderef{FileTest-4} \lipsum[4]

\end{document}

The idea is to write the code to a file (using filecontents) and then input the entire listing - this promotes portability with the source code being contained inside your source .tex rather than being included as a stand-alone file elsewhere. Also, this allows for easy extraction of a line using the new macro \lineref{<ref>}{<source>}. It prints the line from <source> containing the reference <ref>. refcount provides the reference-to-counter interface. Also, a localized no-op for \label is initiated to avoid duplicate labels being made.

A line number correction is required when importing a range and wanting to maintain consistent numbering with the original source, thanks to Listings line numbers that match the linerange specification.

Depending on your distribution date, you may be short a definition of \rc@refused. I've included a patch that you should only use if you obtain errors.

1
  • This works almost fine: the 'lstlinebgrd' package seems affected by the '\lst@lineno' redefinition, that prevents the alternate background color to show up
    – FloDD
    Commented Jul 12, 2013 at 9:31
2

@Werner: I have modified a little bit your answer in order to better fit my needs:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{filecontents,refcount,lipsum}
\usepackage{hyperref}

\usepackage{lstlinebgrd}
\definecolor{myComment}{rgb}{0.0,0.5,0.0}
\definecolor{myKeyword}{rgb}{0.0,0.0,0.5}


\lstset
{
  aboveskip=\bigskipamount,
  belowskip=\bigskipamount,
  frame=tb,
  framesep=2pt,
  basicstyle=\scriptsize\ttfamily,
  keywordstyle=\color{myKeyword},
  commentstyle=\color{myComment}\itshape,
  captionpos=b,
  showstringspaces=false,
  fontadjust=true,
  language=Delphi,
  breaklines=true,
  numbers=left,
  numberstyle=\tiny,
  linebackgroundcolor={\ifodd\value{lstnumber}\color{gray!25}\fi},
  numbersep=5pt,
  escapeinside={(*@}{@*)},
}

\newcommand\coderef[1]{%
  $\Rightarrow$~\ref{#1}:~~%%
}%

\newcounter{lstlinereffirst}
\newcounter{lstlinereflast}
\makeatletter
\let\rc@refused\refused% See http://tex.stackexchange.com/q/32807/5764
\newcommand{\lineref}[3]{{%
  \let\label\@gobble%
  \setcounterref{lstlinereffirst}{#1}%
  \setcounterref{lstlinereflast}{#2}%
  \lstinputlisting[firstline=\thelstlinereffirst,lastline=\thelstlinereflast]{#3}
}}

\def\lst@PlaceNumber{\llap{\normalfont% http://tex.stackexchange.com/q/110187/5764
                \lst@numberstyle{\the\lst@lineno}\kern\lst@numbersep}\lst@linebgrd}

\makeatother

\begin{document}

\begin{filecontents*}{source.pas}
program FileTest;

var
  myFile: file of Integer;

begin
  Assign(myFile, 'c:/test.ext'); (*@\label{FileTest-1}@*)
  Rewrite (myFile); (*@\label{FileTest-2}@*)
  Write(myFile, 1); (*@\label{FileTest-3}@*)
  Write(myFile, 2);
  Close(myFile); (*@\label{FileTest-4}@*)
end.
\end{filecontents*}
\lstinputlisting{source.pas}


\coderef{FileTest-1} \lipsum[1]
\coderef{FileTest-2} \lipsum[2]
\lineref{FileTest-1}{FileTest-3}{source.pas} \lipsum[3]
\coderef{FileTest-4} \lipsum[4]

\end{document}

enter image description here

Now I can specify a range of lines of code and draw the zebra-styled background too. Thanks for your help!

1
  • I thought this was solved, but there's something that goes bad here: the line numbers on blank lines are wrong. I should have had a better look at the pdf...
    – FloDD
    Commented Jul 15, 2013 at 17:15

You must log in to answer this question.

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