# varioref: \vref or \vpageref at page boundary (may loop)

Sometimes a have situations, where I'm not able to compile a document, because the varioref package result in a loop. Here is a MWE, which result not in an endless loop, but it demonstrate the issue (tested with TeX Live 2012)

\documentclass[12pt]{article}
\usepackage{blindtext,varioref}
\begin{document}
\blindtext[3]

These words are only for demonstration purposes. These words are only for demonstration purposes. These words are only for demonstration purposes. These words are only for demonstration purposes. These words are only for demonstration purposes. These words are only for demonstration purposes. These words are only for demonstration purposes. These words are only for demonstration purposes. These words are only for ... See table \vref{tab:example}

\begin{table}[htbp]
\centering
\begin{tabular}{l}
Test\\
\end{tabular}
\caption{Example Table}
\label{tab:example}
\end{table}
\blindtext
\end{document}


Is there a rock solid solution for this issue? I known, removing a word or replace \vref with \ref helps, but this is not a solution (for my opinion).

To say it short: the only solution for this is: rephrase the sentence or use \ref or \pageref.

A little bit longer: This error message informs you that the created reference goes over the current page and variorefdoes not know whether it should count it on page x or x+1. Is the reference on page x the text of the reference is calculated to "this page" or "the page before". If the reference would be on page x+1 it should be "the page before" or "two pages before". If the reference is located on two pages varioref can't be precice.

To get rid of this error the only way is to rephrase the sentence so that the reference stays on a page or use macros \ref and/or \pageref to build a valid reference.

• well, i use vref in textblocks, which are included in other commands. To use \ref and \pageref is not a solution, because sometimes the float object is on the same page and the text "see table \ref{tab:example} on page \pageref{tab:example}" is not what I want. – Micha Mar 25 '13 at 20:20
• well, I have this issue with automatic generated documents. Manually rephrase is a big botch in that case. But thanks for your comments – Micha Mar 25 '13 at 20:29
• Actually, it should be doable to have a "compile count", incremented after each run, and put in the aux file. After, say, 5-6 compilations, varioref should fall back to something "safe" (like \ref/\pageref). Of course, the counter should be resetted after any change - but this could be doable by reading in \jobname.tex and doing some kind of a checksum (which could also be put in the .aux file). – mbork Mar 25 '13 at 20:46

As remarked in this answer the issue is providing a satisfying automatic solution for boundary cases, e.g., if the source contains see the remark \vpageref{example} and the text generated by the \vpageref happens to fall onto a page break.

If varioref assumes the current page then you would end up with something like

see the remark on <next page>
the current page ...


which is obviously wrong, here

see the remark on <next page>
the previous page ...


would be ok. On the other hand if the break is slightly later the second solution would be wrong as well:

see the remark on the previous <next page>
page ...


One way out of the dilemma would be to tell varioref to use \reftextfaraway in such situations, so we would end up with

see the remark on <next page>
page 3 ...


The issue with this approach is that the text generated by \reftextfaraway is usually shorter than the text generated by \reftextcurrent, e.g.

see the remark on page 3
see the remark on the current page


As a consequence you may end up with a loop where the document oscillates between the two states, both being "wrong".

Detecting a loop from within TeX is next to impossible in my opinion (despites the comments above). For simple loops (with just two states, yes (in theory) but effectively you may end up with higher-level loops that oscillate through 3 or more states and then it gets really really complicated. The issue here is that the automatically generated text is non-linear, e.g., on each iteration the document may get longer in places and shorter in others.

One way out here would be to freeze the \reftextfarway approach whenever a reference falls onto a pagebreak once and never look back and reevaluateon the next run. The downside of this is obvious: you will end up with somewhat stange looking references if you're unluckly, like a pageref to the current page given as a number. As a minimum such an approach should only kick in after things like a TOC have been generated and just doing this safely is not easy (in short it is not clear that with a complex document even 5-6 runs are enough to stabilize).

A simple work around if you're still in the process of writing the document and want to compile it quickly, add \vrefwarning in you preamble, as indicated in the package doc p.5.