Get Theorem numbering as per section.subsection.subsubsection.number in Latex

newtheoremnumberingoverleafsectioning

I'm not very familiar to Latex. The numbering of my theorems, definitions, remarks etc. appears like 1.0.0.1 for first definition. I don't want zeroes to appear if there are no subsections or subsubsections. Here is what Have in preamble

\documentclass[12pt]{article}
\usepackage{amsthm,amsmath}
\usepackage{amscd}
\usepackage{hyperref}
\usepackage[all]{xypic}
\usepackage{mathtools}
\usepackage{xcolor}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,mathrsfs}
\usepackage{hyperref}
\usepackage[all]{xypic}
\usepackage{times}
\usepackage{inputenc}
\usepackage{lipsum}
\usepackage{titlesec}
\DeclareUnicodeCharacter{0308}{HERE!HERE!}
\setcounter{MaxMatrixCols}{10}

\newtheorem{lemma}{Lemma}[subsubsection]
\newtheorem{corollary}[lemma]{Corollary}
\newtheorem{fact}[lemma]{Fact}
\newtheorem{theorem}[lemma]{Theorem}
\newtheorem{proposition}[lemma]{Proposition}
\newtheorem{definition}[lemma]{Definition}
\newtheorem{remark}[lemma]{Remark}
\newtheorem{example}[lemma]{Example}
\newtheorem{examples}[lemma]{Examples}
\newtheorem{question}[lemma]{Question}

Please help.

Edit: In my first section (for example), there is no subsection, so I want numbering as 1.1, 1.2 and so on and not as 1.0.0.1, 1.0.0.2 etc. In my second section I have a subsection but no subsubsection, there I want theorem numbering to be like "Theorem 2.1.1" and so on. I want four-digit numbering to start when there is a subsubsection". I hope my question is clear now.

Best Answer

I really would not encourage this way of doing. But quick and dirty you may try

\documentclass[12pt]{article}
\usepackage{amsthm}
\usepackage{mathtools}
\newtheorem{lemma}{Lemma}[subsubsection]
\newcounter{lemmaaux}
\setcounter{lemmaaux}{0}
\begin{document}
\section{first}
\counterwithin{lemma}{section}
\begin{lemma}\addtocounter{lemmaaux}{1}
My nice lemma
\end{lemma}
\subsection{The fist sub}
\counterwithin{lemma}{subsection}\setcounter{lemma}{\thelemmaaux}
\begin{lemma}\addtocounter{lemmaaux}{1}
My nice second lemma
\end{lemma}
\subsubsection{The fist subsub}
\counterwithin{lemma}{subsubsection}\setcounter{lemma}{\thelemmaaux}
\begin{lemma}\addtocounter{lemmaaux}{1}
My new lemma
\end{lemma}
\section{second}
\counterwithin{lemma}{section}\setcounter{lemma}{\thelemmaaux}
\begin{lemma}\addtocounter{lemmaaux}{1}
My nice third lemma
\end{lemma}
\subsection{The second sub}
\counterwithin{lemma}{subsection}\setcounter{lemma}{\thelemmaaux}
\begin{lemma}\addtocounter{lemmaaux}{1}
My nice fourth lemma
\end{lemma}
\end{document} 

enter image description here