【问题标题】:LaTeX listings: different counters for different listing environmentsLaTeX 列表:不同列表环境的不同计数器
【发布时间】:2010-10-10 15:13:21
【问题描述】:

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

如果我使用例如

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

我希望计数对于不同的列表环境是独立的。

我还使用标题包来创建一个漂亮的标题。我已经尝试了很多东西,但没有一个真正成功。我发现指示如何更改计数器/文件扩展名的唯一方法是通过即 \DeclareCaptionType[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}}

【问题讨论】:

标签: latex listings caption


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    相关资源
    最近更新 更多