I'm trying to enumerate the different theorem-like counters within each sections independently. I'm using the code I found on this link. However, theorems, lemmas, and definitions are not numbered in themselves, they are numbered consecutively.
My MWE is like that;
\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}
and it looks like this.
How can I fix this numbering sequence?


theorem, you have to do the same withdefinitionandlemma– DG' May 12 '23 at 09:39theoremcounter fordefinitionandlemma. So you should remove the optional argument[theorem]from the definitions ofdefinitionandlemma. – cabohah May 12 '23 at 09:56[theorem]fromdefinitionandlemma, the section and subsection numbers are also removed, and they do not appear next to the names. Morever, the explanations in the links you gave are not related to numbering. – Bluerose May 12 '23 at 11:10\newcommand{\definitionprefix}{} \let\thedefinitionsaved\thedefinition \renewcommand{\thedefinition}{\definitionprefix\thedefinitionsaved} \let\sectionsaved\section \patchcmd{\@startsection}{\par}{\renewcommand{\definitionprefix}{\csname the#1\endcsname.}}{}{}this code fordefinitionbeforemakeatother, nothing changed – Bluerose May 12 '23 at 11:17