# Varioref and previous page

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 ?

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

\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