Theorem numbering in starred sections

\documentclass{book}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}[section]

\begin{document}

\chapter{Some title}

\section{Another title}

\begin{theorem}
A nice theorem
\end{theorem}

\section{Yet another title}

\begin{theorem}
Another nice theorem
\end{theorem}

\section*{Optional section}

\begin{theorem}
An optional theorem
\end{theorem}

\end{document}


The resulting theorem numbers will be

Theorem 1.1.1

Theorem 1.2.1

Theorem 1.2.2.

What I would like to achieve instead is

Theorem 1.1.1

Theorem 1.2.1

Theorem 1.E.1.

In other words, I would like the counter to restart (this I think I can do with \setcounter), but I would like there to be a different symbol for the section counter, like an E for "extra". Should I do this at the level of the section, by redefining \thesection? Or by modifying the theorem environment (preferably not)? Should I define a new theorem environment (also preferably not)? I would prefer a systematic solution rather than an ad-hoc approach (where I would need to act separately on each instance of the theorem environment).

EDIT: As Andrew noted in his answer, I mistyped the theorem numbers in my question. I have now corrected them to avoid confusion to other users who may be interested in this question.

• i edited the tags to remove amsmath. theorems are (almost) entirely separate from the math component. i added amsthm since that is what you are using in your mwe. – barbara beeton Dec 5 '18 at 2:15

To get this to work you will need to redefine \thesection, which is used as part of the counter in your theorem environment, inside the optional sections and then you will have to reset \thesection at the end of such sections. You can presumably do this with some \patchcmd trickery but an more straightforward approach would be to define a new environment for optional sections that would take care of these changes for you. Doing it this way you would write your optional sections as

 \begin{section*}{Optional section title}

\begin{theorem}
An optional theorem
\end{theorem}

\end{section*}


Rearranging your MWE so that we have an optional section between two sections, this will have output:

Note that even though the OP says that the theorem numbers should be 1.1.1, 1.E.2, 1.2.3 if the theorems are to be numbered inside sections then, as above, they will be reset each time the section number changes, giving the numbering 1.1.1, 1.E.1, 1.2.1. If the numbering 1.1.1, 1.E.2, 1.2.3 is what is required then replace the line defining the theorem environment with:

\newtheorem{theorem}{Theorem}%[section]
\numberwithin{theorem}{chapter}% needs \usepackage{amsmath}
\renewcommand\thetheorem{\thesection.\arabic{theorem}}


and remove \setcounter{theorem}{0} from the section* environment.

Here is the complete code:

\documentclass{book}
\usepackage{amsthm}
\newenvironment{section*}[1]{% \begin{section*}{section title}....\end{section*}
\section*{#1}
\renewcommand\thesection{\thechapter.E}
\setcounter{theorem}{0}}{}

\newtheorem{theorem}{Theorem}[section]

\begin{document}

\chapter{Some title}

\section{Another title}

\begin{theorem}
A nice theorem
\end{theorem}

\begin{section*}{Optional section}

\begin{theorem}
An optional theorem
\end{theorem}

\end{section*}

\section{Yet another title}

\begin{theorem}
Another nice theorem
\end{theorem}

\end{document}


Edit As Barbara Beeton suggests in the comments it would be better to give the section* environment an optional argument to override the E in the theorem numbers that appear in the optional sections. This is easily done with:

% \begin{section*}[label]{section title}....\end{section*}
\newenvironment{section*}[2][E]{
\section*{#2}
\renewcommand\thesection{\thechapter.#1}
\setcounter{theorem}{0}}{}


which is used exactly as before if you want the theorems labeled with an E but to change this to an F you would use

\begin{section*}[F]{Optional section title}

\begin{theorem}
An optional theorem
\end{theorem}

\end{section*}

• nice, but on the (not unlikely) assumption that a document will contain more than one section*, how about providing for an optional argument that allows the middle element (the letter) to be specified explicitly at the start of that section. – barbara beeton Dec 5 '18 at 2:12
• @barbarabeeton Good idea. Done! – Andrew Dec 5 '18 at 4:30

With a little tweak on the syntax, where

\section*[F]{Title}


allows to set a different key instead of the default “E”.

\documentclass{book}
\usepackage{amsthm}
\usepackage{xparse}

% save the original meaning of \section
\let\latexsection\section
% by default, sections are numbered with arabic numbers
\newcommand{\defaultsectionkey}{\arabic{section}}
% initialize \sectionkey
\let\sectionkey\defaultsectionkey
% redefine \thesection
\renewcommand{\thesection}{\thechapter.\sectionkey}

% tweak \section
\RenewDocumentCommand{\section}{s}{%
\IfBooleanTF{#1}{\sectionstar}{\sectionnostar}%
}
\NewDocumentCommand{\sectionstar}{O{E}m}{%
% this resets the counters associated to section
% change the meaning of \sectionkey
\renewcommand{\sectionkey}{#1}%
\latexsection*{#2}%
}
\NewDocumentCommand{\sectionnostar}{O{#2}m}{%
% revert \sectionkey to the default
\let\sectionkey\defaultsectionkey
\latexsection[#1]{#2}%
}

\newtheorem{theorem}{Theorem}[section]

\begin{document}

\chapter{Some title}

\section{Another title}

\begin{theorem}
A nice theorem
\end{theorem}

\section{Yet another title}

\begin{theorem}
Another nice theorem
\end{theorem}

\section*{Optional section}

\begin{theorem}
An optional theorem
\end{theorem}

\section*[F]{Another optional section}

\begin{theorem}
An optional theorem
\end{theorem}

\section{Normal section}

\begin{theorem}
A big theorem
\end{theorem}

\end{document}