2

I am using article class. There is no any chapter. It has some sections only.

In my first section there are five subsections. I want the Theorem and Definition numbers as per section wise, like Theorem 1.2.4 or Definition 1.5.2, etc.

In further sections there are no subsections. In the second section since there is no subsection. So here I want like, Theorem 2.1 or Definition 2.4, etc that is without subsection number.

Please suggest the suitable commands.

4
  • I suggest to accept answers to your other questions first, instead of piling up questions and 'exploiting' the knowledge of users here. \usepackage{chngcntr} and \counterwithin{theorem}{section} in the relevant section should do, assuming that the counter is named theorem.
    – user31729
    May 26, 2017 at 13:52
  • Ok, definitely I will do the same. I was not aware of this. May 26, 2017 at 13:56
  • Sir, In the Second section the first Definition is appearing Definition 2.7 instead of Definition 2.1. And onward third section the Definition is right i.e. Definition 3.1. May 26, 2017 at 14:00
  • 1
    Well, unless you show a compilable document there's only guessing around here.It's up to you to provide this MWE
    – user31729
    May 26, 2017 at 14:04

2 Answers 2

3

You can define a new theorem Definition with the counter depending on section and subsection and the output of the counter \theDefinition use \thesection as long as subsection counter is less than 1 and \thesubsection otherwise:

\documentclass{article}

\usepackage{ntheorem}
\usepackage{chngcntr}

\newtheorem{Definition}{Definition}[subsection]% Counter `Definition` depends on `subsection` …
\counterwithin*{Definition}{section}% … and also on `section`.

\renewcommand*{\theDefinition}{% Output of `Definition` counter:
  \ifnum\value{subsection}<1 % if `subsection` counter is less than 1 (mostly `\subsection` not yet used in this `\section`)
    \thesection% show \thesection, e.g., 1
  \else% otherwise
    \thesubsection% show \thesubsection, e.g., 1.1
  \fi
  .\arabic{Definition}% followed by "." and Arabic number of Definition
}

\usepackage{mwe}

\begin{document}
\section{Test section}
\begin{Definition}[Definition on section level]
\blindtext
\end{Definition}
\subsection{Test subsection}
\begin{Definition}[Definition in subsection level]
\blindtext
\end{Definition}

\section{Second test section}
\begin{Definition}[Second theorem on section level]
\blindtext
\end{Definition}
\subsection{Second test subsection}
\begin{Definition}[Second theorem in subsection level]
\blindtext
\end{Definition}

\end{document}

Results in:

result with Definitions on section or subsection level

2

Here is an abstraction of the procedure, very useful if you eventually change your mind about the numbering.

\documentclass{article}
\usepackage{amsthm}
\usepackage{xparse,chngcntr}

\ExplSyntaxOn
\NewDocumentCommand{\newtheorembysubsection}{mom}
 {
  \IfNoValueTF{#2}
   {
    \newtheorem{#1}{#3}[subsection]
    \cs_set:cpn { the#1 } { \thesection. \deepesh_subsection: \arabic{#1} }
    \counterwithin*{#1}{section}
   }
   {
    \newtheorem{#1}[#2]{#3}
   }
 }
\cs_new:Nn \deepesh_subsection:
 {
  \int_compare:nT { \value{subsection}>0 } { \arabic{subsection}. }
 }
\ExplSyntaxOff

\newtheorembysubsection{theorem}{Theorem}

\theoremstyle{definition}
\newtheorembysubsection{definition}[theorem]{Definition} % could be \newtheorem

\begin{document}

\section{Test section}

\begin{theorem}
A theorem, numbered 1.1
\end{theorem}

\begin{definition}
A definition, numbered 1.2
\end{definition}

\subsection{Test subsection}

\begin{definition}
A definition, numbered 1.1.1
\end{definition}

\begin{theorem}
A theorem, numbered 1.1.2
\end{theorem}

\section{Test second section}

\begin{theorem}
A theorem, numbered 2.1
\end{theorem}

\subsection{Test subsection}

\begin{theorem}
A theorem, numbered 2.1.1
\end{theorem}

\end{document}

In case you decide to number theorems by section (as it should be, I find this scheme quite complex for the reader), you just change \newtheorembysubsection into \newtheorem, possibly adding a trailing [section] if needed.

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .