chngcntr 包的令人困惑的用法

chngcntr 包的令人困惑的用法

我不完全理解它chngcntr的工作原理。我附上了一个示例,希望能解释我的困惑,当然还有源代码。

1.1, 1.2,...我想要的是将第一部分编号为,将第二部分编号为2.1.1, 2.1.2, 2.2.1, 2.2.2,...,然后将第三部分编号回到3.1, 3.2, ...

\documentclass[]{article}
\usepackage{chngcntr}
\newtheorem{thm}{Theorem}[section]
\begin{document}

\section{Labels 1.1, 1.2, ...}
\begin{thm} Theorem 1 \end{thm}
\subsection{New Subsection \textit{Does Not} Change Counter}
\begin{thm} Theorem 2 \end{thm}
\subsection{And Again}
\begin{thm} Theorem 3 \end{thm}

------------------------------

\section{Labels 1.1.1, 1.1.2, 1.2.1, ...}
\counterwithin{thm}{subsection}
\subsection{Subsection}
\begin{thm} Theorem 1 \end{thm}
\begin{thm} Theorem 2 \end{thm}
\subsection{New Subsection \textit{Does} Change Counter}
\begin{thm} Theorem 3 \end{thm}
\begin{thm} Theorem 4 \end{thm}

------------------------------

\section{Now I Try to Go Back to the First Case...}
\counterwithin{thm}{section}
\subsection{Subsection 1}
\begin{thm} Theorem 1 \end{thm}
\subsection{Subsection 2 -- Counter Is Reset to the Start}
\begin{thm} Theorem 2 \end{thm}

\end{document}

展示

答案1

除了不推荐的编号方案之外……

\newtheorem{thm}{Theorem}[section]使thm计数器通过 进行重置section

现在,也\counterwithin{thm}{subsection}将其设置thm在重置列表中subsection。只要\subsection使用,这就可以了。

切换回

\counterwithin{thm}{section}

稍后是不够的,因为thm仍在 的重置列表中subsection,因此任何未加星号的\subsection命令都会重置thm计数器。这是错误的,并且不是要求的,因此thm必须先将计数器从重置列表中删除:

\counterwithout{thm}{subsection}
\counterwithin{thm}{section}

恢复编号并重新设置。

\documentclass[]{article}
\usepackage{chngcntr}
\newtheorem{thm}{Theorem}[section]

\begin{document}

\section{Labels 1.1, 1.2, ...}
\begin{thm} Theorem 1 \end{thm}
\subsection{New Subsection \textit{Does Not} Change Counter}
\begin{thm} Theorem 2 \end{thm}
\subsection{And Again}
\begin{thm} Theorem 3 \end{thm}

------------------------------

\section{Labels 1.1.1, 1.1.2, 1.2.1, ...}
\counterwithin{thm}{subsection}
\subsection{Subsection}
\begin{thm} Theorem 1 \end{thm}
\begin{thm} Theorem 2 \end{thm}
\subsection{New Subsection \textit{Does} Change Counter}
\begin{thm} Theorem 3 \end{thm}
\begin{thm} Theorem 4 \end{thm}

------------------------------

\section{Now I Try to Go Back to the First Case...}
\counterwithout{thm}{subsection}
\counterwithin{thm}{section}
\subsection{Subsection 1}
\begin{thm} Theorem 1 \end{thm}
\subsection{Subsection 2 -- Counter Is Reset to the Start}
\begin{thm} Theorem 2 \end{thm}

\end{document}

在此处输入图片描述

相关内容