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 am using the \lstnewenvironment to format a set of code listings in my document.

\lstnewenvironment{code}
    {\lstset{language=haskell,
    basicstyle=\small\ttfamily,
    numbers=left,
    numberstyle=\tiny\color{gray},
    backgroundcolor=\color{lightgray},
    firstnumber=auto
    }}
    {}

I would like to be able to give all these listings the same name, so that they will be continuously numbered, even if other listings (not using the code environment) are scattered in between them.

However I cannot add the name property using \lstset. The top of page 11 of the listings manual indicates that there are certain properties that can only be set as options directly on the listing (and not in \lstset). I suspect this is one of those properties. So I would have to do

\begin{code}[name=main]
...
\end{code}

on every listing. Is there some other way I can indicate that the code environment should always carry that name option?

share|improve this question
    
By numbers, are you referring to line numbers (set using name), or listing numbers? Also, by virtue of minimal testing (and reflected in Peter's answer), you can set name=main as part of \lstset. –  Werner Jun 22 '12 at 18:30

2 Answers 2

up vote 5 down vote accepted

Thanks to egreg, you can use a custom counter to keep track of the last line number and resume from there:

enter image description here

Code:

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

\newcounter{main}
\setcounter{main}{1}

\lstnewenvironment{code}[1][firstnumber=\themain,name=main]
  {\lstset{language=haskell,
           basicstyle=\small\ttfamily,
           numbers=left,
           numberstyle=\tiny\color{gray},
           backgroundcolor=\color{lightgray},
           #1
          }
}
{\setcounter{main}{\value{lstnumber}}}


\begin{document}
\begin{code}
 foo bar first line
 foo bar second line
 foo bar third line
\end{code}

some text

\begin{lstlisting}[backgroundcolor=\color{lightgray!20}]
  some other non-code listing
\end{lstlisting}


\begin{code}
 listing continued
\end{code}
\end{document}
share|improve this answer
    
This works, if I move firstnumber=\themain into \lstset. I'm fairly certain name=main is simply being ignored -- which is ok -- using a separate counter that resets to \lstcounter handles the line numbering perfectly. –  David Jun 22 '12 at 23:45
    
@David: Agreed, but specifying that as the first optional parameter allows you to override it if ever needed. –  Peter Grill Jun 22 '12 at 23:56

Note that in listings.sty, lstlisting is defined by

\lstnewenvironment{lstlisting}[2][]
    {\lst@TestEOLChar{#2}%
     \lstset{#1}%
     \csname\@lst @SetFirstNumber\endcsname}
    {\csname\@lst @SaveFirstNumber\endcsname}

Copying the lines \csname\@lst @SetFirstNumber\endcsname and \csname\@lst @SaveFirstNumber\endcsname into your own \lstnewenvironment (after the appropriate \makeatletter trick) should work with the usual name key defined in \lstset. See it in the context of the following compilable example, borrowed from Peter Grill's answer:

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

\makeatletter
\lstnewenvironment{code}[1][]
    {\lstset{language=haskell,
    basicstyle=\small\ttfamily,
    numbers=left,
    numberstyle=\tiny\color{gray},
    backgroundcolor=\color{lightgray},
    firstnumber=auto,
    name=main,
    #1}%
    \csname\@lst @SetFirstNumber\endcsname}
    {\csname\@lst @SaveFirstNumber\endcsname}
\makeatother

\begin{document}
\begin{code}
 foo bar first line
 foo bar second line
 foo bar third line
\end{code}

some text

\begin{lstlisting}[backgroundcolor=\color{lightgray!20}]
  some other non-code listing
\end{lstlisting}


\begin{code}
 listing continued
\end{code}
\end{document}
share|improve this answer
    
Can you put together a compilable example that shows that this actually works. I tried and the numbering did not resume when this new environment was used a second time. –  Peter Grill Oct 12 '12 at 0:45
    
@PeterGrill: Compilable example now added. (Sorry, I should have done that in the first place!) I borrowed the example from your solution, hope you don't mind. –  cyberSingularity Oct 12 '12 at 8:39
    
Thanks, and it works great. Will be removing these comments soon. –  Peter Grill Oct 12 '12 at 17:26

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.