(This question and answer led to the new package cleveref-usedon, see the CTAN version or the dev version on github.)
I am trying to overload \label and \cref, so that the following happens (similar in spirit to backref's \backcite):
- In some preliminary chapter, I define in Theorem A a label
\label{thm:A}. - Later in the main text I am referencing Theorem A via
\cref{thm:A}. - Retrospectively, I would like the reader of the prelim chapter to know where the result will be used later on in the main text by letting the header of Theorem A be followed by a "Used on page XYZ." phrase.
I have tried the following which works when the label is only referenced once.
Basically, I create a command \crefusedon which in turn defines a label which adds a prefix backref: to the original labelname, e.g. backref:thm:A. The label command is overloaded to search for the existence of backref:LABELNAME and print the text above. The \cref command is overloaded to include \crefusedon. This way I only have to manually add the option [usedon] to all the \cref's in my main text which need it and the rest work as before.
Problem(s):
- The downside of this is that it currently only works for one (or rather the last) call of
\crefusedon. Of course, it would be much better to actually take note of all\crefusedoncalls and have a phrase such as "Used on pages X, Y and Z." if there multiple calls. Can I usebackreffor this? - I can
\RenewDocumentCommand\labelbut for some reason not\cref. Any idea why? - Somewhat confusingly the hyperlink in the
\cpagerefdoes not link to the correct page in this MWE. However, in my normal document the\cpagerefhyperlink works but sometimes the opposite\crefusedonhyperlink points to a (very) wrong page.
I'm guessing I can somehow use or modify \backcite to do what I want but I've had trouble understanding backref's and cleveref's codebase.
What's a sensible solution to this? If backref can do this, I'm happy to scrap the below half-solution.
Btw, I am using biblatex in my normal document and know that there might be a problem with backref in that case.
\documentclass{amsart}
\usepackage{amsthm} %AMS theorems
\usepackage{thmtools} %better theorem editing interface
\declaretheorem[name=Theorem]{theorem}
\usepackage{tikz}
\usepackage{etoolbox}
\usepackage{xparse}
\usepackage{hyperref}
%=========================================
% -------- Exceptions to Hyperref (load last) ----------
%=========================================
\usepackage[capitalise]{cleveref} % needs to be loaded AFTER hyperref
% to not conflict with hyperref's \autoref
%=========================================
% -------- BEGIN Overload label and cref ----------
%=========================================
\makeatletter
\NewDocumentCommand{\usedon}{ m }{%
% Check if the label backref:<LABELNAME> exists
\ifcsdef{r@backref:#1}{%
\emph{(Used on \cpageref{backref:#1}.)}%
\%
}{}
}%
\makeatother
\AtBeginDocument{
\let\origlabel\label
\let\origcref\cref
\RenewDocumentCommand{\label}{ m }{%
\origlabel{#1}\usedon{#1}%
}%
\NewDocumentCommand{\crefusedon}{ s o m }{%
\IfBooleanTF{#1}{%
\origcref*{#3}
}{%
\origcref{#3}
}%
\ifstrequal{#2}{usedon}{%
% \cref can also get a list of labels as input, loop through them
\foreach \x in {#3}{
\label{backref:\x}%
}%
}{}%
}%
\let\cref\crefusedon
}%
%=========================================
% -------- END Overload label and cref ----------
%=========================================
\begin{document}
\section{Introduction to wonders}
\begin{theorem}
\label{thm:egregium}
This is a great theorem about wonderful mathematics.
\end{theorem}
\begin{theorem}
\label{thm:inconspicuam}
A plane is flat.
\end{theorem}
\clearpage
As seen in \cref[usedon]{thm:egregium} the earth is positively curved
and according to \cref{thm:inconspicuam} flat earth is not.
\clearpage
As seen in \cref[usedon]{thm:egregium} the earth is positively curved
and according to \cref{thm:inconspicuam} flat earth is not.
\end{document}