# Number theorems as pagenumber.counter

For my thesis I want to number theorem-like environments as (pagenumber).(counter), where counter is a counter shared among all environments which restarts at every page. So for example, on page 86 I would have

Definition 86.1. Aaa

Theorem 86.2. Bbb

Remark 86.3. Ccc

and then, on page 87,

Corollary 87.1. Ddd

Proposition 87.2. Eee

I tried to implement this with amsthm and thmtools by setting \declaretheorem[numberwithin=page]{theorem}, and defining all other theorem environments as siblings of theorem. When doing so, I noticed that sometimes the counters are wrong, especially when theorems appear close to the top of a page. Consider the following MWE:

\documentclass{memoir}

\usepackage[height=5cm]{geometry}

\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{lipsum}

\declaretheorem[numberwithin=page]{theorem}

\begin{document}
\begin{theorem}
Aaa
\end{theorem}

\lipsum[1]

\begin{theorem}
Bbb
\end{theorem}

\begin{theorem}
Ccc
\end{theorem}

\lipsum[1]

aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
aa aa aa aa aa aa

\begin{theorem}
Ddd
\end{theorem}
\end{document}


Here, the theorems on page 1 are numbered correctly, but those on pages 2 and 3 are not (1.3 and 3.0 should be 2.1 and 3.1, respectively). The problem disappears when theorems appear further down the page.

This question was already asked here, but the solution there seems to be incompatible with amsthm.

Any idea how to fix this?

• Welcome to tex.sx. Actual output of pages, and thus numbering, is asynchronous, so it will be necessary to take that into consideration, and also to run the job multiple times to get everything into synch. (Other participants in this forum are better equipped than I to provide workable code.) May 27 at 14:59

I'm not sure this is a sound style, but the thesis is yours.

My solution involves perpage and needspace.

1. The representation \thetheorem is \pageref{theorem@\theglobaltheorem}.\arabic{theorem}. Using \pageref is needed because the page number cannot be known at any time during compilation and only the reference gathered in the previous run is safe (you'll be warned if the references have changed).

2. The counter globaltheorem is incremented after a suitable \label is emitted; I use postheadhook, but preheadhook might be used as well.

3. The theorem counter is declared to be “per page”.

4. There is a slight complication, because the counter is incremented before the theorem is actually typeset, so I use preheadhook to ensure that at least one line is available, in order to overcome this problem. You can check that if \needspace{\baselineskip} is omitted, the remark on page 2 would be numbered 2.3 instead of the expected 2.1.

\documentclass{memoir}

\usepackage[a6paper]{geometry}

\usepackage{perpage}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{needspace}

\declaretheorem[
name=Theorem,
]{theorem}

\MakePerPage{theorem}
\newcounter{globaltheorem}
\renewcommand{\thetheorem}{\pageref{theorem@\theglobaltheorem}.\arabic{theorem}}

\declaretheorem[
style=definition,
sibling=theorem,
name=Definition,
]{definition}
\declaretheorem[
style=definition,
sibling=theorem,
name=Remark,
]{remark}

\begin{document}

\begin{theorem}
Aaa
\end{theorem}

\begin{definition}
Bbb
\end{definition}

\vspace{20\baselineskip}

\begin{remark}
Ccc
\end{remark}

\begin{theorem}
Ddd
\end{theorem}

\end{document}


• Great solution, thank you very much for this! As a minor suggestion, when using hyperref, replace \pageref with \pageref* (globaltheorem is only used to give every theorem a unique label, not to be clickable). That way counters in the theorem headings are not hyperlinked and boxes around references are not "split." Also: Any particular reason why you check for 2\baselineskip before a remark, as opposed to (1)\baselineskip for the other environments? Thanks again! May 28 at 7:10
• @user271265 The 2 has remained from a previous attempt. Yes, with hyperref you need \pageref*. May 28 at 7:46