4

In Making broken references extra-visible, I asked how to make LaTex's normal ?? and [?] markers for broken references instead render as some super-annoying, colored text, so that in skimming a long book-class document, I can know instantly what needs fixing.

I have been instructed now how to do this for broken \refs, but the MWE I provided was too minimal: I'd also like to know how to similarly mark broken \Crefs (available via the cleveref package).

Here is an MWE in case there is any doubt what I mean by this.

 \documentclass{article}
 \usepackage{amsthm,hyperref,cleveref}
 \bibliographystyle{amsalpha}
 \newtheorem{theorem}{Theorem}
 \begin{document}
 \begin{theorem}\label{catfish}
 \end{theorem}
 Theorem \ref{catfish} implies the truth of \Cref{god}.
 \bibliography{bibshort}{}
 \end{document}.
  • 1
    Related (but different: provided for background): tex.stackexchange.com/questions/224799/…. – cfr Jan 27 '15 at 0:55
  • Do you want to patch up all the cleveref macros or just \Cref? – Paul Gessler Jan 27 '15 at 0:58
  • That's the only one I've used so far. Maybe I should be using others, and then I'd want them similarly patched. – jdc Jan 27 '15 at 20:00
7

Assuming you want all of the cleveref user-level macros to behave this way, and not solely \Cref, we can use a similar approach to the one in the original question, substituting the internal cleveref macro names and search code:

\documentclass{article}
\usepackage{amsthm,etoolbox,xcolor,hyperref,cleveref}

\makeatletter
\patchcmd{\@@setcref}         {??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setcref}         {??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setcrefrange}    {??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setcrefrange}    {??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setcrefrange}    {??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setcrefrange}    {??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setcrefrange}    {??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setcrefrange}    {??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setnamecref}     {??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setnamecref}     {??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setcpageref}     {??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setcpageref}     {??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setcpagerefrange}{??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setcpagerefrange}{??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setcpagerefrange}{??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setcpagerefrange}{??}{\color{red} undefined Label}{}{}
\patchcmd{\@@setcpagerefrange}{??}{\color{red} undefined Label}{}{}
\patchcmd{\@@cref}            {??}{\color{red} undefined Label}{}{}
\makeatother

\bibliographystyle{amsalpha}
\newtheorem{theorem}{Theorem}
\begin{document}
\section{Section}\label{god}
\subsection{Subsection}\label{dog}
\subsection{Subsection 2}\label{dog2}
\begin{theorem}\label{catfish}
\end{theorem}
\begin{equation}\label{math}
\end{equation}
\Cref{catfish} implies the truth of \cref{god} and \cref{dog}.

Also \cref{catfish,dog,god,math}. \Crefrange{dog}{dog2} with \crefrange{dog}{dog2}.
\bibliography{bibshort}
\end{document}

There are many more patches to perform here, because cleveref has many private helper macros for its different user-facing commands. Additionally, a replacement is only performed the first time the search text is found in the command, so we must repeat the patch if the search text occurs multiple times within one command.

Output on the first run:

enter image description here

Output on the second run (resolved references):

enter image description here

The patch text (here, undefined Label) and color red can be customized to your liking, and you can use any other formatting commands to change the way this is typeset. Inside the macros being patched, the second argument (#2) is the label, so you can use #2 anywhere in the patch replacement text to display the particular label that is undefined. For example:

\patchcmd{\@@setcref}{??}{\color{red} undefd label (#2)}{}{} % typ. (repeat for all patches)

will print "undefd label (catfish)" in red in the text when the label is not resolved.


Note: the internal macro names have changed in a subsequent version of cleveref from the current version at the time of the original answer. I've updated the answer to match the new names, but if you need to use an older version of cleveref, check the edit history of this post.

  • 1
    Is it possible to have the replacement text include the label it tries to refer to? It would make it easier to have these as placeholders for future references and still keep some readability. – retorquere Dec 28 '15 at 14:04
  • 1
    @retorquere sure, just use #1 anywhere in the patch replacement text to print the label name. For example, \patchcmd{\@setcref}{??}{\color{red} undefd label (#1)}{}{} to print undefd label (catfish) in red in the text. – Paul Gessler Dec 28 '15 at 19:31
  • Awesome! This should be the default, really. So useful! – retorquere Dec 28 '15 at 19:39
  • it seem now an extra @ is necessary, e.g. \patchcmd{\@@setcref} – user190633 Jun 12 at 13:21
  • Thanks for noticing this @user190633, I've updated the post – Paul Gessler Jun 13 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.