# Making refcheck work with cleveref

It seems that refcheck does not recognize references made by cleveref:

\documentclass{article}
\usepackage{amsmath}
\usepackage{cleveref}
\usepackage{refcheck}

\begin{document}
\begin{align}
a & = b \label{eq:a} \\
c & = d \label{eq:b}
\end{align}

See~\eqref{eq:a}, \cref{eq:b}.

\begin{figure}
\caption{Figure 1}
\label{fig:a}
\end{figure}
\begin{figure}
\caption{Figure 2}
\label{fig:b}
\end{figure}

See Figure~\ref{fig:a}, \cref{fig:b}.

\end{document}


Here, refcheck marks eq:b and fig:b as unreferenced.

1. Is there a way to make the two packages work together?
2. Alternatively, are there replacement packages that do work better?
3. If the answer to 1) is yes, can this even be achieved for \crefrange?
• Have you tried "Poor man's cleveref" (chapter 10 in cleveref's docs)? This should be an easy workaround on a Linux or Mac OS X system. – krlmlr Dec 19 '12 at 23:19
• @user946850 I know that option, but I'm not convinced. As a purist, I don't want to run sed scripts in between. And you still have the false positives when using compressed references (\crefrange, and \cref with several labels). – mafp Dec 19 '12 at 23:40

refcheck doesn't know about \cref and \Cref. We can add a macro that makes the package aware of other reference commands:

\documentclass{article}
\usepackage{amsmath}
\usepackage{cleveref}
\usepackage{refcheck}

%%% Infrastructure
\makeatletter
\newcommand{\refcheckize}[1]{%
\expandafter\let\csname @@\string#1\endcsname#1%
\expandafter\DeclareRobustCommand\csname relax\string#1\endcsname[1]{%
\csname @@\string#1\endcsname{##1}\wrtusdrf{##1}}%
\expandafter\let\expandafter#1\csname relax\string#1\endcsname
}
\makeatother
%%%

%%% Now we add the reference commands we want refcheck to be aware of
\refcheckize{\cref}
\refcheckize{\Cref}

\begin{document}
\begin{align}
a & = b \label{eq:a} \\
c & = d \label{eq:b}
\end{align}

See~\eqref{eq:a}, \cref{eq:b}.

\begin{figure}
\caption{Figure 1}
\label{fig:a}
\end{figure}
\begin{figure}
\caption{Figure 2}
\label{fig:b}
\end{figure}

See Figure~\ref{fig:a}, \Cref{fig:b}.

\end{document}


• This is great! But does it work when \creffing multiple labels? And what about \crefrange and friends? -- It seems that at least one of the packages should be enhanced to support the other, perhaps both: I imagine a command like \noref (provided by refcheck) that would simply mark a reference as "used" and could be called by cleveref, autoref, nameref, ... – krlmlr Dec 19 '12 at 23:06
• Nice. Unfortunately, I use \cref almost exclusively for multiple references to employ the compression feature, and in this case your solution obviously won't work. I guess one needs to hook deeper into the packages. Should I ask the cleveref author for active support of refcheck, or the refcheck author (I'm not sure if he is still an active Texie.)? – mafp Dec 19 '12 at 23:09
• @mafp: According to egreg's comment, it seems that there's no need to modify refcheck, but cleveref (or its author...) should be made aware of that package. The cleveref docs suggest that (a) the package is well maintained and (b) the author is not aware of refcheck (quick search for "refcheck"). It would definitely be a great improvement for cleveref... – krlmlr Dec 19 '12 at 23:12
• This also works with \autoref of hyperref. – Reinstate Monica Jul 21 '14 at 20:37
• @egreg Any updates/thoughts on this after three years? Could not find any tool (like chklref) or package (like refcheck) that supports commands such as \Cref{fig:a,fig:b}. Already wrote the cleveref author. – Alex Jan 4 '16 at 20:29

I ran into the same issue, and found it was relatively straightforward to extend egreg's answer to handle extra cases like \crefrange{eq1}{eq2} and \cref{eq1,eq2,eq4}:

\makeatletter
\newcommand{\refcheckize}[1]{%
\expandafter\let\csname @@\string#1\endcsname#1%
\expandafter\DeclareRobustCommand\csname relax\string#1\endcsname[1]{%
\csname @@\string#1\endcsname{##1}\@for\@temp:=##1\do{\wrtusdrf{\@temp}\wrtusdrf{{\@temp}}}}%
\expandafter\let\expandafter#1\csname relax\string#1\endcsname
}
\newcommand{\refcheckizetwo}[1]{%
\expandafter\let\csname @@\string#1\endcsname#1%
\expandafter\DeclareRobustCommand\csname relax\string#1\endcsname[2]{%
\csname @@\string#1\endcsname{##1}{##2}\wrtusdrf{##1}\wrtusdrf{{##1}}\wrtusdrf{##2}\wrtusdrf{{##2}}}%
\expandafter\let\expandafter#1\csname relax\string#1\endcsname
}
\makeatother

\refcheckize{\cref}
\refcheckize{\Cref}
\refcheckizetwo{\crefrange}
\refcheckizetwo{\Crefrange}


Credit goes to egreg for the original solution.

## 2016 Edit:

I'm not sure why, but this had stopped working for me at some point. I started getting a lot of errors like this again:

Package refcheck Warning: Unused label {eq:objective}' on input line 30.


For whatever reason (maybe a strange interaction of packages) refcheck was expecting an extra pair of curly braces. That is, it expected \label{eq:objective} to be paired with \wrtusdrf{{eq:objective}}, but the \refcheckize commands were only producing \wrtusdrf{eq:objective}.

To fix this I have now put a matching \wrtusdrf{{...}} along with every \wrtusdrf{...} in the macros. This shouldn't have any negative effect provided that you don't differentiate labels solely with curly braces (please don't do that.)

• Works very well. However, the solution doesn't seem to like subfigures (using the subcaption package). – Alex Dec 25 '15 at 19:01

I ended up with using a little shell script to find unreferenced labels. This has some drawbacks of course, but works very well in my case (document with hundreds of pages):

# Helper function to find unreferenced labels, since the chkltex tool and the refcheck package do not support cleveref
# Note: this could show false positives sometimes
echo "Checking for unreferenced labels...";
labels=$(grep -Eo '^[\s]*[^%]+.*label{[^}]+}'$_dir_sources/*.tex|sed -Ee "s/^.*{(.*)}/\1/g")
for label in $labels; do found=$(grep -E "ref{[^}]*$label"$_dir_sources/*.tex)
if [[ -z "$found" ]]; then echo "Label '$label' not referenced?";
fi
done
`