# How to number equations and floating materials by page number

I am somewhat intrigued by question Something like \enumerate, but with custom numbers at each \item, which mentions a book where each question is numbered by its page number and the question number.

That question is about manual numbering of list items, but how would one go about making each equation be automatically numbered by its page number + a sequential number for each page. For instance, the first equation on page 50 would be numbered 50-1 and the second 50-2, while the first equation on the following page would be numbered 51-1.

Also, how can one extend this to cover floating material too, i.e., figures and tables. I’d prefer a numbering where each type of material (equation, figure, table) has a separate counter, so the first equation, the first figure and the first table on page 50 would all be numbered 50-1. (One would of course disambiguate between them by referring to ‘Equation 50-1’, ‘Figure 50-1’ and ‘Table 50-1’.)

For anyone wanting to have a go at this problem, here’s a simple example document you can use:

\documentclass{article}

\usepackage{lipsum}

\begin{document}

\def\bodytext{

$$2 + 2 = 4$$

$$e^{\pi i} = -1$$

\begin{figure}[htbp]
First figure.
\caption{Figure caption one.}
\end{figure}

\begin{figure}[htbp]
Second figure.
\caption{Figure caption two.}
\end{figure}

\begin{table}[htbp]
First table.
\caption{Table caption one.}
\end{table}
}

\bodytext
\lipsum
\bodytext
\lipsum
\bodytext

\end{document}


By the way, I’m not sure ‘50-1’ is the best formatting to use. Other alternatives I can think of are (50-1), 50.1, 50(1) and 50a, and I actually think I prefer the last one. Any comments on this? (If so, please post them as comments, not answers.)

-

## 3 Answers

Quoting from the UK TeX FAQ:

[T]he output routine is asynchronous, and (La)TeX will usually process quite a bit of the “next” page before deciding to output any page. As a result, the page counter (known internally in LaTeX as \c@page) is normally only reliable when you’re actually in the output routine.

In other words, don't use the page counter for the task at hand. Instead, use the perpage package.

\documentclass{article}

\usepackage{perpage}
\MakePerPage{equation}
\renewcommand{\theequation}{\theperpage--\arabic{equation}}
\MakePerPage{figure}
\renewcommand{\thefigure}{\theperpage--\arabic{figure}}
\MakePerPage{table}
\renewcommand{\thetable}{\theperpage--\arabic{table}}

\usepackage{lipsum}

\begin{document}

\def\bodytext{

$$2 + 2 = 4$$

$$e^{\pi i} = -1$$

\begin{figure}[htbp]
First figure.
\caption{Figure caption one.}
\end{figure}

\begin{figure}[htbp]
Second figure.
\caption{Figure caption two.}
\end{figure}

\begin{table}[htbp]
First table.
\caption{Table caption one.}
\end{table}
}

\bodytext
\lipsum
\bodytext
\lipsum
\bodytext

\end{document}


Here's an example of what might happen by simply using the page counter (modified from Werner's answer) -- the first equation on page 2 is incorrectly labelled (1--4):

\documentclass{article}
\usepackage{chngcntr}% http://ctan.org/pkg/chngcntr
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\counterwithin*{equation}{page}% equation counter is reset with every page counter reset
\renewcommand{\theequation}{\thepage-\arabic{equation}}% page-equation
\begin{document}
\lipsum[1]
$$f(x)=ax^2+bx+c$$
\lipsum[2]
$$f(x)=ax^2+bx+c$$
\lipsum[3]
$$f(x)=ax^2+bx+c$$
\lipsum[4]

A short paragraph.

Another short paragraph.
$$f(x)=ax^2+bx+c$$
\lipsum[5]
$$f(x)=ax^2+bx+c$$
\lipsum[6]
$$f(x)=ax^2+bx+c$$
\lipsum[7]
$$f(x)=ax^2+bx+c$$
\end{document}

-
You can use page counter if you do it inside \AtBeginShipout, because there you are inside the output routine - see my answer. – Boris Nov 4 '11 at 22:00
Nice! Much more reliable. – Werner Nov 4 '11 at 22:02
The first solution works even if one equation is at the start of the page contrary tpo the secund one. To see that, put one empty line after each \lipsum calls, and two before. – projetmbc Nov 5 '11 at 11:36

Well, you need to redefine your prefix when you ship out the page. Here is how:

\documentclass{article}
\newcommand{\eqprefix}{1-}
\usepackage{atbegshi}
\AtBeginShipout{\gdef\eqprefix{\arabic{page}-}\setcounter{equation}{0}}
\renewcommand{\theequation}{\eqprefix\arabic{equation}}
\begin{document}
$$\label{eq:first} a=b$$
\newpage
$$\label{eq:second} b=c$$

From equations~(\ref{eq:first}) and~(\ref{eq:second})  we get:
$$\label{eq:third} a=c$$

\end{document}

-
This does not work well with hyperref due to setting the equation counter to 0, right? – Werner Nov 4 '11 at 22:22
No, for compatibility with hyperred we need to do slightly more work... – Boris Nov 4 '11 at 22:32

Caveat: The following method is inferior to options provided by (say) the perpage package or at the page shipout routine, as suggested by the other answers.

Here is the basic idea for counters of equations.

Use the chngcntr package to specify a <master> counter for a <slave> using:

\counterwithin*{<slave>}{<master>}


Then you can redefine how the equation counter is printed using

\renewcommand{\theequation}{\thepage-\arabic{equation}}%


Here is a minimal example:

\documentclass{article}
\usepackage{chngcntr}% http://ctan.org/pkg/chngcntr
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\counterwithin*{equation}{page}% equation counter is reset with every page counter reset
\renewcommand{\theequation}{\thepage-\arabic{equation}}% page-equation
\begin{document}
\lipsum[1]
$$f(x)=ax^2+bx+c$$
\lipsum[2]
$$f(x)=ax^2+bx+c$$
\lipsum[3]
$$f(x)=ax^2+bx+c$$
\lipsum[4]
$$f(x)=ax^2+bx+c$$
\lipsum[5]
$$f(x)=ax^2+bx+c$$
\lipsum[6]
$$f(x)=ax^2+bx+c$$
\lipsum[7]
$$f(x)=ax^2+bx+c$$
\end{document}


The same for the figure and table environment:

\counterwithin*{figure}{page}% figure counter is reset with every page counter reset
\renewcommand{\thefigure}{\thepage-\arabic{figure}}%
\counterwithin*{table}{page}% table counter is reset with every page counter reset
\renewcommand{\thetable}{\thepage-\arabic{table}}%


lipsum was added for dummy text.

It is not actually necessary to use the chngcntr package, since LaTeX does provide

\@addtoreset{<slave>}{<master>}


which requires the \makeatletter and \makeatother escaping pair. At this point it may just be personal preference. See the UK TeX FAQ entry on Master and slave counters.

-