自定义子部分计数器

自定义子部分计数器

我正在编写一个解决方案文件,其中包含来自一本书不同章节的练习,因此我希望练习的编号为sectionnumber. subsectionnumber. exercisenumber. 出现此问题的原因是这本书的小节标签很特别:它是数字和字母的组合,因此例如在小节 1.1、1.2 之后,它将按字母顺序开始,例如小节 1.A 和 1.B(我假设是为了说明不重要的材料)。我正在寻找一种方法来自定义计数器,以便小节和练习相应编号。谢谢!


更新:刚刚看到这个帖子,这解决了大部分问题。我仍然希望找到一种更“自动化”的方法来实现这一点,比如使用计数器等。


举个例子,

\documentclass[12pt]{article}
\usepackage[letterpaper, margin=1in]{geometry}
\usepackage{amsmath, amssymb, mathtools, tikz-cd, enumitem, fancyhdr, mleftright, wrapfig, scrextend,mathrsfs,amsthm}

\theoremstyle{definition}
\newtheorem{exercise}{Exercise}[subsection]
\newtheorem{corollary}{Corollary}[exercise]
\newtheorem{lemma}{Lemma}[exercise]
\newtheorem{proposition}{Proposition}[exercise]
\newtheorem{claim}{Claim}[exercise]
\newtheorem{remark}{Remark}[exercise]

\begin{document}

\section{Section 1}

\begin{exercise} % 1.0.1

\end{exercise}

\subsection{Subsection 1.1}

\begin{exercise} % 1.1.1

\end{exercise}

\subsection{Subsection 1.2}

\begin{exercise} % 1.2.1

\end{exercise}

\subsection{Subsection 1.A}

\begin{exercise} % 1.A.1

\end{exercise}

\subsection{Subsection 1.B}

\begin{exercise} % 1.B.1

\end{exercise}

\section{Section 2}

\subsection{Subsection 2.1}

\begin{exercise} % 2.1.1

\end{exercise}

\subsection{Subsection 2.2}


\end{document}

答案1

您可以定义一个命令,更改子节编号样式的重新定义\thesubsection,同时存储前一个编号样式的最后一个值,并可选择恢复该值。在下面的示例中,\subsectionnumbering{<style>}将恢复样式的先前最后一个数字,但星号变体\subsectionnumber*{<style>}始终以 1 开头。

\documentclass[12pt]{article}
\usepackage[letterpaper, margin=1in]{geometry}
\usepackage{amsmath, amssymb, mathtools, tikz-cd, enumitem, fancyhdr, mleftright, wrapfig, scrextend,mathrsfs,amsthm}

\theoremstyle{definition}
\newtheorem{exercise}{Exercise}[subsection]
\newtheorem{corollary}{Corollary}[exercise]
\newtheorem{lemma}{Lemma}[exercise]
\newtheorem{proposition}{Proposition}[exercise]
\newtheorem{claim}{Claim}[exercise]
\newtheorem{remark}{Remark}[exercise]
\errorcontextlines\maxdimen
\makeatletter
\newcommand*{\currentsubsectionnumbering}{arabic}
\NewDocumentCommand{\subsectionnumbering}{sm}{%
  \expandafter\edef\csname lastsubsection\currentsubsectionnumbering number\endcsname
    {\expandafter\the\value{subsection}}%
  \renewcommand*{\thesubsection}{\thesection.\@nameuse{#2}{subsection}}%
  \setcounter{subsection}{0}%
  \IfBooleanF{#1}{% star variant don't set it to the last used number of the numbering style
    \@ifundefined{lastsubsection#2number}{}{%
      \setcounter{subsection}{\@nameuse{lastsubsection#2number}}%
    }%
  }
  \@namedef{currentsubsectionnumbering}{#2}%
}
\makeatother

\begin{document}

\section{Section 1}

\begin{exercise} % 1.0.1

\end{exercise}

\subsection{Subsection 1.1}

\begin{exercise} % 1.1.1

\end{exercise}

\subsection{Subsection 1.2}

\begin{exercise} % 1.2.1

\end{exercise}

\subsectionnumbering{Alph}
\subsection{Subsection 1.A}

\begin{exercise} % 1.A.1

\end{exercise}

\subsection{Subsection 1.B}

\begin{exercise} % 1.B.1

\end{exercise}

\subsectionnumbering{arabic}
\subsection{Subsection 1.3}

\begin{exercise} % 1.1.1

\end{exercise}

\subsectionnumbering{Alph}
\subsection{Subsection 1.C}

\begin{exercise} % 1.C.1

\end{exercise}
\subsectionnumbering{arabic}% should be BEFORE \section

\section{Section 2}

%\subsectionnumbering*{arabic}% alternative: use the star variant to start
                              % again with subsection number 1
\subsection{Subsection 2.1}

\begin{exercise} % 2.1.1

\end{exercise}

\subsectionnumbering*{Alph}% Here we don't want to use the previous upper case
                           % letter value, but want to start new.
\subsection{Subsection 2.A}

\begin{exercise} % 2.A.1

\end{exercise}

\end{document}

在此处输入图片描述

相关内容