# zref-perpage doesn't work with theorem-like environments

I am trying to use the zref-perpage package to number theorem-like enviroments and equations by page. Unfortunately, it doesn't work well. In this first example, we see that the second thm appears on the 2nd page, but its counter is 1.2 instead of 2.1. The equation numbers come out OK.

\documentclass{amsart}
\usepackage[textheight=1in]{geometry}
\usepackage{zref-perpage}

\newtheorem{thm}{Theorem}
\zmakeperpage{thm}

\renewcommand{\thethm}{\thezpage.\arabic{thm}}
\zmakeperpage{equation}
\renewcommand{\theequation}{\thezpage.\arabic{equation}}

\begin{document}

\begin{thm}
We have
$$\int_1^e\frac1x = 1$$
\end{thm}

\begin{thm}
We have
$$\int_1^e\frac1x = 1$$

$$\int_1^e\frac1x = 1$$
\end{thm}

\end{document}


In the second example, the only change I make is I increase the textheight from 1 in to 1.5 in. This makes the second thm begin on the first page but finishes on the second page. As you can see, this time the equation counter gets messed up: the second equation is on the 2nd page, but it gets the label 1.3, so as if it was on page 1.

\documentclass{amsart}
\usepackage[textheight=1.5in]{geometry}
\usepackage{zref-perpage}

\newtheorem{thm}{Theorem}
\zmakeperpage{thm}

\renewcommand{\thethm}{\thezpage.\arabic{thm}}
\zmakeperpage{equation}
\renewcommand{\theequation}{\thezpage.\arabic{equation}}

\begin{document}

\begin{thm}
We have
$$\int_1^e\frac1x = 1$$
\end{thm}

\begin{thm}
We have
$$\int_1^e\frac1x = 1$$

$$\int_1^e\frac1x = 1$$
\end{thm}

\end{document}


At the suggestion of @christian, in the following example I only use article and, with textheight=.9in, the second thm is on the 2nd page, but the thm counter shows 1.2 instead of 2.1.

\documentclass[letterpaper]{article}
%\usepackage{mdframed}
\usepackage[textheight=.9in]{geometry}
\usepackage{zref-perpage}

\newtheorem{thm}{Theorem}
\zmakeperpage{thm}

\renewcommand{\thethm}{\thezpage.\arabic{thm}}
\zmakeperpage{equation}
\renewcommand{\theequation}{\thezpage.\arabic{equation}}

\begin{document}

\begin{thm}
We have
$$\int_1^e\frac1x = 1$$
\end{thm}

\begin{thm}
We have
$$\int_1^e\frac1x = 1$$

$$\int_1^e\frac1x = 1$$
\end{thm}

\end{document}


This example shows that the perpage package doesn't work either.

\documentclass[letterpaper]{article}

\usepackage[textheight=.9in]{geometry}
\usepackage{perpage}

\newtheorem{thm}{Theorem}
\let\origthethm\thethm
\MakePerPage{thm}
\renewcommand{\thethm}{\theperpage.\origthethm}

\let\origtheequation\theequation
\MakePerPage{equation}
\renewcommand{\theequation}{\theperpage.\origtheequation}

\begin{document}

\begin{thm}
We have
$$\int_1^e\frac1x = 1$$
\end{thm}

\begin{thm}
We have
$$\int_1^e\frac1x = 1$$

$$\int_1^e\frac1x = 1$$
\end{thm}

\end{document}

• Using the perpage package works (as in the question linked by @barbarabeeton). I don't think this is related to the thm environment at all – user31729 Jan 24 '17 at 20:12
• @christian and @barbarabeeton : Thanks guys a lot. My problem is that, in fact, I am also using the mdframed package and perpage has a conflict with it. I think the reason for the conflict is that mdframed loads the zref-abspage package. I would show here an example, but I do not know how to do this in this comment box. What can I do? No doubt, I have to abandon zref since it has other problems: it resets the equation numbers when the align environment is used. – Máté Wierdl Jan 25 '17 at 3:50
• @christian , @barbara : it doesn't work even if I just use article. I'll show in a minute in the original post. – Máté Wierdl Jan 25 '17 at 4:12
• @ChristianHupfer , @BarbaraBeeton : I added the fourth example which shows that perpage doesn't work either: the second thm gets 1.2 though it's on the second page. The equations are fine. I am now using only article. – Máté Wierdl Jan 25 '17 at 4:43

Environment equation, redefined by package amsmath, increments the equation counter before the math is started. Thus, it happens that there is a page break between the counter setting and the displayed math equation. The former is the place, where the label is set to get the page number.

Workarounds:

• Putting a minipage around to prevent the page break:

\noindent
\begin{minipage}{\linewidth}
$$...$$
\end{minipage}

• Redefinition of \equation to move \incr@eqnum, which calls \refstepcounter{equation}, to a later place:

\makeatletter
\renewcommand*{\equation}{% amsmath version
\nobreak
\mathdisplay@push
\st@rredfalse
\global\@eqnswtrue
\mathdisplay{equation}%
\incr@eqnum
}
\makeatother

• Thank you. I do not understand. The problem is not with the equation counter but the thm counter. So in the first workaround, didn't you mean to put a minipage around thm instead of equation? My last example uses only article, so I guess that doesn't have amsmath version of equation. – Máté Wierdl Jan 26 '17 at 12:05
• Putting a minipage around thm did help, thanks. – Máté Wierdl Jan 26 '17 at 12:10
• I wouldn't mind if a thm could run onto the next page---especially since a minipage may put my thm on top of the page number (as it happens in my actual document a number of times). Isn't there some kind of trick you did with equation to use for thm ? – Máté Wierdl Jan 26 '17 at 12:21
• @MátéWierdl There is no simple trick. Each implementation of a theorem (there are lots of theorem packages) needs to be investigated, where the counter is incremented and if there is a reliable way to move it to a better place if necessary. – Heiko Oberdiek Jan 26 '17 at 17:27