# Page counter doesn't work in equation on top of a new page

In the example below (sorry, I don't know how to make it smaller), I try to include the page number in the equation numbering, but on top of a new page, the equation gets numbered by the previous page not by the page it is actually on. I indicate where the problem occurs with a sentence that references the incorrectly numbered equation. As you can see, on top of page 3, all 4 equations in the align environment get numbered by the previous page.

I see the same problem with any theorem-like environment.

Motivation: it is much easier to find an equation (or any theoremlike structure) in a large article or book if the reference indicates the page number instead of the section or anything else.)

\documentclass[12pt,letterpaper]{amsart}
\numberwithin{equation}{page}
\begin{document}
$$\frac12=\frac24\label{eq:1}$$
$$\frac12=\frac24\label{eq:2}$$
$$\frac12=\frac24\label{eq:3}$$
$$\frac12=\frac24\label{eq:4}$$
$$\frac12=\frac24\label{eq:5}$$
$$\frac12=\frac24\label{eq:6}$$
$$\frac12=\frac24\label{eq:7}$$
$$\frac12=\frac24\label{eq:8}$$
$$\frac12=\frac24\label{eq:9}$$
$$\frac12=\frac24\label{eq:10}$$
$$\frac12=\frac24\label{eq:11}$$
$$\frac12=\frac24\label{eq:12}$$
$$\frac12=\frac24\label{eq:13}$$
$$\frac12=\frac24\label{eq:14}$$
$$\frac12=\frac24\label{eq:15}$$
$$\frac12=\frac24\label{eq:16}$$
$$\frac12=\frac24\label{eq:17}$$
Equation \eqref{eq:17} is on page \pageref{eq:17}, not on the
previous page.
$$\frac12=\frac24\label{eq:18}$$
$$\frac12=\frac24\label{eq:19}$$
$$\frac12=\frac24\label{eq:20}$$
$$\frac12=\frac24\label{eq:21}$$
$$\frac12=\frac24\label{eq:22}$$
$$\frac12=\frac24\label{eq:23}$$
$$\frac12=\frac24\label{eq:24}$$
$$\frac12=\frac24\label{eq:25}$$
$$\frac12=\frac24\label{eq:26}$$
$$\frac12=\frac24\label{eq:27}$$
$$\frac12=\frac24\label{eq:28}$$
$$\frac12=\frac24\label{eq:29}$$
$$\frac12=\frac24\label{eq:30}$$
$$\frac12=\frac24\label{eq:31}$$
$$\frac12=\frac24\label{eq:32}$$
\begin{align}
\frac12&=\frac24\label{eq:33} \\
\frac12&=\frac24\\
\frac12&=\frac24\\
\frac12&=\frac24
\end{align}
Equation \eqref{eq:33} is on page \pageref{eq:33}, not on the previous
page.
$$\frac12=\frac24\label{eq:34}$$
$$\frac12=\frac24\label{eq:35}$$
$$\frac12=\frac24\label{eq:36}$$

\end{document}

Added (thanks!!!): The perpage package worked perfectly. One remark: it requires an extra run of latex. I could even use it for theoremlike environments. Here is what I needed to do to have all theorems be numbered by page. Note that the counter redefinition had to be done after the definition of the thm environment since the counter thm counter doesn't exist before.

\mdtheorem[style=theorem]{thm}{Theorem}
\let\origthethm\thethm
\MakePerPage{thm}
\renewcommand{\thethm}{\theperpage.\origthethm}
• The page counter is increased at shipout of the page, i.e. at the end, not at the beginning of a page – user31729 Jan 20 '17 at 19:52
• I hope the \label{eq:1} system is only used to track the equations here in demonstration, not in your real document – user31729 Jan 20 '17 at 20:08
• you can never refer to the page counter in the body of the document as page breaking happens later after all macros have been expanded, you always need to refer to it indirectly eg via \pageref – David Carlisle Jan 20 '17 at 20:26
• Basically any real setup of a sophisticated document with LateX requires two or more runs. – user31729 Jan 22 '17 at 18:33

Use the perpage package, which is designed for such per - page activities. The correct page value can be retrieved with the \theperpage macro.

\documentclass[12pt,letterpaper]{amsart}

\let\origtheequation\theequation

\usepackage{perpage}

\MakePerPage{equation}

\renewcommand{\theequation}{\theperpage.\origtheequation}

\begin{document}
$$\frac12=\frac24\label{eq:1}$$
$$\frac12=\frac24\label{eq:2}$$
$$\frac12=\frac24\label{eq:3}$$
$$\frac12=\frac24\label{eq:4}$$
$$\frac12=\frac24\label{eq:5}$$
$$\frac12=\frac24\label{eq:6}$$
$$\frac12=\frac24\label{eq:7}$$
$$\frac12=\frac24\label{eq:8}$$
$$\frac12=\frac24\label{eq:9}$$
$$\frac12=\frac24\label{eq:10}$$
$$\frac12=\frac24\label{eq:11}$$
$$\frac12=\frac24\label{eq:12}$$
$$\frac12=\frac24\label{eq:13}$$
$$\frac12=\frac24\label{eq:14}$$
$$\frac12=\frac24\label{eq:15}$$
$$\frac12=\frac24\label{eq:16}$$
$$\frac12=\frac24\label{eq:17}$$
Equation \eqref{eq:17} is on page \pageref{eq:17}, not on the
previous page.
$$\frac12=\frac24\label{eq:18}$$
$$\frac12=\frac24\label{eq:19}$$
$$\frac12=\frac24\label{eq:20}$$
$$\frac12=\frac24\label{eq:21}$$
$$\frac12=\frac24\label{eq:22}$$
$$\frac12=\frac24\label{eq:23}$$
$$\frac12=\frac24\label{eq:24}$$
$$\frac12=\frac24\label{eq:25}$$
$$\frac12=\frac24\label{eq:26}$$
$$\frac12=\frac24\label{eq:27}$$
$$\frac12=\frac24\label{eq:28}$$
$$\frac12=\frac24\label{eq:29}$$
$$\frac12=\frac24\label{eq:30}$$
$$\frac12=\frac24\label{eq:31}$$
$$\frac12=\frac24\label{eq:32}$$
\begin{align}
\frac12&=\frac24\label{eq:33} \\
\frac12&=\frac24\\
\frac12&=\frac24\\
\frac12&=\frac24
\end{align}
Equation \eqref{eq:33} is on page \pageref{eq:33}, not on the previous
page.
$$\frac12=\frac24\label{eq:34}$$
$$\frac12=\frac24\label{eq:35}$$
$$\frac12=\frac24\label{eq:36}$$

\end{document}

• example would be more compelling if the visual showed two pages, with the xref on the first page to an equation on the second. (the code is solid, but if someone just looks at the present visual, it looks "manufactured".) maybe use geometry to set a shorter page. – barbara beeton Jan 20 '17 at 21:18

You can use the zref package, in particular its perpage module.

Note the usage of \thezpage for getting the correct page number, which is not the same as \thepage in some circumstances: the problem is that TeX typesets things further before realizing it has to eject a page; this is done in order to find the optimal page break point according to its rules.

\documentclass[12pt]{amsart}

\usepackage{zref-perpage}

\usepackage[a6paper]{geometry} % just for the example

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

\begin{document}
$$\frac12=\frac24\label{eq:1}$$
$$\frac12=\frac24\label{eq:2}$$
$$\frac12=\frac24\label{eq:3}$$
$$\frac12=\frac24\label{eq:4}$$
$$\frac12=\frac24\label{eq:5}$$
$$\frac12=\frac24\label{eq:6}$$
$$\frac12=\frac24\label{eq:7}$$
$$\frac12=\frac24\label{eq:8}$$
$$\frac12=\frac24\label{eq:9}$$
$$\frac12=\frac24\label{eq:10}$$
$$\frac12=\frac24\label{eq:11}$$
$$\frac12=\frac24\label{eq:12}$$
$$\frac12=\frac24\label{eq:13}$$
$$\frac12=\frac24\label{eq:14}$$
$$\frac12=\frac24\label{eq:15}$$
$$\frac12=\frac24\label{eq:16}$$
$$\frac12=\frac24\label{eq:17}$$
$$\frac12=\frac24\label{eq:18}$$
$$\frac12=\frac24\label{eq:19}$$
$$\frac12=\frac24\label{eq:20}$$
$$\frac12=\frac24\label{eq:21}$$
$$\frac12=\frac24\label{eq:22}$$
$$\frac12=\frac24\label{eq:23}$$
$$\frac12=\frac24\label{eq:24}$$
$$\frac12=\frac24\label{eq:25}$$
$$\frac12=\frac24\label{eq:26}$$
$$\frac12=\frac24\label{eq:27}$$
$$\frac12=\frac24\label{eq:28}$$
$$\frac12=\frac24\label{eq:29}$$
$$\frac12=\frac24\label{eq:30}$$
\end{document}

The geometry package has been used just for reducing the amount of code and for showing several pages in a small picture.