LaTeX Lists: Different Counters for Different Accounts

How to create two lstlisting environments, each of which has 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, that is, the result will look, for example, in

Algorithm 1.1
    ...
Program 1.2
    ...

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

I also use the caption package to create a good caption. I have tried a lot, but have not achieved anything. The only way I found that indicates how to change the counter / file extension is \ DeclareCaptionType [fileext = alg] {algorithm}, but the problem is that this command already defines the new environment, so I don’t know how to use this along with a new list environment and subtitle package. I use, 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}}
+3
1
\newcounter{algorithm}
\newcounter{program}

\makeatletter
\lstnewenvironment{algorithm}[2]{
  \renewcommand\lstlistingname{Algorithm}
  \let\c@lstlisting=\c@algorithm
  \let\thelstlisting=\thealgorithm
  \lstset{caption=#1}
} {}

\lstnewenvironment{program}[2]{
  \renewcommand\lstlistingname{Program}
  \let\c@lstlisting=\c@program
  \let\thelstlisting=\theprogram
  \lstset{caption=#1}
} {}
\makeatother
+1

Source: https://habr.com/ru/post/1768858/


All Articles