# Customizing a theorem while taking over the counter from another theorem

I defined a theorem within an environment which is numbering like T1.2. for the second theorem in the first section. Now I want to have another theorem which has the same counter as the other theorem but looks like T1.3*.

A minimal example:

\documentclass{scrartcl}

\usepackage{amsthm}
\usepackage{blindtext}

\theoremstyle{definition}

\newenvironment{myenv}{
\newpage
\section{New section}
}{}

\newtheorem{mytheorem}{}[section]
\renewcommand{\themytheorem}{T\thesection.\arabic{mytheorem}}

\newtheorem{starredtheorem}[mytheorem]{}

\begin{document}

\begin{myenv}

\begin{mytheorem}
\blindtext
\end{mytheorem}

\begin{starredtheorem}
\blindtext
\end{starredtheorem}

\begin{mytheorem}
\blindtext
\end{mytheorem}

\end{myenv}

\end{document}


I want something like

\renewcommand{\thestarredtheorem}{T\thesection.\arabic{mytheorem}*}


so the numbering in the minimal example should be

T1.1. ABC
T1.2*. DEF
T1.3. GHI


How can I do this?

• May it happen these environment use the optional argument? – Bernard Apr 9 '20 at 13:44
• Yes. In the real document, the environment has an optional parameter which is shown in the header but not the theorem. – ATW Apr 9 '20 at 16:29

I propose to use etoolbox's \AtBeginEnvironment command in order to redefine \themytheorem in the scope of the starredtheorem environment:

\let\themytheoremNormal=\themytheorem

\AtBeginEnvironment{starredtheorem}{%
\renewcommand*{\themytheorem}{\themytheoremNormal\relax *}%
}


This also works when referencing the theorem, because:

1. The begin code of the starredtheorem environment does \refstepcounter{mytheorem}, which does a \protected@edef\@currentlabel{...} which stores the expansion of \themytheorem at that point in macro \@currentlabel.

2. \label{th:starred} does \protected@write\@auxout{}{\string\newlabel{th:starred}{{\@currentlabel}{\thepage}}}, which writes a \newlabel command to the .aux file containing the expansion of \@currentlabel, i.e., what \refstepcounter{mytheorem} has prepared in step 1 using the special redefinition of \themytheorem (the one that appends a star).

Full code:

\documentclass{article}
\usepackage{amsthm}
\usepackage{etoolbox}
\usepackage{lipsum}

\theoremstyle{definition}

\newenvironment{myenv}
{\newpage\section{New section}}
{}

\newtheorem{mytheorem}{}[section]
\renewcommand{\themytheorem}{T\thesection.\arabic{mytheorem}}
\let\themytheoremNormal=\themytheorem

\newtheorem{starredtheorem}[mytheorem]{}

\AtBeginEnvironment{starredtheorem}{%
\renewcommand*{\themytheorem}{\themytheoremNormal\relax *}%
}

\begin{document}

\begin{myenv}

\begin{mytheorem}\label{th:normal-a}
\lipsum[1][1-3]
\end{mytheorem}

\begin{starredtheorem}\label{th:starred}
\lipsum[1][1-3]
\end{starredtheorem}

\begin{mytheorem}\label{th:normal-b}
\lipsum[1][1-3]
\end{mytheorem}

References to theorems~\ref{th:normal-a}, \ref{th:starred}
and~\ref{th:normal-b}.

\end{myenv}

\end{document}


# Discussion of the \relax

The \relax in the special (“starred”) definition of \themytheorem is not really necessary, it is just safe practice when using a * as text: even if what precedes the star were a command having a starred form, the \relax would prevent said command from interpreting our star as meaning “I want the starred form of the command.” In this case, after expansion of \themytheoremNormal, what precedes the star would be \arabic{mytheorem}, which doesn't behave differently when followed by a star. Thus, the \relax isn't necessary, but it doesn't hurt either.

The kind of problem this \relax is preventing could occur if, for instance, one were to use (silly-looking example, but it can be tested):

\renewcommand{\themytheorem}{%
T\thesection.\arabic{mytheorem}\protect\\%
}


In this case, though, the best practice is to prevent the error at the call site. In this example, this is where \\ is used, since it is the command that will look ahead and behave differently if there is a star coming along. The best fix in this case would be to use:

\renewcommand{\themytheorem}{%
T\thesection.\arabic{mytheorem}\protect\\\relax
}


in order to make sure that the starred form of \\ isn't used. This way, even if someone uses a * not preceded by \relax afterwards, the call to \\ will work as a \\ and not as a \\* (which would start a new line, prevent a page break but wouldn't print a star).

• I've added a bit more programming advice regarding the potential problem with the star (which doesn't affect the code in the answer, don't worry: this is only general advice regarding the special status of * in LaTeX). – frougon Apr 9 '20 at 12:15