5

I'd like to put a list of pages, hyperlinked to the position of the \eqref or \cref that point to it in the margin of each \labeled equation, like backref does for bibliographies and \cite.

Both this answer for \ref and this one for a figure list don't support hyperref links, deduplication and ranges like backref. (Edit: see below and first comment, turns out to not be that hard).

Edit: so my main problem is displaying text in the margin next to an equation's tag.

Edit 2: posted my current solution as an answer

Edit 3: It doesn't work with cleveref because that patches \label{X} in a way that \df@label now contains {X} instead of just X as before which breaks \refsto. No idea how that happens, or how I could remove the {}. Edit 4: actually it's when amsmath is loaded before cleverref. But it must be loaded before, because \cref can't be used to refer to equations otherwise.

Edit 5: found a way to remove {} if present, is there a builtin command for this?

\def\maybeWithBraces{foo} % this
\def\maybeWithBraces{{foo}} % or this
\def\@identity#1{#1}
\def\@@identity#1{\@identity#1}
\edef\withoutBraces{\expandafter\@@identity\maybeWithBraces}
% \withoutBraces→foo in both cases.
2
  • Something like this: tex.stackexchange.com/questions/14981/… Commented May 25, 2013 at 7:58
  • Ah yes, turns out getting the backreferences isn't the main problem but putting them next to the equation. Didn't expect that when writing the question, I'll edit it
    – pascal
    Commented May 26, 2013 at 14:01

1 Answer 1

6

Found I had to patch \make@display@tag, before calling, the current label is in \df@label, it's written out and reset there. By storing the label, calling the original and using the label with my \refsto I can put backreferences in the margin.

There's no support for ranges like “see p. 2—4” though.

\ProvidesPackage{eqbackref}[2013/05/26 v0.1 Backreferences for equations]
\RequirePackage{amsmath}
\RequirePackage{etoolbox}

% dump both reference ends
\newcounter{eqbr@source}
\newcommand{\eqbr@record}[1]{%
    \edef\@l{eqbr\arabic{eqbr@source}}\label{\@l}%
    \if@filesw\protected@write\@auxout{}%
        {\string\eqbr@referenced{#1}{\@l}{\thepage}}\fi%
    \stepcounter{eqbr@source}}

% patch amsmath's eqref
\let\ams@eqref=\eqref
\renewcommand{\eqref}[1]{%
    \eqbr@record{#1}%
    \ams@eqref{#1}}

% make a list of refering pages
\newcommand{\eqbr@referenced}[3]{%
    \ifinlistcs{#3}{eqbr@page@#1}{}{%
        \listcsxadd{eqbr@page@#1}{#3}%
        \listcsxadd{eqbr@back@#1}{#2}}}

\newcommand{\refsto@prefix}{see p. }
\newcommand{\refsto@delim}{, }
\newcommand{\refsto@suffix}{}
% print list of refering pages
\newcommand{\refsto}[1]{%
    \ifcsundef{eqbr@back@#1}{}{%
        \refsto@prefix%
        \undef\@first%
        \renewcommand*{\do}[1]{%
            \ifdef\@first{\refsto@delim}{\def\@first{}}%
            \pageref{##1}}%
        \dolistcsloop{eqbr@back@#1}%
        \refsto@suffix}}

\newcommand{\refsto@marginstyle}{\footnotesize\quad}
% patch tag display, show refsto in margin.
\let\ams@make@display@tag=\make@display@tag
% sometimes \df@label expands to `{name}` instead of `name`
\def\@@removebrace@#1{#1}
\def\@@removebrace#1{\@@removebrace@#1}
\renewcommand{\make@display@tag}{%
    \ifdefempty{\df@label}{% can happen with package `autonum`.
        \ams@make@display@tag%
    }{% not empty, show refs
        \edef\orig@label{\expandafter\@@removebrace\df@label}% save label
        \ams@make@display@tag% resets label
        \rlap{\mbox{\refsto@marginstyle\refsto\orig@label}}% show
    }%
}

Example use:

\documentclass{article}
\usepackage{hyperref} % optional
\usepackage{amsmath} % load before cleveref    
\usepackage{cleveref} % optional
\ussepackage{autonum} % optional
\usepackage{eqbackref}
\begin{document}
\begin{align}
    E_1 \label{eA} \\
    E_2 \label{eB} \\
    E_3 \label{eC}
\end{align}
\newpage\section{page two}\eqref{eA}
\newpage\section{page three}\eqref{eA}\eqref{eB}\eqref{eB}
\newpage\section{page four}\eqref{eA}
\end{document}

Generates:

backrefs in margin

1
  • This only works for equations defined in align environments but not in equation environments.
    – Paul Wintz
    Commented Aug 2, 2023 at 0:10

You must log in to answer this question.

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