LaTeX列表:不同的列表环境下的不同计数器

LaTeX列表:不同的列表环境下的不同计数器,latex,listings,caption,Latex,Listings,Caption,如何创建两个都有自己计数器的lstlisting环境 例如,如果我使用 \lstnewenvironment{algorithm}[2]{ \renewcommand\lstlistingname{Algorithm} \lstset{ ... } } {} \lstnewenvironment{program}[2]{ \renewcommand\lstlistingname{Program} \lstset{ ... } } {} 然后 \begin{al

如何创建两个都有自己计数器的lstlisting环境

例如,如果我使用

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

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

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

\begin{program}{Program caption}{prg-label}
...
\end{program}
然后他们将共享计数器,例如,它将导致

Algorithm 1.1
    ...
Program 1.2
    ...
我希望计数是独立的不同的上市环境

我还使用标题包创建了一个漂亮的标题。我已经尝试过很多事情,但没有一件真正成功。我发现指示如何更改计数器/文件扩展名的唯一方法是通过I.e.\declarecoptiontype[fileext=alg]{algorithm}但是问题是,这个命令已经定义了一个新的环境,所以我不知道如何将它与新的列表环境和标题包一起使用。例如,我使用以下设置:

\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}}

你可能会更幸运地问你关于好主意的问题,我不知道这个,谢谢!
\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