[Tex/LaTex] Numbering theorems in a chapter with no sections

numberingtheorems

Say I have an index such as:

1. Chapter
  1.1 Section
  1.2 Section
2. Chapter
3. Chapter

and I number my theorems as

\newtheorem{theorem}{Theorem}[section]

with the last theorem in 1.2 being Theorem 1.2.5. Then the first theorem in chapter two will be 2.0.6.

My first question is: is this really a feature and not a bug? Is there a situation where this is the preferred behavior?

My second question is about how to fix it. I would like the theorems in Chapter 2 to be 2.1, 2.2, etc while keeping the theorems in Chapter 1 to appear as 1.1.1, 1.1.2, etc. I can manually do it various ways (see here) by putting in code in individual chapters. But I shouldn't have to do such a thing, should I? I would really like to have a solution where the format of the numbering is determined entirely in the preamble.

Here is a MWE:

\documentclass[12pt]{report}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}[section]

\begin{document}
 \begin{chapter}{Chapter 1}
  \begin{section}{Secton 1.1}
   \begin{theorem}
    asdf
   \end{theorem}
   \begin{theorem}
    asdf
   \end{theorem}
  \end{section}
 \end{chapter}
 \begin{chapter}{Chapter 2}
  \begin{theorem}
   asdf
  \end{theorem}
 \end{chapter}
\end{document}

Best Answer

The following conditions on the value of section, inserting it into the theorem numbering as needed:

enter image description here

\documentclass[12pt]{report}
\usepackage{amsthm,amsmath}
\newtheorem{theorem}{Theorem}[section]
\numberwithin{theorem}{section}% Reset theorem counter with every section
\numberwithin{theorem}{chapter}% Reset theorem counter with every chapter

\renewcommand{\thetheorem}{%
  \ifnum\value{section}=0 
    \thechapter% "no section"
  \else
    \thesection% at least within a section
  \fi%
  .\arabic{theorem}}

\begin{document}

\chapter{First chapter}
\section{First section}
\begin{theorem}
asdf. See Theorem~\ref{abc} and~\ref{def}.
\end{theorem}
\begin{theorem}\label{abc}
asdf
\end{theorem}
\section{Second section}
\begin{theorem}
asdf
\end{theorem}
\begin{theorem}
asdf
\end{theorem}

\chapter{Second chapter}
\begin{theorem}\label{def}
asdf
\end{theorem}

\end{document}

Note that you have to consider that theorem counters are reset with every \chapter and \section, otherwise you'd have Theorem 2.3 starting off in Chapter 2 in the above example. Also, if you have a theorem inside the "introductory" part of a chapter that eventually will contain a section, then you'll still receive the "reduced" numbering.