[Tex/LaTex] Intelligently Numbering “remarks” only when multiple are made

amsthmtheorems

Problem. I am using the amsthm package, and have remarks after theorems.

If there is only one remark between two theorems, the remark should not be numbered.

If there are multiple remarks between two theorems, they should be numbered (with the counter reset with respect to the theorem counter).

I have two environments to handle this, rmk and rmk*, but can I have only one?

(If anyone is curious, I'm typesetting some work by Euler, and he does this with Scholium numbering. But I'm also thinking about doing this with my own personal notes, which is why I ask for a robust single environment solution…)

Minimal Working Example. For what I do now, here's a minimal working example using a workaround. But it's unpleasant when I add more remarks to a theorem to remember to change rmk* to rmk.

\documentclass{article}

\usepackage{amsthm}
\newtheorem{thm}{Theorem}
\theoremstyle{remark}
\newtheorem{rmk}{Remark}[thm]
\newtheorem*{rmk*}{Remark}

\begin{document}
\begin{thm}
Lorem ipsum.
\end{thm}

% it would be really nice if these could just be "rmk" and
% magically unnumbered...
\begin{rmk*} % only one remark made, so don't number it
It is Latin for "I have boring examples".
\end{rmk*}

\begin{thm}
We see that $\pi$ is an interesting number.
\end{thm}

\begin{rmk} % Multiple remarks require numbering
It's the ratio of the circumference to the diameter of a circle.
\end{rmk}
\begin{rmk}
It's also used when I do my taxes.
\end{rmk}
% snip
\end{document}

Best Answer

The following example patches \@thm that is defined by amsthm to remove the number, if there is only one entity.

If the theorem entity has reached number 2, then there are multiple items and a label is written to the .aux file using the theorem name and number as key.

In the next LaTeX run, the existence of this key is checked for the first item and the number is suppressed, if the label exists and there are multiple items.

\documentclass{article}

\usepackage{amsthm}
\newtheorem{thm}{Theorem}
\theoremstyle{remark}
\newtheorem{rmk}{Remark}[thm]

\usepackage{etoolbox}
\usepackage{zref-base}
\makeatletter
\patchcmd\@thm{%
  \refstepcounter{#2}%
  \def\@tempa{\@oparg{\@begintheorem{#3}{\csname the#2\endcsname}}[]}%
}{%
  \refstepcounter{#2}%
  \def\@tempa{\@oparg{\@begintheorem{#3}{\csname the#2\endcsname}}[]}%
  % \in@{,#2,}{,...,}\ifin@...\fi select the theorems for this patch
  \in@{,#2,}{,rmk,}% list of theorems with "intelligent numbering"
  \ifin@
    \ifnum\value{#2}=2 %
      \if@filesw
        \zref@wrapper@immediate{%
          \zref@labelbyprops{IntNumTheo.#2@\csname the#2\endcsname}{}%
        }%
      \fi
    \fi
    \ifnum\value{#2}=1 %
      \stepcounter{#2}%
      \zref@ifrefundefined{IntNumTheo.#2@\csname the#2\endcsname}{%
        \def\@tempa{\@oparg{\@begintheorem{#3}{}}[]}%
      }{}%
      \addtocounter{#2}{-1}%
    \fi
  \fi
}{}{\errmessage{Patching \noexpand\@thm failed}}
\makeatother

\begin{document}
\begin{thm}
Lorem ipsum.
\end{thm}

\begin{rmk} % only one remark made, so don't number it
It is Latin for ``I have boring examples''.
\end{rmk}

\begin{thm}
We see that $\pi$ is an interesting number.
\end{thm}

\begin{rmk} % Multiple remarks require numbering
It's the ratio of the circumference to the diameter of a circle.
\end{rmk}
\begin{rmk}
It's also used when I do my taxes.
\end{rmk}
% snip
\end{document}

Result

Related Question