# Create a \lstnewenvironment that creates named listings

I am using the \lstnewenvironment to format a set of code listings in my document.

\lstnewenvironment{code}
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?

-
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

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

## Code:

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

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

\lstnewenvironment{code}[1][firstnumber=\themain,name=main]
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}

-
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][]
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}

-
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