9

With the following MWE:

\documentclass{article}

\usepackage{thmtools}
\usepackage{varioref}
\usepackage{cleveref}

\declaretheorem[name=Lemma,Refname={Lemma,Lemmas}]{lem}

\begin{document}

\begin{lem}\label{lem1}
    A lemma.
\end{lem}

\clearpage

\begin{lem}\label{lem2}
    A lemma.
\end{lem}

\begin{lem}\label{lem3}
    A lemma.
\end{lem}

\Vref{lem1}, \Vref{lem2} and \Vref{lem3}.

\end{document}

Varioref produces: Lemma 1 on page 1, Lemma 2 and Lemma 3. Where I was expecting: Lemma 1 on the previous page, Lemma 2 and Lemma 3.

Is it just me that don't understand varioref, or is there some option to get the second behaviour ?

lockstep
  • 250,273
Zii8roZi
  • 555

2 Answers2

8

You understand varioref correctly, but cleveref overwrites some of its functions and does so incorrectly. It is a typical case of adding % characters to the end of every line and in some cases that is simply wrong.

The cleveref packages makes a modified copy of the command \@@vpageref that contained the lines

    \advance\@tempcnta-2
    \ifnum \thevpagerefnum =\@tempcnta

The redefinition turns this into

    \advance\@tempcnta-2%                 <--- wrong %
    \ifnum \thevpagerefnum =\@tempcnta%

As a result TeX expands the \ifnum while still looking for the finish of the number -2... instead of substracting 2 from \@tempcnta and then doing the test.

In other words: blindly adding % the the end of every line in a definition can be harmful (the one on the next line is also useless but does no harm).

So the best fix is to get cleveref corrected. Short term solution: you can repatch it like this:

\usepackage{etoolbox}
\makeatletter
\patchcmd\cref@old@@vpageref
{\advance\@tempcnta-2}
{\advance\@tempcnta-2 }{\typeout{patch ok}}{\ERRORpatchFaild}
\makeatother
4

Ugh. I think some time ago I got fed up with tracking down spurious spacing errors in cleveref, and ran the source through sed to add % at the ends of all lines. That was dumb. But then, so is TeX's whitespace handling.

@Frank Mittelbach: Thanks for taking the time to track this down. This should be fixed in version 0.19.3 now on my web site. The MWE from this question works, at least. I'll upload to CTAN after a bit more testing. (I added a \relax instead of removing the %, as it makes it slightly easier to track down spurious spaces if any more crop up in future.)

I fear there may be other similar bugs still lurking in cleveref, though.

Denis Bitouzé
  • 9,652
  • 4
  • 27
  • 85
Toby Cubitt
  • 2,072
  • 1
    Hi Toby, could this be updated in CTAN? The one on your website seems to work great, but it would be nice if it didn't need to be manually installed. I guess it's a weird one since although this is a 'correction', it wouldn't be entirely backwards compatible. Still, cleveref is great anyway :) – zelanix Apr 06 '17 at 22:36