匿名列表环境

匿名列表环境

我希望能够使用星号 (*) 单独关闭列表计数器,就像使用

\begin{equation*}...\end{equation*}

与方程式环境一样,星号不应该增加列表计数器,即

\begin{lstlisting}This will become Listing 1\end{lstlisting}
\begin{lstlisting*}This one is anonymous\end{lstlisting*}
\begin{lstlisting}This is Listing 2\end{lstlisting}

如何做到这一点?

答案1

添加label=...而不caption增加lstlisting计数器!

caption要么在相关调用中不使用 any lstlisting,要么定义一个新的lstlisting*环境,\lstnewenvironment使环境在源代码中更加突出地未被编号,但无论如何这个选择都不安全caption=...

\documentclass{article}

\usepackage{listings}

% This is not needed actually.     
\lstnewenvironment{lstlisting*}[1][]{%
 \lstset{#1}%
}{}

\usepackage{hyperref}

\begin{document}

\section{foo}

\section{Foo stuff}
\thelstlisting
\begin{lstlisting}[label={lst:first-one}] % Will increment the listings counter
Foo
\end{lstlisting}

\autoref{lst:first-one} % This prints 'Listing 2' since the counter is taken from the 2nd section (Foo Stuff), not from the first lstlisting environment. The output of `\thelstlisting` is still 0!

\begin{lstlisting*}[language={C}]
#include<stdio.h>

int main(int argc,char **argv)
{
  printf("Hello World!\n");
  return(0);
}
\end{lstlisting*}
\begin{lstlisting}[caption={Foo caption},label={lst:second-one}] % Will increment the listings counter
Foo
\end{lstlisting}

\thelstlisting

\autoref{lst:second-one}

\ref{lst:second-one}

\end{document}

在此处输入图片描述

相关内容