1

I'm using amsthm and thmtools to define custom theorem environments, definition environments, etc. All such environments share the same counter.

Is it possible to vary the numbering format depending on the section depth? I want to achieve the following numbering format:

1 Section

1.1 Theorem

1.2 Definition

1.1 Subsection

1.1.1 Theorem

1.1.2 Lemma

Werner
  • 603,163

2 Answers2

2

i'm not sure if you want something like this:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}
\swapnumbers
\newtheorem{theorem}{Theorem}[section]
\newtheorem{definition}[theorem]{Definition}
\newtheorem{lemma}[theorem]{Lemma}
\usepackage{thmtools}

\usepackage{xpatch}
\makeatletter
\newif\ifsection
\newif\ifsubsection
\preto\section{\sectiontrue\subsectionfalse}
\preto\subsection{\sectionfalse\subsectiontrue}
\patchcmd{\@xsect}% <cmd>
  {\ignorespaces}% <search>
  {\ifsection
    \numberwithin{theorem}{section}
    \else
    \numberwithin{theorem}{subsection}
    \fi
    \setcounter{theorem}{0}\relax\ignorespaces}% <replace>
  {}{}% <success><failure>
\makeatother
\begin{document}

\section{Section}
\begin{theorem}
Th 1
\end{theorem}
\begin{definition}
Df 1
\end{definition}
\subsection{Subsection}
\begin{theorem}
Th 2
\end{theorem}
\begin{lemma}
Lm 1
\end{lemma}
\section{Two}
\begin{theorem}
Th 3
\end{theorem}
\begin{lemma}
Lm 2
\end{lemma}
\subsection{Other}
\begin{theorem}
Th 4
\end{theorem}
\subsection{Other 2}
\begin{definition}
Df 2
\end{definition}
\end{document}

enter image description here

skpblack
  • 8,904
  • 3
  • 31
  • 42
1

You can prefix the theorem counter with a section-specific numbering scheme:

enter image description here

\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}
\subsection{Subsection}
\begin{theorem}
Th 2
\end{theorem}
\begin{lemma}
Lm 1
\end{lemma}
\section{Two}
\begin{theorem}
Th 3
\end{theorem}
\begin{lemma}
Lm 2
\end{lemma}
\subsection{Other}
\begin{theorem}
Th 4
\end{theorem}
\subsection{Other 2}
\begin{definition}
Df 2
\end{definition}
Werner
  • 603,163
  • Thank you. Can you explain how this works? – Joel Croteau Apr 14 '23 at 23:27
  • 1
    @JoelCroteau: \thetheorem represents the theorem number, which I've changed to \theoremprefix\thetheoremsaved (adding \theoremprefix). Then, I update \theoremprefix with every call to a sectional unit using \patchcmd. – Werner Apr 16 '23 at 02:34