# Different numbering scheme per chapter [duplicate]

I would like to change the numbering scheme for my theorems per chapter. That's because I have smaller and bigger chapters. Bigger chapters need subsections for proper structuring, and so my theorems are numbered within subsections. For smaller chapters there won't be any subsections so I would like to have my theorems numbered within sections. (That means one less number).

What I'm trying to accomplish is:

Theorem 1.1.1.1.
Theorem 1.1.1.2.

# Chapter 2.

## Section 2.1.

Theorem 2.1.1.
Theorem 2.1.2.

How can I do that?

MWE: (randomize section titles and theorem body text as you please)

\documentclass{book}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}[subsection]
\begin{document}
\chapter{}
\section{}
\subsection{}
\begin{theorem}\end{theorem}
\chapter{}
\section{}
\begin{theorem}\end{theorem}
\begin{theorem}\end{theorem}
\end{document}


Note: egreg's answer includes a version incorporating the hack he gave me here.

-

## marked as duplicate by Guido, Xavier, Werner, Andrew Swann, Stefan Kottwitz♦Jun 17 '13 at 6:21

It would be great if you could include a minimal working example (MWE) that produces the above output as it currently stands (without the adjustable/relative number of your theorems) for the community to have something to start with. It should start with \documentclass and end with \end{document}. –  Werner Dec 7 '12 at 17:07
What you've added is not a MWE for what you have in mind. In the question of yours that you removed the link to there's much more involved; the problem with the simple MWE could be solved quite easily, but this wouldn't take care of \autoref and friends. –  egreg Dec 7 '12 at 18:55
@egreg And what's the quite easy solution to the reduced problem? (Or is the answer to the duplicate question already the easiest possible?) Also, should I add the link back? –  marczellm Dec 7 '12 at 20:08
I added the simplified version, which is along the same lines. I feel that the old question should be linked again. –  egreg Dec 7 '12 at 20:21
I found a very easy solution which uses the chngcntr package and appropriate \counterwithin commands before the beginning of a chapter. It is not automatic as the answer below but requires fewer and clearer code. Do you think this has drawbacks? –  marczellm Dec 8 '12 at 14:34

If all theorem-like environments share this numbering system, then the following should work. Each \snewtheorem declarations defines two theorem-like environments and another environment; the "subsection" related theorem-like environment is chosen if the counter value \subsection has a positive value. So all theorems should be stated under a section or a subsection, but this seems what you need.

\documentclass{book}
\usepackage{amsthm,xparse}
\NewDocumentCommand{\snewtheorem}{m o m}
{
\IfNoValueTF{#2}
{
\newtheorem{sec#1}{#3}[section]
\newtheorem{subsec#1}{#3}[subsection]
}
{
\newtheorem{sec#1}[sec#2]{#3}
\newtheorem{subsec#1}[subsec#2]{#3}
}
\newenvironment{#1}
{\ifnum\value{subsection}>0
\csname subsec#1\expandafter\endcsname
\else
\csname sec#1\expandafter\endcsname
\fi}
{\ifnum\value{subsection}>0
\csname endsubsec#1\expandafter\endcsname
\else
\csname endsec#1\expandafter\endcsname
\fi}
}

\snewtheorem{thm}{Theorem}
\snewtheorem{lem}[thm]{Lemma}

\begin{document}
\mainmatter
\chapter{Long}
\section{First}
\subsection{A}
\begin{lem}[Big]\label{insubsec}
This is a big lemma.
\end{lem}

\begin{thm}[Big]
This is a big theorem.
\end{thm}

\subsection{B}
\begin{lem}
This is a lemma.
\end{lem}

\begin{thm}
This is a theorem.
\end{thm}

\chapter{Short}
\section{First}
\begin{lem}[Big]
This is a big lemma.
\end{lem}

\begin{thm}[Big]\label{insec}
This is a big theorem.
\end{thm}

\section{Second}
\begin{lem}
This is a theorem.
\end{lem}

\begin{thm}
This is a theorem.
\end{thm}

\ref{insubsec}

\ref{insec}

\end{document}


An extension of the \xnewtheorem I defined time ago for a question you posed about supporting hyperref and \autoref can be like this.

\documentclass{book}

\usepackage{amsthm,xparse,aliascnt,hyperref,bookmark}

\NewDocumentCommand{\xnewtheorem}{m o m}
{%
\IfNoValueTF{#2}
{%
\newtheorem{sec#1}{#3}[section]%
\newtheorem{subsec#1}{#3}[subsection]%
}
{%
\newaliascnt{sec#1}{sec#2}%
\newtheorem{sec#1}[sec#1]{#3}%
\aliascntresetthe{sec#1}%
\newaliascnt{subsec#1}{subsec#2}%
\newtheorem{subsec#1}[subsec#1]{#3}%
\aliascntresetthe{subsec#1}%
}
\expandafter\newcommand\csname sec#1autorefname\endcsname{#3}%
\expandafter\newcommand\csname subsec#1autorefname\endcsname{#3}%
\newenvironment{#1}
{\ifnum\value{subsection}>0
\csname subsec#1\expandafter\endcsname
\else
\csname sec#1\expandafter\endcsname
\fi}
{\ifnum\value{subsection}>0
\csname endsubsec#1\expandafter\endcsname
\else
\csname endsec#1\expandafter\endcsname
\fi}
}

\xnewtheorem{thm}{Theorem}
\xnewtheorem{lem}[thm]{Lemma}

\begin{document}
\mainmatter
\chapter{Long}
\section{First}
\subsection{A}
\begin{lem}[Big]\label{insubsec}
This is a big lemma.
\end{lem}

\begin{thm}[Big]
This is a big theorem.
\end{thm}

\subsection{B}
\begin{lem}
This is a lemma.
\end{lem}

\begin{thm}
This is a theorem.
\end{thm}

\chapter{Short}
\section{First}
\begin{lem}[Big]
This is a big lemma.
\end{lem}

\begin{thm}[Big]\label{insec}
This is a big theorem.
\end{thm}

\section{Second}
\begin{lem}
This is a theorem.
\end{lem}

\begin{thm}
This is a theorem.
\end{thm}

\autoref{insubsec}

\autoref{insec}

\end{document}


-