我需要在报告中添加很多列表环境,并且我希望列表标题的编号遵循与图形或表格标题相同的结构(例如列表 1.1、列表 1.2、列表 2.1)而不是当前结构,即每添加一个列表,编号就会增加一。
我不确定对于像这样的(看似)简单的问题,MWE 有多么必要,但下面有一个:
\documentclass[]{article}
\usepackage{listings}
\begin{document}
\section{Section 1}
\begin{lstlisting}[caption = CAPTION., captionpos = b]
CODE
\end{lstlisting} % I want this to say Listing 1.1, says Listing 1
\begin{lstlisting}[caption = CAPTION., captionpos = b]
CODE
\end{lstlisting} % I want this to say Listing 1.2, says Listing 2
\section{Section 2}
\begin{lstlisting}[caption = CAPTION., captionpos = b]
CODE
\end{lstlisting} % I want this to say Listing 2.1, says Listing 3
\end{document}
答案1
这是一个简单的解决方案。有关更全面的信息,请参阅列表编号
\documentclass[]{article}
\usepackage{listings}
\AtBeginDocument{%added <<<<<<<<<<<<<<
\counterwithin*{lstlisting}{section}
\renewcommand{\thelstlisting}{\thesection.\arabic{lstlisting}}
}
\begin{document}
\section{Section 1}
\begin{lstlisting}[caption = CAPTION., captionpos = b]
CODE
\end{lstlisting} % I want this to say Listing 1.1, says Listing 1
\begin{lstlisting}[caption = CAPTION., captionpos = b]
CODE
\end{lstlisting} % I want this to say Listing 1.2, says Listing 2
\section{Section 2}
\begin{lstlisting}[caption = CAPTION., captionpos = b]
CODE
\end{lstlisting} % I want this to say Listing 2.1, says Listing 3
\end{document}