计算一页中的方程式数量

计算一页中的方程式数量

有没有办法计算一页中的方程式(或其他计数项目)的数量并将其显示在页眉中?当分页符出现时,我无法保持正确的计数,而且我没有使用\newpage。实际上,\thepage当它在文档正文中时,也损坏了,并显示错误的页码。

我目前正在处理以下文档:

\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}

在更新的系统上,这将给出如下输出

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

但是第一页应该有“第 2 页上的方程式”,第二页应该有“第 1 页上的方程式”。

更新:由于 Willie 的更改,我遇到了以下问题,即标题报告了错误的计数:

\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}

输出类似于

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

答案1

更新

\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}

这仍然使用perpage(见下文);但现在应该保证标题正确,至少它应该尽可能正确perpage。我们不再依赖 的值来\theCurrentEquation确保发货时的正确性,而是利用 的核心perpage并附加计数。

  1. perpage使用label类似 的机制来定位正在步进的计数器的页码,并将该信息写入文件.aux。该文件在 处加载\begin{document},并允许它计算如何将CurrentEquation计数器的值与要显示的“每页”值进行匹配。
  2. 每次CurrentEquation执行时,都会将一行写入文件.aux,其中包括与其对应的绝对页码。
  3. 因此,上述代码修补了\pp@pagectr所使用的命令,以便每次调用该命令时,每页的总计数器都会增加。(缺点:这将为每个页面创建一个新的计数器。)这意味着在\begin{document}第二次编译后,每页的总计数已经可用。
  4. 我们还定义了一个\showPageTotal命令来显示信息。

注意事项:如果您有多个“PerPage”计数器要跟踪总数,则需要对代码进行一些修改。目前,它只计算每个页面上显示的所有内容。将其修改为单独的流并不难。


TeX 的页面输出与其输入处理是异步的,因此尝试手动固定页面是难的。因此,与其自己做,不如让别人帮你做。使用perpage包裹

\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}

perpage包使用\label幕后机制,因此如果事物位于页面边界附近,则需要多次传递才能收敛。

(请注意,第 2 页上显示的第一个方程的页码是不正确的,这是因为\thepage处理时它仍然在第 1 页。但计数器CurrentEquation现在报告了正确的值。如果您还想真正访问当前页码,该perpage包还提供了\theperpage命令。)

相关内容