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.

How do I create two lstlisting environments that each have its own counter?

If I use for example

\lstnewenvironment{algorithm}[2]{
    \renewcommand\lstlistingname{Algorithm}
    \lstset{ ... }
} {}

\lstnewenvironment{program}[2]{
    \renewcommand\lstlistingname{Program}
    \lstset{ ... }
} {}

And then

\begin{algorithm}{Algorithm caption}{alg-label}
...
\end{algorithm}

\begin{program}{Program caption}{prg-label}
...
\end{program}

Then they will share the counter, i.e. it will result for example in

Algorithm 1.1
    ...
Program 1.2
    ...

I would like the counting to be independent for different listing environments.

I also use the caption package to create a nice caption. I've already tried many things but none really successfuly. The only way that I found that indicates how to change the counter/file extension is via i.e. \DeclareCaptionType[fileext=alg]{algorithm} but the problem is that this command already defines a new environment so I don't know how to use it together with a new listings environment and the caption package. I'm using for example the following settings:

\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox[cmyk]{0.43, 0.35, 0.35,0.01}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}
share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

A solution in the spirit of Elenaher's comment:

\newcounter{algorithm}
\lstnewenvironment{algorithm}[2]{
    \renewcommand\lstlistingname{Algorithm}
    \setcounter{lstlisting}{\value{algorithm}}
    \lstset{ ... }
} {\addtocounter{algorithm}{1}}

\newcounter{program}
\lstnewenvironment{program}[2]{
    \renewcommand\lstlistingname{Program}
    \setcounter{lstlisting}{\value{program}}
    \lstset{ ... }
} {\addtocounter{program}{1}}

Then the following:

\begin{algorithm}{Algorithm caption}{alg-label}
...
\end{algorithm}

\begin{algorithm}{Another algorithm caption}{another-alg-label}
...
\end{algorithm}

\begin{program}{Program caption}{prg-label}
...
\end{program}

results in:

Algorithm 1.1
    ...
Algorithm 1.2
    ...
Program 1.1
    ...
share|improve this answer
    
+1 : good solution using counters ! –  Elenaher Oct 13 '10 at 13:44
add comment

Maybe you should try to invoke manually the command setcounter to change the counter of lstlisting like that :

\setcounter{lstlisting}{value}

Thus, you will be able to re-initialize the counter for each new environments.

Then you could do more complicated things like automating the invocation of setcounter with the values of the label.

share|improve this answer
    
You are right,thanks! I looked at this and related commands before but I didn't see a clean way to use them. Well, I guess that letting all new environments share the same counter and changing it is not very clean, more of a hack, but then again, I don't know that much about LaTeX. I'll post the solution as a separate answer. –  fraktalek Oct 13 '10 at 12:26
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.