Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

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

share|improve this answer
    
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 Jan 23 '13 at 17:50
add comment

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.

share|improve this answer
    
Yes, Yiannis. Those macros could well prove useful. Thanx. –  James B Jan 23 '13 at 18:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.