在每个部分、小节和小节中独立编号定理类计数器

在每个部分、小节和小节中独立编号定理类计数器

我正在尝试独立枚举每个部分内不同的定理计数器。我使用我在此链接但是,定理、引理和定义本身并没有编号,而是连续编号的。

我的 MWE 就是这样的;

\documentclass{article}
\usepackage{amsthm,etoolbox}
\newtheorem{theorem}{Theorem}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{lemma}[theorem]{Lemma}

\makeatletter
\@addtoreset{theorem}{section}% Reset theorem counter with every section
\@addtoreset{theorem}{subsection}
\@addtoreset{theorem}{subsubsection}
\newcommand{\theoremprefix}{}
\let\thetheoremsaved\thetheorem
\renewcommand{\thetheorem}{\theoremprefix\thetheoremsaved}
\let\sectionsaved\section
\patchcmd{\@startsection}{\par}{\renewcommand{\theoremprefix}{\csname the#1\endcsname.}}{}{}
\makeatother

\begin{document}
    
    \section{Section}
    \begin{theorem}
        Th 1
    \end{theorem}
    \begin{definition}
        Df 1
    \end{definition}
    \begin{theorem}
        Th 2
    \end{theorem}
    \begin{definition}
        Df 2
    \end{definition}
    
    \subsection{Subsection}
    \begin{theorem}
        Th 1
    \end{theorem}
    \begin{definition}
        Df 1
    \end{definition}
    \begin{definition}
        Df 2
    \end{definition}

    \begin{lemma}
        Lm 1
    \end{lemma}
    \begin{theorem}
        Th 2
    \end{theorem}

    \subsubsection{SubSubsection}
    \begin{theorem}
        Th 1
    \end{theorem}
    \begin{definition}
        Df 1
    \end{definition}
    \begin{definition}
        Df 2
    \end{definition}
    
    \begin{lemma}
        Lm 1
    \end{lemma}
    \begin{theorem}
        Th 2
    \end{theorem}
    
    
    \section{Two}
    \begin{theorem}
        Th 1
    \end{theorem}
    \begin{lemma}
        Lm 1
    \end{lemma}
    
    
\end{document}

它看起来像这样。

MWE 的输出

我该如何修复这个编号序列?

答案1

我通过正确应用@cabohah的建议找到了答案。这是按我想要的方式工作的MWE。

\documentclass{article}
\usepackage{amsthm,etoolbox}
\newtheorem{theorem}{Theorem}
\newtheorem{definition}{Definition}
\newtheorem{lemma}{Lemma}

\makeatletter
\@addtoreset{theorem}{section}% Reset theorem counter with every section
\@addtoreset{theorem}{subsection}
\@addtoreset{theorem}{subsubsection}

\@addtoreset{definition}{section}
\@addtoreset{definition}{subsection}
\@addtoreset{definition}{subsubsection}

\@addtoreset{lemma}{section}
\@addtoreset{lemma}{subsection}
\@addtoreset{lemma}{subsubsection}

\newcommand{\definitionprefix}{}
\let\thedefinitionsaved\thedefinition
\renewcommand{\thedefinition}{\definitionprefix\thedefinitionsaved}

\newcommand{\lemmaprefix}{}
\let\thelemmasaved\thelemma
\renewcommand{\thelemma}{\lemmaprefix\thelemmasaved}

\newcommand{\theoremprefix}{}
\let\thetheoremsaved\thetheorem
\renewcommand{\thetheorem}{\theoremprefix\thetheoremsaved}

\let\sectionsaved\section
\patchcmd{\@startsection}{\par}{
    \renewcommand{\theoremprefix}{\csname the#1\endcsname.}
    \renewcommand{\definitionprefix}{\csname the#1\endcsname.}
    \renewcommand{\lemmaprefix}{\csname the#1\endcsname.}
    }{}{}

\makeatother

\begin{document}
    
    \section{Section}
    \begin{theorem}
        Th 1
    \end{theorem}
    \begin{definition}
        Df 1
    \end{definition}
    \begin{theorem}
        Th 2
    \end{theorem}
    \begin{definition}
        Df 2
    \end{definition}
    
    \subsection{Subsection}
    \begin{theorem}
        Th 1
    \end{theorem}
    \begin{definition}
        Df 1
    \end{definition}
    \begin{definition}
        Df 2
    \end{definition}

    \begin{lemma}
        Lm 1
    \end{lemma}
    \begin{theorem}
        Th 2
    \end{theorem}

    \subsubsection{SubSubsection}
    \begin{theorem}
        Th 1
    \end{theorem}
    \begin{definition}
        Df 1
    \end{definition}
    \begin{definition}
        Df 2
    \end{definition}
    
    \begin{lemma}
        Lm 1
    \end{lemma}
    \begin{theorem}
        Th 2
    \end{theorem}
    
    
    \section{Two}
    \begin{theorem}
        Th 1
    \end{theorem}
    \begin{lemma}
        Lm 1
    \end{lemma}
    
\end{document}

这是输出: 新 MWE 输出

相关内容