5

I want to create a dictionary-like header, where on the top left of the even page and the top right of the odd page equation numbers will be shown: on the top left is the equation number of the first equation appearing on the page and on the top right is the last equation number.

I know I can achieve this with \marks and in particular the emarks package. Right now my "solution" requires manually pushing the new equation number onto the mark like

\marksthe{EqNumTracker}{\theequation} 

everytime I write a new equation, which seems to be less than ideal.

A further complication is that the equation counter is also touched by some other things (for example, using ntheorem I've requested that the theorems count on the same counter).

Question: what is the best way to automatically "mark" the equation number everytime the equation counter is incremented?


A minimal possibly working example:

\documentclass{book}
\usepackage[pagestyles]{titlesec}
\usepackage{emarks}
\usepackage{amsmath,amssymb}
\usepackage[amsmath,thmmarks,thref,hyperref]{ntheorem}
\usepackage{hyperref}

\usepackage{lipsum}

% Pagestyle using the marks. 
\makeatletter
\newpagestyle{wwwfancy}{\headrule%
    \setmarks{chapter}{section}
    \sethead[\thefirstmarks{www@equation@mark}]%
        [][]%
        {\footnotesize\slshape\ifthechapter{\chaptertitlename\ %
        \thechapter.\ \chaptertitle}{}}{}%
        {\thebotmarks{www@equation@mark}}%
    \setfoot[][\thepage][]{}{\thepage}{}}
\pagestyle{wwwfancy}

% Redefining refstepcounter
\let\www@old@refstepcounter\refstepcounter
\def\refstepcounter#1{%
    \www@old@refstepcounter{#1}%
    \marksthecs{www@#1@mark}{the#1}}
\makeatother

% Define a theorem style
\theoremstyle{break}
\theoremheaderfont{\normalfont\bfseries}
\theorembodyfont{\itshape}
\theoremseparator{}
\theoremindent0cm
\theoremnumbering{arabic}
\theoremsymbol{\ensuremath{_\blacksquare}}

\newtheorem{thm}[equation]{Theorem}

\numberwithin{equation}{section}

\begin{document}

\chapter{A chapter}

\lipsum[1-5]

\begin{equation}
E = mc^2
\end{equation}

\lipsum[6]

\begin{thm}\label{testref}
\lipsum[7]
\end{thm}

\lipsum[8-12]

A reference \thref{testref}. 

\lipsum[1-4]
\end{document}

Is there something I should watch out for?

(Note, in the above I defined a mark for every counter used, effectively. I think 32,768 is probably enough to do that... but I would also welcome an improvement that allows me to just track equation and nothing else.)

6
  • One can think to modify \stepcounter to do something more when receiving the equation counter as argument, but having a small example is better: if you use hyperref, for instance, a solution must take this into account and timing of redefinitions is crucial.
    – egreg
    Jul 5, 2016 at 20:18
  • @egreg: I included a first attempt (modified \refstepcounter instead). It seems to work. What is the potential problem with hyperref that you are thinking of? Jul 5, 2016 at 20:34
  • I've done something similar recently in another question ... looking for it...
    – user31729
    Jul 5, 2016 at 20:37
  • Since hyperref modifies \stepcounter/\refstepcounter, it's important to do the change at the right time.
    – egreg
    Jul 5, 2016 at 20:37
  • Something like here, perhaps: tex.stackexchange.com/questions/307688/… ?
    – user31729
    Jul 5, 2016 at 20:43

2 Answers 2

2

The code by the O.P. was already working, also with hyperref, but it placed a mark every time a counter was refstepped.

I've place some expl3 code to 'register' counter names in a sequence that should be tracked for convenience and perform a check in the redefined \refstepcounter command that only the relevant mark will be placed.

\documentclass{book}
\usepackage[pagestyles]{titlesec}
\usepackage{emarks}
\usepackage{amsmath,amssymb}
\usepackage[amsmath,thmmarks,thref,hyperref]{ntheorem}
\usepackage{xparse}
\usepackage{hyperref}


\usepackage{lipsum}

\ExplSyntaxOn
\seq_new:N \l_williewong_counter_seq
\newcommand{\RegisterMarkCounters}[1]{%
  \seq_set_from_clist:Nn \l_williewong_counter_seq {#1}
  \seq_remove_duplicates:N \l_williewong_counter_seq
}
\newcommand{\IsInMarkListT}[2]{%
  \seq_if_in:NnT \l_williewong_counter_seq {#1} {#2}
}
\ExplSyntaxOff

% Pagestyle using the marks. 
\makeatletter
\newpagestyle{wwwfancy}{\headrule%
    \setmarks{chapter}{section}
    \sethead[\thefirstmarks{www@equation@mark}]%
        [][]%
        {\footnotesize\slshape\ifthechapter{\chaptertitlename\ %
        \thechapter.\ \chaptertitle}{}}{}%
        {\thebotmarks{www@equation@mark}}%
    \setfoot[][\thepage][]{}{\thepage}{}}
\pagestyle{wwwfancy}





\RegisterMarkCounters{equation}

\let\www@old@refstepcounter\refstepcounter
\def\refstepcounter#1{%
  \www@old@refstepcounter{#1}%
  \IsInMarkListT{#1}{\marksthecs{www@#1@mark}{the#1}}
}


\makeatother

% Define a theorem style
\theoremstyle{break}
\theoremheaderfont{\normalfont\bfseries}
\theorembodyfont{\itshape}
\theoremseparator{}
\theoremindent0cm
\theoremnumbering{arabic}
\theoremsymbol{\ensuremath{_\blacksquare}}

\newtheorem{thm}[equation]{Theorem}
%\newtheorem{thm}{Theorem}

\numberwithin{equation}{section}

\begin{document}

\chapter{A chapter}

\section{Foo}

\begin{equation}
E = mc^2
\end{equation}


\lipsum[1-3]

\begin{equation}
E = mc^2
\end{equation}

\lipsum[6]

\begin{equation}
E = mc^2
\end{equation}


\begin{thm}\label{testref}
  \lipsum[7]
\end{thm}

\lipsum[8-12]

A reference \thref{testref}. 

\lipsum[1-4]


\begin{thm}\label{othertestref}
\lipsum[7]
\end{thm}

\begin{equation}
E = mc^2
\end{equation}

\lipsum[6]

\begin{equation}
E = mc^2
\end{equation}



\lipsum[8-12]

\end{document}
4
  • Slowly slowly I am getting used to reading expl3 code. Question about the data structure: what is a \seq? Is it ordered? Are entries unique? (In the sense of "what happens if I \RegisterMarkCounters twice the same counter?") In actually using it I would probably have to skip the \typeout since it will really add a lot of lines to the log files. Jul 6, 2016 at 4:13
  • I will add some explanations later on
    – user31729
    Jul 6, 2016 at 7:44
  • @WillieWong: Changed...
    – user31729
    Jul 6, 2016 at 11:22
  • @WillieWong: As \seq is a ordered list,but the entries are not unique by default, so it's not a set. You can remove duplicates with \seq_remove_duplicates:N
    – user31729
    Jul 6, 2016 at 12:07
2

We can add a hook at \stepcounter using etoolbox. Without amsmath the patch could be

\apptocmd{\stepcounter}{\csname action@#1\endcsname}{}{}

but amsmath already does a change.

\documentclass{book}
\usepackage[pagestyles]{titlesec}
\usepackage{emarks}
\usepackage{amsmath,amssymb}
\usepackage[amsmath,thmmarks,thref,hyperref]{ntheorem}
\usepackage{etoolbox}
\usepackage{hyperref}

\usepackage{lipsum}

% add a hook to \stepcounter
\patchcmd{\stepcounter}
  {\fi}
  {\csname action@#1\endcsname\fi}
  {}{}
\makeatletter
\newcommand{\definecounteraction}[2]{\@namedef{action@#1}{#2}}
\makeatother

% Pagestyle using the marks. 
\makeatletter
\newpagestyle{wwwfancy}{\headrule%
    \setmarks{chapter}{section}
    \sethead[\thefirstmarks{www@equation@mark}]%
        [][]%
        {\footnotesize\slshape\ifthechapter{\chaptertitlename\ %
        \thechapter.\ \chaptertitle}{}}{}%
        {\thebotmarks{www@equation@mark}}%
    \setfoot[][\thepage][]{}{\thepage}{}}
\pagestyle{wwwfancy}

\definecounteraction{equation}{%
  \typeout{=== Stepping equation ===}%
  \marksthe{www@equation@mark}{\theequation}%
}

% Define a theorem style
\theoremstyle{break}
\theoremheaderfont{\normalfont\bfseries}
\theorembodyfont{\itshape}
\theoremseparator{}
\theoremindent0cm
\theoremnumbering{arabic}
\theoremsymbol{\ensuremath{_\blacksquare}}

\newtheorem{thm}[equation]{Theorem}

\numberwithin{equation}{section}

\begin{document}

\chapter{A chapter}

\lipsum[1-5]

\begin{equation}
E = mc^2
\end{equation}

\lipsum[6]

\begin{thm}\label{testref}
\lipsum[7]
\end{thm}

\lipsum[8-12]

A reference \thref{testref}. 

\lipsum[1-4]
\end{document}

enter image description here

2
  • Both great answers! I flipped a coin. Jul 6, 2016 at 13:09
  • @WillieWong Which fell on the wrong side. ;-)
    – egreg
    Jul 6, 2016 at 13:56

You must log in to answer this question.

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