% AJ Roberts, http://orcid.org/0000-0001-8930-1552, Dec 2016 -- Apr 2018
% At every \label, prints pages at which the label is referenced
% via \ref, \eqref, \autoref, \cref or \Cref
% One option "disable" which omits the package, but 
% defines the commands that might still be in auxiliary files
% ToDo: 
% * need to gobble space after the tR@label, 
%   and also not interfere with mintoc headings afterwards
% * \label{} followed by my \minitoc needs a blank line between??
% * \cref in section headers DNW, Apr 2018
\newif\iftR@disable \tR@disablefalse
\DeclareOption{disable}{\tR@disabletrue}
\ProcessOptions

\iftR@disable% disable the commands
  \def\useRefPage#1#2{}
  \def\tR@AutoRef#1{} \def\tR@AutoRefS#1{}
  \def\tR@AutoPageRef#1{} \def\tR@AutoPageRefS#1{}
  \endinput
\fi

\RequirePackage{hyperref} % tested with 2012/11/06 v6.83m
\RequirePackage{pgffor}% for cleveref over comma separated list
\AtBeginDocument{% must invoke after hyperref  and cleveref are loaded
% Following for some of cleveref, Apr 2018
% But must NOT use cref in section headers??
\newcommand{\tR@list}[1]{\foreach \x in {#1} {\tRatRefPage{\x}}}
\let\tRcref\cref
\renewcommand{\cref}[1]{\tRcref{#1}\protect\tR@list{#1}}
\let\tRCref\Cref
\renewcommand{\Cref}[1]{\tRCref{#1}\protect\tR@list{#1}}
% Following appears to work for \ref*{} in hyperref, Dec 2016
% There is no \eqref*{} defined
\let\tR@T@ref\T@ref
\def\T@ref#1{\tRatRefPage{#1}\tR@T@ref{#1}}
% Interfere with \autoref
\let\tR@autoref\autoref 
% This does not work with the star version
%\def\autoref#1{\protect\tRatRefPage{#1}\tR@autoref{#1}}
\def\autoref{\protect\@ifnextchar *{\protect\tR@AutoRefS}{\protect\tR@AutoRef}}
\def\tR@AutoRef#1{\protect\tRatRefPage{#1}\tR@autoref{#1}}
\def\tR@AutoRefS*#1{\protect\tRatRefPage{#1}\tR@autoref*{#1}}
% Interfere with \autoref*
\let\tR@autopageref\autopageref 
%\def\autopageref#1{\protect\tRatRefPage{#1}\tR@autopageref{#1}}
\def\autopageref{\protect\@ifnextchar *{\protect\tR@AutoPageRefS}{\protect\tR@AutoPageRef}}
\def\tR@AutoPageRef#1{\protect\tRatRefPage{#1}\tR@autopageref{#1}}
\def\tR@AutoPageRefS*#1{\protect\tRatRefPage{#1}\tR@autopageref*{#1}}
% needs all the above protection for captions, and sections, why??
\let\tR@label\label
\def\label#1{\iftrackRefsAuto%
  \@ifundefined{TR@#1}{}%
  {\trackRefsSet{\trackRefsPreamble\csname TR@#1\endcsname}}%
  \fi\tR@label{#1}}
% next is for amsmath, and is also OK if amsmath not invoked
\let\tR@label@in@display\label@in@display
\def\label@in@display#1{\iftrackRefsAuto%
  \@ifundefined{TR@#1}{}%
  {\trackRefsSet{\trackRefsPreamble\csname TR@#1\endcsname}}%
  \fi\tR@label@in@display{#1}}
}%endAtBeginDocument

% \tRatRefPage cannot have @ in name as might occur via toc, lof, ... 
\newcommand{\tRatRefPage}[1]{\@bsphack
  \protected@write\@auxout{}%
    {\string\useRefPage{#1}{\thepage}}%
  \@esphack}
\let\tR@RefPage\tRatRefPage% for backwards compatibility for now
\newcommand{\useRefPage}[2]{%
  \@ifundefined{TR@#1}%
  {\expandafter\global\expandafter\edef\csname TR@#1\endcsname{#2}}
  {\expandafter\global\expandafter\edef\csname TR@#1\endcsname{%
    \csname TR@#1\endcsname, #2}}
  }
  
\newcommand{\pagesRefd}[1]{\csname TR@#1\endcsname}
\newif\iftrackRefsAuto 
\trackRefsAutotrue
\def\trackRefsPreamble{\color[rgb]{0,0.5,0}\sf\footnotesize pp.~}
\newcommand{\trackRefsSet}[1]{\makebox[0pt][c]{%
  \raisebox{1.7ex}[0pt][0pt]{#1}}}

% following OKish
% Uses marginnote, but sometimes is on wrong side
%\RequirePackage{pdfcomment}
%\def\trackRefsPreamble{Referenced on pp.~}
%\renewcommand{\trackRefsSet}[1]
%  {\pdfmargincomment[icon=Help,color={0.7 1 0.7}]{#1}}

%% following OKish, but does not seem to have any advantages.
%\RequirePackage{pdfcomment}
%\def\trackRefsPreamble{pp.~}
%\renewcommand{\trackRefsSet}[1]
%  {\pdffreetextcomment[width=6em, height=4.5ex, fontsize=8pt,
%  fontcolor=magenta, color=white]{#1}}

