11

I have been using the listings package to present my source code in LaTeX. However, I can't get the line numbers to continue between two separate user-defined listings that have been given the same name. For example, the following code just seems to start the line number at 1 for each listing block rather than sequentially numbering them:

\usepackage{listings}

\lstnewenvironment{bash}[1][]{
#1,
language = bash,
numbers = left,
firstnumber = auto}

\begin{document}

\begin{bash}[name=Test]
echo xyz
\end{bash}
\begin{bash}[name=Test]
echo xyz
\end{bash}

\end{document}

I'm aware I can set the number manually each time with \firstnumber = last, but this kind of defeats the object of naming your listing. I presume I'm missing something here. Any help would be greatly appreciated.

2 Answers 2

8

A slightly different approach from Heiko's fine answer:

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

\lstnewenvironment{bash}[1][]
  {\lstset{language=C}\lstset{%
   numbers=left,numberstyle=\normalsize,stepnumber=1,numbersep=5pt,
   framesep=5pt,
   basicstyle=\normalsize\ttfamily,
   showstringspaces=false,
   keywordstyle=\itshape\color{blue},
   stringstyle=\color{maroon},
   commentstyle=\color{black},
   xleftmargin=5pt,
   xrightmargin=5pt,
   aboveskip=\bigskipamount,
   belowskip=\bigskipamount,
   backgroundcolor=\color{gray!20}, #1
}}
{}


%
%%% Always I forget this so I created some aliases
\def\ContinueLineNumber{\lstset{firstnumber=last}}
\def\StartLineAt#1{\lstset{firstnumber=#1}}
\let\numberLineAt\StartLineAt



\begin{document}

\begin{bash}[name=Test]
echo xyz
\end{bash}

\StartLineAt{30}
\begin{bash}[name=Test]
echo xyz
\end{bash}

\ContinueLineNumber


\begin{bash}[name=Test]
echo xyz
\end{bash}

\end{document}

The code offers two macros that I personally find convenient

 \ContinueLineNumber

and

 \StartLineAt

If you wouldn't like all your code to have consecutive numbering you might find it useful.

1
  • Yes, Yiannis. Those macros could well prove useful. Thanx.
    – James B
    Commented Jan 23, 2013 at 18:11
5

The code for saving and restoring the line number is not called in case of a environment defined by \lstnewenvironment. The following adds it:

\documentclass{article}

\usepackage{listings}

\makeatletter
\lstnewenvironment{bash}[1][]{%
  \lstset{%
    #1,
    language=bash,
    numbers=left,
    firstnumber=auto,
  }%
  \csname\@lst @SetFirstNumber\endcsname
}{%
  \csname \@lst @SaveFirstNumber\endcsname
}
\makeatother

\begin{document}

\begin{bash}[name=Test]
echo xyz
\end{bash}
\begin{bash}[name=Test]
echo xyz
\end{bash}

\end{document}

Result

1
  • Perfect. I could not find these details in the Listings documentation. Perhaps they should be included as it provides a powerful way to use the Listings package if you have to present a lot of syntax. Thanks Heiko.
    – James B
    Commented Jan 23, 2013 at 17:50

You must log in to answer this question.

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