3

Is there a way to count the number of equations (or other counted items) in a page and display it in the header? I am having trouble with keeping correct count when the page breaks and I'm not using \newpage. Actually, \thepage also is broken and displaying the wrong page number when it's in the body of the document.

I'm currently working on the following document:

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{everypage}
\pagestyle{fancy}

\newcounter{CurrentPageCounter}
\newcounter{CurrentEquation}[CurrentPageCounter]
\lhead{Page \theCurrentPageCounter{}, equations on page: \theCurrentEquation{}} % not working

\newcommand{\addequation}{\stepcounter{CurrentEquation}}
\AddEverypageHook{\stepcounter{CurrentPageCounter}}
\chead{\today}
\rhead{Page \thepage{} of \pageref{LastPage}}
\cfoot{}
\begin{document}

Equation \addequation{}\theCurrentEquation{} on page \thepage{}:
\[ 1 != 0 \]
\\
\vspace{50em}
\\
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:
\[ 1 = 1 \]
\newpage
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:
\[ a > 0 \]
\end{document}

On an updated system, this gives output like

Page 0, equations on page: 2   March 31, 2021   Page 1 of 3


   Equation 1 on page 1:
                                1! = 0
Page 1, equations on page: 0   March 31, 2021   Page 2 of 3


Equation 2 on page 1:
                                1=1
Page 2, equations on page: 1   March 31, 2021   Page 3 of 3


   Equation 1 on page 3:
                                a>0

but it should have "equations on page: 2" for the first page and "equations on page: 1" for the second.

Update: With Willie's changes I have the following issue where the header reports the incorrect count:

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{perpage}
\pagestyle{fancy}

\newcounter{CurrentEquation}
\MakePerPage{CurrentEquation}
\lhead{Page \thepage{}, equations on page: \theCurrentEquation{}} % not working, appears to update after the third equation

\newcommand{\addequation}{\stepcounter{CurrentEquation}}
\chead{\today}
\rhead{Page \thepage{} of \pageref{LastPage}}
\cfoot{}
\begin{document}

Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 != 0 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ 1 = 1 \]
\newpage

Equation \addequation{}\theCurrentEquation{} on page \thepage{}:\[ a > 0 \]
\end{document}

The output looks something like

Page 1, equations on page: 3   March 31, 2021   Page 1 of 3


   Equation 1 on page 1:
                                1! = 0
Equation 2 on page 1:
                                1=1
Equation 3 on page 1:
                                1=1
Equation 4 on page 1:
                                1=1
Equation 5 on page 1:
                                1=1
Equation 6 on page 1:
                                1=1
Equation 7 on page 1:
                                1=1
Equation 8 on page 1:
                                1=1
Equation 9 on page 1:
                                1=1
Equation 10 on page 1:
                                1=1
Equation 11 on page 1:
                                1=1
Equation 12 on page 1:
                                1=1
Equation 13 on page 1:
                                1=1
Equation 14 on page 1:
                                1=1
Equation 15 on page 1:
                                1=1
Equation 16 on page 1:
                                1=1
Equation 17 on page 1:
                                1=1
Equation 18 on page 1:
                                1=1
Page 2, equations on page: 5   March 31, 2021   Page 2 of 3


Equation 1 on page 1:
                                1=1
Equation 2 on page 1:
                                1=1
Equation 3 on page 1:
                                1=1
Equation 4 on page 2:
                                1=1
Equation 5 on page 2:
                                1=1
Page 3, equations on page: 1   March 31, 2021   Page 3 of 3


   Equation 1 on page 3:
                                a>0
8
  • Using your MWE, I get the proper number of equations listed on each page. However, I get a page-number count that is off by one. However, I can fix your page issue by not using CurrentPageCounter and just using \thepage, which is already provided by LaTeX. Mar 31, 2021 at 17:56
  • @StevenB.Segletes Would an old version change the AddEveryPageHook to make it run after the page is done being processed? I'm using the version from Alpine Linux, but it doesn't have some of the new Fall update stuff so I'm still using the obsolete everypage package.
    – new user
    Mar 31, 2021 at 18:01
  • It is possibly a version issue. My version is everypage 2020/10/17 R2.0b Mar 31, 2021 at 18:05
  • My log has Package: everypage 2007/06/20 1.1 Hooks to run on every page in it.
    – new user
    Mar 31, 2021 at 18:09
  • @StevenB.Segletes It looks like that was only one of my problems. Running it on an updated Gentoo gives the correct results for the example, but if I separate the two equations on the first page with enough content (a bunch of `\` to make them go on different pages) it counts both equations on page 1, zero on page 2, and 1 on page 3.
    – new user
    Mar 31, 2021 at 18:59

1 Answer 1

2

Updated:

\documentclass{article}
\usepackage{etex}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{perpage}
\pagestyle{fancy}

\newcounter{CurrentEquation}
\MakePerPage{CurrentEquation}

\makeatletter
\def\pp@pagectr#1#2#3#4{\@ifundefined{c@pp@a@#1}{}{%
    \addtocounter{pp@a@#1}\@ne
    \expandafter\pp@labeldef\expandafter
      {\number\value{pp@a@#1}}{#1}{#2}{#3}{#4}}%
      \@ifundefined{c@nu@pp@peq@#3}{\newcounter{nu@pp@peq@#3}}{}%
      \stepcounter{nu@pp@peq@#3}}
      \newcommand\showPageTotal[1]{\csname thenu@pp@peq@#1\endcsname}
\makeatother

\lhead{Page \thepage{}, equations on page: \showPageTotal{\thepage}}

\newcommand{\addequation}{\stepcounter{CurrentEquation}}
\chead{\today}
\rhead{Page \thepage{} of \pageref{LastPage}}
\cfoot{}
\begin{document}

Equation \addequation{}\theCurrentEquation{} on page \thepage{}:
\[ 1 != 0 \]
\\
\vspace{50em}
\\
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:
\[ 1 = 1 \]Equation \addequation{}\theCurrentEquation{} on page \thepage{}:
\[ 1 = 1 \]Equation \addequation{}\theCurrentEquation{} on page \thepage{}:
\[ 1 = 1 \]Equation \addequation{}\theCurrentEquation{} on page \thepage{}:
\[ 1 = 1 \]
\newpage
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:
\[ a > 0 \]
\end{document}

This still uses perpage (see below); but now the header should be guaranteed correct, at least it should be as correct as perpage is. Instead of relying on the value of \theCurrentEquation to be correct at ship-out, here we tap into the guts of perpage and piggy back a count.

  1. perpage uses a label-like mechanism to locate the page numbers of the counters being stepped, and writes that information to the .aux file. This gets loaded at \begin{document} and allows it to compute how to match from the values of the CurrentEquation counter to the "per-page" values to be displayed.
  2. Each time the CurrentEquation is stepped a line is written to the .aux file with, among other things, the absolute page number that it corresponds to.
  3. So the code above patches the \pp@pagectr command that gets used, so that each time it is called, a per-page total counter is increased. (Downside: this will create a new counter per page.) This means that at the end of \begin{document} the per-page total count is already available, after the second compilation.
  4. We also define a \showPageTotal command to display the information.

Caveats: the code will need a bit modification if you have more than one "PerPage" counter that you want to track totals of. Currently it just counts everything that shows up on each page. Modifying it to separate streams is not too hard.


TeX's page output is asynchronous from its processing of the input, and so trying to manually peg pages is difficult. So instead of doing it yourself, let someone else do it for you. Use the perpage package:

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{perpage}
\pagestyle{fancy}

\newcounter{CurrentEquation}
\MakePerPage{CurrentEquation}
\lhead{Page \thepage{}, equations on page: \theCurrentEquation{}} % not working

\newcommand{\addequation}{\stepcounter{CurrentEquation}}
\chead{\today}
\rhead{Page \thepage{} of \pageref{LastPage}}
\cfoot{}
\begin{document}

Equation \addequation{}\theCurrentEquation{} on page \thepage{}:
\[ 1 != 0 \]
\\
\vspace{50em}
\\
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:
\[ 1 = 1 \]
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:
\[ 1 = 1 \]
\newpage
Equation \addequation{}\theCurrentEquation{} on page \thepage{}:
\[ a > 0 \]
\end{document}

The perpage package uses the \label mechanism behind the scenes, and so requires multiple passes for things to converge if they sit near page boundaries.

(Notice that the page number of the first equation shown on page 2 is incorrect, this is because when \thepage was processed it was still on page 1. But the CurrentEquation counter reports the correct values now. If you also want to access the current page number for real, the perpage package also provides the \theperpage command.)

4
  • (Credit: what you are doing is a lot similar to this question, from which this answer was taken.) Apr 1, 2021 at 2:09
  • It looks like this is mostly working, but the header with the count can get off. I'll add to the bottom of the question if I can make a small example.
    – new user
    Apr 1, 2021 at 13:59
  • Please see the update and let me know if it helps. Apr 1, 2021 at 14:50
  • Wow that got really complicated.
    – new user
    Apr 1, 2021 at 15:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .