The varioref package^{∗}

Frank Mittelbach

2001/09/04

Abstract This package defines the commands \vref, \vpageref, \vrefrange, and \vpagerefrange for L^{A}TEX 2ε. \vref is similar to \ref but adds an additional page reference, like ‘on the facing page’ or ‘on page 27’ whenever the corresponding \label is not on the same page. The command \vpageref is a variation to \pageref with a similar functionality. The \v...range commands take two labels as arguments and produce strings which depend on whether or not these labels fall onto a single page or on different pages. Generated strings are customizable so that these commands are usable with various languages.

1 Introduction

In many cases it is helpful when refering to a figure or table to put both a \ref and a \pageref command into the document especially when there are one or more pages between the reference and the object. Therefore some people use a command like

  \newcommand{\fullref}[1]{\ref{#1} on page~\pageref{#1}}

which reduces the number of key strokes, necessary to make such a complete reference. But since one never knows where the referenced object finally falls, using such a device may result in a page reference to the current page which is disturbing and therefore should be avoided.

2 The user interface

\vref The implementation of \vref below produces only a \ref when reference and \label are on the same page. It will additionally produce one of the strings ‘on the facing page’, ‘on the preceding page’, or ‘on the following page’, if label and reference differ by one and it will produce both \ref and \pageref when the difference is larger. The word ‘facing’ is used when label and reference both fall onto a double spread. However, if a special page numbering scheme is used instead of the usual arabic numbering (e.g., \pagenumbering{roman}) then there will be no distinction between one or many pages off.

∗This file has version number v1.3c, last revised 2001/09/04.

\vpageref Sometimes one wants to refer only to page number and again such a reference should normally be suppressed if we are refering to the current page. For this purpose the package defines the \vpageref command. It will produce the same strings as \vref except that it doesn’t start with the \ref and except that it will produce the string that is saved in \reftextcurrent if label and reference fall onto the same page. By defining \reftextcurrent to produce “on this page” or something similar, we can avoid that

   ... see the example \vpageref{ex:foo} which shows ...

comes out as “… see the example which shows … ”, which could be misleading.

You can put a space in front of \vpageref it will be ignored if the command doesn’t produce any text at all.

But in fact \vpageref allows even more control. If has two optional arguments. With the first one, one can specify the text that should be used if label and reference fall on the same page. This is very helpful if both are near to each other, so that they may or may not be separated by a page break. In such a case we usually know (!) whether the reference is before or after the label so that we can say something like

   ... see the example \vpageref[above]{ex:foo} which shows ...

which will then come out as “… see the example above which shows … ” if we are still on the same page, but as “… see the example on the page before which shows … ” (or something similar depending on the settings of the \reftext..before commands) when there was a page break in the meantime. One warning however, if you use \vpageref with the optional argument to refer to a figure or table, keep in mind that depending on the float placement parameters the float may show up on top of the current page and therefore before the reference even if it came after it in the source file.

But maybe you prefer to say “… see the above example” if example and reference fall onto the same page, i.e., reverse the word order. In fact, in some languages the word order automatically changes in that case. To allow for this variation the second optional argument can be used. It specifies the text preceding the generated reference if object and reference do not fall onto the same page. Thus one would write

   ... see the \vpageref[above example][example]{ex:foo}
       which shows ...

to achieve the desired effect.

2.1 Additions in 1998

\vrefrange This command is similar to \vref but it takes two mandatory arguments denoting a range to refer to (e.g., a sequences of figures or a sequence of equations, etc.). So if fig:a is your first figure in the sequence and fig:c your last you can write

   ... see figures \vrefrange{fig:a}{fig:c} ...

which would then be formatted as

… see figures 3.4 to 3.6 on pages 23–24 …

or, if they happen to all fall onto the next page, as

… see figures 3.4 to 3.6 on the following page …

i.e., the command is deciding what to say depending on where the two labels are placed in relation to each other; it is essentially implemented using \vpagerefrange described below. The optional argument the command may take is the text to use in case both labels are placed on the current page.

\vpagerefrange This command is similar to \vpageref but takes two mandatory arguments which are two labels denoting a range. If both labels fall onto the same page, the command acts exactly like \vpageref (with a single label), otherwise it produces something like “on pages 15–18” (see customization possibilities below). The optional argument it may take is the text to use in case both labels are placed on the current page.

\vrefpagenum This macro is provided to allow the user to write their own small commands which implement functions similar to those provided by the two previous commands. It takes two arguments: the second is a label (i.e., as used in \label or \ref) and the first is an arbitrary command name (make sure you use our own) that receives the page number related to this label. So if you have two (or more) labels you could retrieve their page numbers, compare them and then decide what to print. For example, the following not very serious definition (also using the ifthen package)

 \newcommand\amusingversion[2]{%
   \vrefpagenum\firstnum{#1}%
   \vrefpagenum\secondnum{#2}%
   the definition%
   \ifthenelse{\equal\firstnum\secondnum}%
     {s of \ref{#1} and \ref{#2} \vpageref{#1}}%
     { of \ref{#1} \vpageref{#1} and of \ref{#2} \vpageref{#2}}%
 }
 ...\amusingversion{foo}{bar}

will print something like

… the definitions of 3 and 4 on the previous page

in the case both labels are on the same page but something like

… the definition of 3 on the next page and of 4 on page 13

in case the are on different pages.

2.2 Additions in 2001

The user commands \vref, \vpageref, and \vpagerefrange all work by first removing any space on their left and then inserting some space of their own (\vref, for example, a nonbreakable space). That seemed like a good idea back then, but it has the disadvantage that you can’t use these macros in situations where you definitely do not want any space before the generated text. E.g., in situations like (\vref{foo} ...) you end up with a space after the open parenthesis.

\vref* Since it is to late to change the default behaviour I’ve added star versions

\vpageref*
\vpagerefrange*

of the macros which do not add any space before the generated text (they do nevertheless remove space at the left).

3 Customization

The package supports all options defined by the babel package to translate the fixed strings into other languages than English. (Some languages need updating, however.) It also supports languages currently not in babel; check the section on options later on. You can also modify some or all of the strings by redefining

\reftextbefore the following commands. Backward references use \reftextbefore if the label

\reftextfacebefore
\reftextafter
\reftextfaceafter

is on the preceding page but invisible and \reftextfacebefore if it is one the facing page (i.e., if the current page number is odd). Similarily \reftextafter is used when the label comes on the next page but one has to turn the page and \reftextfaceafter if it is on the following but facing page.

In fact, \reftextface... is used only if the user or the document class specified two-sided printing.

\reftextfaraway Finally we have \reftextfaraway which is used whenever label and reference differ by more than one or when they aren’t numeric. This macro is a bit different because it takes one argument, the symbolic reference string so that one cane make use of \pageref in its replacement text.

\vreftextvario To allow a bit random variation in the generated strings one can use the command \reftextvario inside the string macros. It takes two arguments and selects one or the other for printing depending on the number of already seens \vref or \vpageref commands. As an example see the definitions of \reftextbefore etc. on page 7.

3.1 Additions in 1998

The commands \vrefrange and \vpagerefrange produce their text using two macros described below. By redefining them one can modify the results to accommodate special requirements.

They both take two mandatory arguments denoting the first and the last label of the range.

\reftextpagerange This macro produces text that describes the page range of the two labels, e.g.,

the default for English is “on pages~\pageref{#1}--\pageref{#2}}”.

\reftextlabelrange This macro produces text that describes the range of figures, tables, or whatever the labels refer to, the default for English is “\ref{#1} to~\ref{#2}”.

4 Options

As mentioned above the package supports all standard options offered by the Babel system to customize the strings produced. In addition it offers the option draft to turn error messages into warnings during development. The defaul final produces error message when a generated string falls onto a page boundary (see next section).

5 A few warnings

Defining commands like the ones described above poses some interesting problems. Suppose, for example, that a generated text like ‘on the next page’ gets broken across pages. If this happens it is very difficult to find an acceptable solution and in fact can even result in a document that will always change from one state to another (i.e., inserting one string, finding that this is wrong, inserting another string on the next run which makes the first string correct again, inserting …). The current implementation of varioref therefore issues an error message whenever the generated text is broken across page boundaries, e.g.,

table 5 on the current ⟨page break⟩ page

would would result in an error, which needs to be resolved by the user by replacing the \vref command with an ordinary \ref just before the final run. This is not

\vrefwarning completely satisfactory but in such case no solution really is. During document preparation, while one is still changing the text, such error messages can be turned into warnings by placing a \vrefwarning command in the preamble. This is

\vrefshowerrors equivalent to specifying “draft” as an option to the package. \vrefshowerrors ensures that varioref stops when detecting a possible loop. This is the default and equivalent to specifying “final” as an option.

At the end final a warning: every use of \vref will internally generate two macro names to keep track of the string positions within the document. As a result you may run out of name space or main memory if you make heavy use of

\fullref this macro on a small TEX installation. For this reason the primitive command \fullref is also provided. This command can be used whenever you know for sure that label and reference can’t fall onto nearby pages.

6 The documentation driver file

The next bit of code contains the documentation driver file for TEX, i.e., the file that will produce the documentation you are currently reading. It will be extracted from this file by the docstrip program.

1 ⟨∗driver⟩

 2 \documentclass{ltxdoc}
 3 \usepackage{varioref}
 4 \GetFileInfo{varioref.sty}
 5 \setlength\hfuzz{1pt}    % ignore slight overfulls

 6 \EnableCrossrefs
 7 %\DisableCrossrefs   % Say \DisableCrossrefs if index is ready

8

 9 \RecordChanges       % Gather update information
10 %\OnlyDescription    % comment out for implementation details
11 \begin{document}
12 \DocInput{varioref.dtx}
13 \end{document}

14 ⟨/driver⟩

7 The implementation

The main implementation idea is to generate an internal label command for every \vpageref and compare the page reference of this label with the page reference of the user-requested label. Since this means one additional macro name for each use of \vpageref or vref (which is implemented in terms of \vpageref) the memory requirements of this package may be high in terms of main and macro name memory. Since the information held in the internal labels is used only once one could envision a different scheme if suitable primitives for this kind of “one place information” would be provided by L^{A}TEX. This type of data structure is planned for L^{A}TEX3 but for the moment we have to live with the memory restrictions.

We start by checking if this file was already loaded. If not we identify the current version. This is actually done at the top of the file, so we comment it out here

15 ⟨∗package⟩

16 %\NeedsTeXFormat{LaTeX2e}[1994/06/01]
17 %\ProvidesPackage{varioref}[\filedate\space\fileversion\space
18 %                     LaTeX2e package for extended references (FMi)]

\vref@addto To support the use of babel we want to add the additional strings to the \extras⟨language⟩ commands. Since \addto in the current implementation of babel has a bug that does not allow to use arguments containing hashmarks we do this by hand.

19 \def\vref@addto#1#2{%
20 #2%
21 \@temptokena{#2}%
22 \ifx#1\undefined
23  \edef#1{\the\@temptokena}%
24 \else
25  \toks@\expandafter{#1}%
26  \edef#1{\the\toks@\the\@temptokena}%
27 \fi
28 \@temptokena{}\toks@\@temptokena

29 }

30 \@onlypreamble\vref@addto

7.1 Options

\vref@excuse Excuse that we don’t know the strings for a certain language.

31 \def\vref@excuse#1{%
32 \PackageWarningNoLine{varioref}{Sorry, language `#1'
33  not supported%
34  \MessageBreak
35  in this version.
36  English strings used by default.
37  \MessageBreak
38  Please modify \protect\reftext... commands manually.%
39  \MessageBreak\MessageBreak
40  If you can suggest translations for
41  this language,%
42  \MessageBreak
43  please mail them
44  to the author of this package}}
45 \@onlypreamble\vref@excuse

\vref@stringwarning And we sometimes just don’t know some strings …

46 \def\vref@stringwarning#1{%
47 \PackageWarningNoLine{varioref}{Sorry, there is no proper
48  translation for the string\MessageBreak
49  produced by \protect#1\on@line.\MessageBreak
50  English string used instead.\MessageBreak
51  \MessageBreak
52  If you can suggest translations for
53  the current\MessageBreak
54  language, please mail them
55  to the author of this\MessageBreak package}}
\reftextfaceafter \reftextfacebefore \reftextafter \reftextbefore \reftextcurrent The options do set the the macros that generate the textual strings. Note, that

they do not start with a space, the space is already added in the main macro below.

56 \DeclareOption{american}
57 {\vref@addto\extrasamerican{%
58  \def\reftextfaceafter {on the \reftextvario{facing}{next} page}%
59  \def\reftextfacebefore{on the \reftextvario{facing}{preceding}
60  page}%
61  \def\reftextafter     {on the \reftextvario{following}{next} page}%
62  \def\reftextbefore    {on the \reftextvario{preceding page}{page
63  before}}%
64  \def\reftextcurrent   {on \reftextvario{this}{the current} page}%
65  \def\reftextfaraway#1{on page~\pageref{#1}}%
66  \def\reftextpagerange#1#2{on pages~\pageref{#1}--\pageref{#2}}%
67  \def\reftextlabelrange#1#2{\ref{#1} to~\ref{#2}}%
68 }}

The austrian defaults are the same as the german ones.

69 \DeclareOption{austrian}

70 {\vref@addto\extrasaustrian{%
71  \def\reftextfaceafter {auf der n\"achsten Seite}%
72  \def\reftextfacebefore{auf der vorherigen Seite}%
73  \let\reftextafter     \reftextfaceafter
74  \let\reftextbefore    \reftextfacebefore
75  \def\reftextcurrent   {auf dieser Seite}%
76  \def\reftextfaraway#1{auf Seite~\pageref{#1}}%
77  \def\reftextpagerange#1#2{auf Seiten~\pageref{#1}--\pageref{#2}}%
78  \def\reftextlabelrange#1#2{\ref{#1} bis~\ref{#2}}%
79 }}

The naustrian defaults are the same but needed for “Neue Rechtschreibung”.

80 \DeclareOption{naustrian}
81 {\vref@addto\extrasnaustrian{%
82  \def\reftextfaceafter {auf der n\"achsten Seite}%
83  \def\reftextfacebefore{auf der vorherigen Seite}%
84  \let\reftextafter     \reftextfaceafter
85  \let\reftextbefore    \reftextfacebefore
86  \def\reftextcurrent   {auf dieser Seite}%
87  \def\reftextfaraway#1{auf Seite~\pageref{#1}}%
88  \def\reftextpagerange#1#2{auf Seiten~\pageref{#1}--\pageref{#2}}%
89  \def\reftextlabelrange#1#2{\ref{#1} bis~\ref{#2}}%
90 }}

Text for brazil defaults was contributed by Alcino Dall Igna Junior

(adij@di.ufpe.br).
91 \DeclareOption{brazil}
92 {\vref@addto\extrabrazil{%
93  \def\reftextfaceafter {na \reftextvario{p\'agina oposta}{pr\'oxima
94  p\'agina}}%
95  \def\reftextfacebefore{na p\'agina \reftextvario{oposta}{anterior}}%
96  \def\reftextafter     {na \reftextvario{p\'agina seguinte}{pr\'oxima
97  p\'agina}}%
98  \def\reftextbefore{na p\'agina \reftextvario{anterior}{precedente}}%
99  \def\reftextcurrent   {\reftextvario{nesta p\'agina}{na p\'agina
100  corrente}}%
101  \def\reftextfaraway#1{na p\'agina~\pageref{#1}}%
102  \def\reftextpagerange#1#2{\vref@stringwarning\reftextpagerange
103  on pages~\pageref{#1}--\pageref{#2}}%
104  \def\reftextlabelrange#1#2{\vref@stringwarning\reflabelpagerange
105  \ref{#1} to~\ref{#2}}%

106 }}

Text for Breton defaults was contributed by Christian ROLLAND

(Christian.Rolland@univ-brest.fr).
107 \DeclareOption{breton}
108 {\vref@excuse{breton}%
109 \vref@addto\extrasbreton{%
110  \def\reftextfaceafter {war ar bajenn \reftextvario{a-dal}{da heul}}%
111  \def\reftextfacebefore{war ar bajenn \reftextvario{a-dal}{a-raok}}%
112  \def\reftextafter     {war ar bajenn da heul}%

113  \def\reftextbefore    {war ar bajenn a-raok}%
114  \def\reftextcurrent   {war ar \reftextvario{bajenn-ma\~n}%
115  {bajenn war-wel}}%
116  \def\reftextfaraway#1{war ar bajenn~\pageref{#1}}%
117  \def\reftextpagerange#1#2{\vref@stringwarning\reftextpagerange
118  on pages~\pageref{#1}--\pageref{#2}}%
119  \def\reftextlabelrange#1#2{\vref@stringwarning\reflabelpagerange
120  \ref{#1} to~\ref{#2}}%

121 }}

Text for catalan defaults was contributed by Robert Fuster

 (rfuster@mat.upv.es).
122 \DeclareOption{catalan}
123 {\vref@addto\extrascatalan{%
124  \def\reftextfaceafter {a la p\`agina seg\"uent}%
125  \def\reftextfacebefore{a la p\`agina anterior}%
126  \let\reftextafter     \reftextfaceafter
127  \let\reftextbefore    \reftextfacebefore
128  \def\reftextcurrent   {en aquesta p\`agina}%
129  \def\reftextfaraway#1{a la p\`agina~\pageref{#1}}%
130  \def\reftextpagerange#1#2{a les
131  p\`agines~\pageref{#1}--\pageref{#2}}%
132  \def\reftextlabelrange#1#2{\ref{#1} a~\ref{#2}}%

133 }}

134 \DeclareOption{croatian}
135 {\vref@excuse{croatian}%
136 \vref@addto\extrascroatian{%
137  \def\reftextfaceafter {on the \reftextvario{facing}{next} page}%
138  \def\reftextfacebefore{on the \reftextvario{facing}{preceding}
139  page}%
140  \def\reftextafter     {on the \reftextvario{following}{next} page}%
141  \def\reftextbefore    {on the \reftextvario{preceding page}{page
142  before}}%
143  \def\reftextcurrent   {on \reftextvario{this}{the current} page}%
144  \def\reftextfaraway#1{on page~\pageref{#1}}%
145  \def\reftextpagerange#1#2{on pages~\pageref{#1}--\pageref{#2}}%
146  \def\reftextlabelrange#1#2{\ref{#1} to~\ref{#2}}%

147 }}

Defaults for the Czech option provided by Milan Vancura <milan@suse.cz>.

148 \DeclareOption{czech}
149 {%\vref@excuse{czech}%
150 \vref@addto\extrasczech{%
151  \def\reftextfaceafter {na n\'a\-sle\-du\-j\'\i c\'\i\ stran\v{e}}%
152  \def\reftextfacebefore{na p\v{r}ed\-choz\'\i\ stran\v{e}}%
153  \def\reftextafter     {na n\'a\-sle\-du\-j\'\i c\'\i\ stran\v{e}}%
154  \def\reftextbefore    {na p\v{r}ed\-choz\'\i\ stran\v{e}}%
155  \def\reftextcurrent   {na t\'eto stran\v{e}}%
156  \def\reftextfaraway#1{na stran\v{e}~\pageref{#1}}%
157  \def\reftextpagerange#1#2{na stran\'ach~\pageref{#1}\--\pageref{#2}}%
158  \def\reftextlabelrange#1#2{\ref{#1} a\v{z}~\ref{#2}}%

159 }}

Defaults for Danish provided by Torsten Martinsen (tmar91@kom.auc.dk). Further defaults by Ole Laursen (olau@hardworking.dk).

160 \DeclareOption{danish}
161 {\vref@addto\extrasdanish{%
162  \def\reftextfaceafter {p\aa{} \reftextvario{modst\aa ende}%
163  {n\ae ste} side}%
164  \def\reftextfacebefore{p\aa{} \reftextvario{modst\aa ende}%
165  {forrige} side}%
166  \def\reftextafter     {p\aa{} \reftextvario{den f\o lgende}%
167  {n\ae ste} side}%
168  \def\reftextbefore    {p\aa{} \reftextvario{forrige side}
169  {foreg\aa ende side}}%
170  \def\reftextcurrent   {p\aa{} denne side}%
171  \def\reftextfaraway#1{p\aa{} side~\pageref{#1}}%
172  \def\reftextpagerange#1#2{p\aa{} side~\pageref{#1}--\pageref{#2}}%
173  \def\reftextlabelrange#1#2{\ref{#1} til~\ref{#2}}%

174 }}

Default string for dutch have been contributed by Frank Poppe (POPPE@SWOV.NL). This option currently supports one additional string macro \refpagename so that you can easily change to bladzijde instead of pagina if you prefer this word for “page”. However, I will not guarantee that this will survive future versions of this package, so use it on your own risk (you can always update the full strings to be on the safe side).

175 \DeclareOption{dutch}
176 {\vref@addto\extrasdutch{%
177  \def\refpagename{pagina}%
178  \def\reftextfaceafter {op de \reftextvario{rechter \refpagename}%
179  {\refpagename\ hiernaast}}%
180  \def\reftextfacebefore{op de \reftextvario{linker \refpagename}%
181  {\refpagename\ hiernaast}}%
182  \def\reftextafter     {op de \reftextvario{volgende \refpagename}%
183  {\refpagename\ hierna}}%
184  \def\reftextbefore    {op de \reftextvario{vorige \refpagename}%
185  {\refpagename\ hiervoor}}%
186  \def\reftextcurrent   {op deze \refpagename}%
187  \def\reftextfaraway#1{op \refpagename~\pageref{#1}}%
188  \def\reftextpagerange#1#2{op pagina's~\pageref{#1} t/m~\pageref{#2}}%
189  \def\reftextlabelrange#1#2{\ref#1 t/m~\ref{#2}}%

190 }}

191 \DeclareOption{english}
192 {\vref@addto\extrasenglish{%
193  \def\reftextfaceafter {on the \reftextvario{facing}{next} page}%
194  \def\reftextfacebefore{on the \reftextvario{facing}{preceding}
195  page}%
196  \def\reftextafter     {on the \reftextvario{following}{next} page}%
197  \def\reftextbefore    {on the \reftextvario{preceding page}{page

198  before}}%
199  \def\reftextcurrent   {on \reftextvario{this}{the current} page}%
200  \def\reftextfaraway#1{on page~\pageref{#1}}%
201  \def\reftextpagerange#1#2{on pages~\pageref{#1}--\pageref{#2}}%
202  \def\reftextlabelrange#1#2{\ref{#1} to~\ref{#2}}%

203 }}

204 \DeclareOption{esperanto}
205 {\vref@excuse{esperanto}%
206 \vref@addto\extrasesperanto{%
207  \def\reftextfaceafter {on the \reftextvario{facing}{next} page}%
208  \def\reftextfacebefore{on the \reftextvario{facing}{preceding}
209  page}%
210  \def\reftextafter     {on the \reftextvario{following}{next} page}%
211  \def\reftextbefore    {on the \reftextvario{preceding page}{page
212  before}}%
213  \def\reftextcurrent   {on \reftextvario{this}{the current} page}%
214  \def\reftextfaraway#1{on page~\pageref{#1}}%
215  \def\reftextpagerange#1#2{on pages~\pageref{#1}--\pageref{#2}}%
216  \def\reftextlabelrange#1#2{\ref{#1} to~\ref{#2}}%

217 }}

The finnish strings were suggested by Matti Rintala (bitti@cs.tut.fi).

218 \DeclareOption{finnish}
219 {\vref@addto\extrasfinnish{%
220  \def\reftextfaceafter {\reftextvario{viereisell\"a}%
221  {seuraavalla} sivulla}%
222  \def\reftextfacebefore{\reftextvario{viereisell\"a}%
223  {edellisell\"a} sivulla}%
224  \def\reftextafter     {seuraavalla sivulla}%
225  \def\reftextbefore    {edellisell\"a sivulla}%
226  \def\reftextcurrent   {t\"all\"a sivulla}%
227  \def\reftextfaraway#1{sivulla~\pageref{#1}}%
228  \def\reftextpagerange#1#2{\vref@stringwarning\reftextpagerange
229  on pages~\pageref{#1}--\pageref{#2}}%
230  \def\reftextlabelrange#1#2{\vref@stringwarning\reflabelpagerange
231  \ref{#1} to~\ref{#2}}%

232 }}

French defaults are provided by Daniel Flippo (Daniel.Flipo@univ-lille1.fr).

233 \DeclareOption{french}
234 {\vref@addto\extrasfrench{%
235  \def\reftextfaceafter {page \reftextvario{ci-contre}{suivante}}%
236  \def\reftextfacebefore{page \reftextvario{ci-contre}%
237  {pr\'ec\'edente}}%
238  \def\reftextafter     {page suivante}%
239  \def\reftextbefore    {page pr\'ec\'edente}%
240  \def\reftextcurrent   {de la pr\'esente page}%
241  \def\reftextfaraway#1{page~\pageref{#1}}%
242  \def\reftextpagerange#1#2{pages~\pageref{#1}--\pageref{#2}}%

243  \def\reftextlabelrange#1#2{\ref{#1} \`a~\ref{#2}}%

244 }}

Galician defaults are provided by Matthias Moebius

 (Matthias.Moebius@uni-konstanz.de).
245 \DeclareOption{galician}
246 {\vref@excuse{galician}%
247 \vref@addto\extrasgalician{%
248  \def\reftextfaceafter {na seguinte p\'axina}%
249  \def\reftextfacebefore{na p\'agina anterior}%
250  \let\reftextafter     \reftextfaceafter
251  \let\reftextbefore    \reftextfacebefore
252  \def\reftextcurrent   {nesta p\'axina}%
253  \def\reftextfaraway#1{na p\'axina~\pageref{#1}}%
254  \def\reftextpagerange#1#2{\vref@stringwarning\reftextpagerange
255  on pages~\pageref{#1}--\pageref{#2}}%
256  \def\reftextlabelrange#1#2{\vref@stringwarning\reflabelpagerange
257  \ref{#1} to~\ref{#2}}%

258 }}

There are no good variants for German (I think and still think but this is a matter of taste :-). The following definitions were recently suggested to me but since the original are in for a long time i don’t want to change them now since that could make a lot of documents change their formatting. If you fancy them, add a redefinition of the corresponding macro(s) to the preamble of your document.

     \def\reftextfaceafter {auf der \reftextvario
           {gegen\"uberliegenden}{anderen} Seite}%
     \def\reftextfacebefore {auf der \reftextvario
           {gegen\"uberliegenden}{anderen} Seite}%
     \def\reftextafter     {auf der \reftextvario
           {n\"achsten}{folgenden} Seite}%
     \def\reftextbefore    {auf der \reftextvario
           {vorigen}{vorhergehenden} Seite}%
     \def\reftextcurrent   {\reftextvario
           {auf dieser}{diese} Seite}%
259 \DeclareOption{german}
260 {\vref@addto\extrasgerman{%
261  \def\reftextfaceafter {auf der n\"achsten Seite}%
262  \def\reftextfacebefore{auf der vorherigen Seite}%
263  \let\reftextafter     \reftextfaceafter
264  \let\reftextbefore    \reftextfacebefore
265  \def\reftextcurrent   {auf dieser Seite}%
266  \def\reftextfaraway#1{auf Seite~\pageref{#1}}%
267  \def\reftextpagerange#1#2{auf Seiten~\pageref{#1}--\pageref{#2}}%
268  \def\reftextlabelrange#1#2{\ref{#1} bis~\ref{#2}}%

269 }}

The ngerman defaults are the same but needed for “Neue Rechtschreibung” (i.e., a different set of hyphenation patterns).

270 \DeclareOption{ngerman}
271 {\vref@addto\extrasngerman{%
272  \def\reftextfaceafter {auf der n\"achsten Seite}%
273  \def\reftextfacebefore{auf der vorherigen Seite}%
274  \let\reftextafter     \reftextfaceafter
275  \let\reftextbefore    \reftextfacebefore
276  \def\reftextcurrent   {auf dieser Seite}%
277  \def\reftextfaraway#1{auf Seite~\pageref{#1}}%
278  \def\reftextpagerange#1#2{auf Seiten~\pageref{#1}--\pageref{#2}}%
279  \def\reftextlabelrange#1#2{\ref{#1} bis~\ref{#2}}%

280 }}

Defaults for greek suggested by Apostolos Syropoulos

 (apostolo@obelix.ee.duth.gr).
281 \DeclareOption{greek}
282 {\vref@addto\extrasgreek{%
283  \def\reftextfaceafter {sthn \reftextvario{paro'usa}%
284  {ep'omenh} sel'ida}%
285  \def\reftextfacebefore{sthn \reftextvario{paro'usa}{prohgo'umenh}
286  sel'ida}%
287  \def\reftextafter     {sthn ep'omenh sel'ida}%
288  \def\reftextbefore    {sthn prohgo'umenh sel'ida}%
289  \def\reftextcurrent   {s'' aut'h th sel'ida}%
290  \def\reftextfaraway#1{sth sel'ida\nobreakspace\pageref{#1}}%
291  \def\reftextpagerange#1#2{stis sel'ides\nobreakspace
292  \pageref{#1}---\pageref{#2}}%
293  \def\reftextlabelrange#1#2{\ref{#1} ws\nobreakspace\ref{#2}}%

294 }}

Defaults for Italian suggested by Giovanni Pensa (pensa@dsi.unimi.it) with iTEXnici.

295 \DeclareOption{italian}
296 {\vref@addto\extrasitalian{%
297  \def\reftextfaceafter {\reftextvario{a fronte}%
298  {nella pagina successiva}}%
299  \def\reftextfacebefore{\reftextvario{a fronte}%
300  {nella pagina precedente}}%
301  \def\reftextafter     {nella pagina \reftextvario{seguente}%
302  {successiva}}%
303  \def\reftextbefore    {nella pagina precedente}%
304  \def\reftextcurrent   {in questa pagina}%
305  \def\reftextfaraway#1{a pagina~\pageref{#1}}%
306  \def\reftextpagerange#1#2{\vref@stringwarning\reftextpagerange
307  on pages~\pageref{#1}--\pageref{#2}}%
308  \def\reftextlabelrange#1#2{\vref@stringwarning\reflabelpagerange
309  \ref{#1} to~\ref{#2}}%

310 }}

Defaults for Hungarian by Jeff Goldberg (J.goldberg@Cranfield.ac.uk). There is a problem with the use of the definite article a or az in Hungarian before expansions of \vref or \ref. The problem is that the the word should be az if the number following would begin with a vowel if spelled out, and a if the number would begin with a consonant.

The option assumes that there is a command \aza defined which helps resolving this problem.^{1}

311 \DeclareOption{magyar}
312 {\vref@addto\extrasmagyar{%
313  \def\reftextfaceafter {a k\"o\-vet\-kez\H{o} ol\-da\-lon}%
314  \def\reftextfacebefore{az el\H{o}\-z\H{o} ol\-da\-lon}%
315  \def\reftextafter     {a k\"o\-vet\-kez\H{o} ol\-da\-lon}%
316  \def\reftextbefore    {az el\H{o}\-z\H{o} ol\-da\-lon}%
317  \def\reftextcurrent   {ezen az ol\-da\-lon}%
318  \def\reftextfaraway#1{\aza{\pageref{#1}} \pageref{#1}.~ol\-da\-lon}%
319  \def\reftextpagerange#1#2{\vref@stringwarning\reftextpagerange
320  on pages~\pageref{#1}--\pageref{#2}}%
321  \def\reftextlabelrange#1#2{\vref@stringwarning\reflabelpagerange
322  \ref{#1} to~\ref{#2}}%
323  \AtBeginDocument{\providecommand\aza[1]{a/az%
324  \PackageError{varioref}{%
325  Proper definition for command \noexpand\aza missing}%
326  {You need to (re)define the command \noexpand\aza that
327  takes one argument (a number or a \noexpand\pageref command)
328  and produces the string `a' or `az' depending on the argument
329  value. It should also accept an optional argument (default `l')
330  to denote whether or not the string should be lowercase or
331  uppercase.}}}%

332 }}

The Norwegian and ‘nynorsk’ translations have been provided by Dag F Langmyhr

 <dag@ifi.uio.no> and Karl Ove Hufthammer <huftis@bigfoot.com>.
333 \DeclareOption{norsk}
334 {\vref@addto\extrasnorsk{%
335  \def\reftextfaceafter {p\aa{} neste side}%
336  \def\reftextfacebefore{p\aa{} forrige side}%
337  \def\reftextafter     {p\aa{} neste side}%
338  \def\reftextbefore    {p\aa{} forrige side}%
339  \def\reftextcurrent   {p\aa{} denne siden}%
340  \def\reftextfaraway#1{p\aa{} side~\pageref{#1}}%
341  \def\reftextpagerange#1#2{p\aa{} side~\pageref{#1}--\pageref{#2}}%
342  \def\reftextlabelrange#1#2{\ref{#1} til~\ref{#2}}%

343 }}

344 \DeclareOption{nynorsk}
345 {\vref@addto\extrasnynorsk{%
346  \def\reftextfaceafter {p\aa{} neste side}%
347  \def\reftextfacebefore{p\aa{} f\o{}rre side}%
348  \def\reftextafter     {p\aa{} neste side}%
349  \def\reftextbefore    {p\aa{} f\o{}rre side}%
350  \def\reftextcurrent   {p\aa{} denne sida}%

1This information is actually quite old but unfortunately never made it into varioref. In case any work has been undertaken to resolve this please inform the author about it.

351  \def\reftextfaraway#1{p\aa{} side~\pageref{#1}}%
352  \def\reftextpagerange#1#2{p\aa{} side~\pageref{#1}--\pageref{#2}}%
353  \def\reftextlabelrange#1#2{\ref{#1} til~\ref{#2}}%

354 }}

The polish translations have been provided by Tomasz Michniewski <tomekm@ias.wat.waw.pl>. The use of the command \eob and aob etc. requires appropriate definitions as provided, for example, by the babel system. This should probably be handled differently but it would require modifications in babel’s language support.

355 \DeclareOption{polish}
356 {\vref@addto\extraspolish{%
357  \def\reftextfaceafter {na \reftextvario{s\aob{}siedniej}%
358  {nast\eob{}pnej} stronie}%
359  \def\reftextfacebefore{na \reftextvario{s\aob{}siedniej}%
360  {poprzedniej} stronie}%
361  \def\reftextafter     {na nast\eob{}pnej stronie}%
362  \def\reftextbefore    {na poprzedniej stronie}%
363  \def\reftextcurrent   {na \reftextvario{tej
364  samej}{bie\zkb{}\aob{}cej} stronie}%
365  \def\reftextfaraway#1{na \pageref{#1}~stronie}%
366  \def\reftextpagerange#1#2{na stronach~\pageref{#1}--\pageref{#2}}%
367  \def\reftextlabelrange#1#2{od~\ref{#1} do~\ref{#2}}%

368 }}

The portuguese default text has been provided by José Carlos Oliveira Santos

 jcsantos@fc.up.pt.
369 \DeclareOption{portuges}
370 {\vref@addto\extraportuges{%
371  \def\reftextfaceafter {na \reftextvario{p\'agina ao lado}{pr\'oxima
372  p\'agina}}%
373  \def\reftextfacebefore{na p\'agina \reftextvario{ao lado}{anterior}}%
374  \def\reftextafter     {na \reftextvario{p\'agina seguinte}{pr\'oxima
375  p\'agina}}%
376  \def\reftextbefore{na p\'agina \reftextvario{anterior}{precedente}}%
377  \def\reftextcurrent   {nesta p\'agina}%
378  \def\reftextfaraway#1{na p\'agina~\pageref{#1}}%
379  \def\reftextpagerange#1#2{nas p\'aginas~\pageref{#1}--\pageref{#2}}%
380  \def\reftextlabelrange#1#2{\ref{#1} a~\ref{#2}}%

381 }}

382 \DeclareOption{romanian}
383 {\vref@excuse{romanian}%
384 \vref@addto\extrasromanian{%
385  \def\reftextfaceafter {on the \reftextvario{facing}{next} page}%
386  \def\reftextfacebefore{on the \reftextvario{facing}{preceding}
387  page}%
388  \def\reftextafter     {on the \reftextvario{following}{next} page}%
389  \def\reftextbefore    {on the \reftextvario{preceding page}{page
390  before}}%
391  \def\reftextcurrent   {on \reftextvario{this}{the current} page}%

392  \def\reftextfaraway#1{on page~\pageref{#1}}%
393  \def\reftextpagerange#1#2{on pages~\pageref{#1}--\pageref{#2}}%
394  \def\reftextlabelrange#1#2{\ref{#1} to~\ref{#2}}%

395 }}

The text for russian was taken from the book in Russian by I. A. Kotelnikov and P. Z. Chebotaev, and adapted to the encoding-independent form by Vladimir

 Volovich (vvv@vvv.vsu.ru).
396 \DeclareOption{russian}
397 {\vref@addto\extrasrussian{%
398  \def\reftextfaceafter{\cyrn\cyra\ \reftextvario{\cyrp\cyrr\cyro
399  \cyrt\cyri\cyrv\cyro\cyrp\cyro\cyrl\cyro\cyrzh\cyrn\cyro\cyrishrt}%
400  {\cyrs\cyrl\cyre\cyrd\cyru\cyryu\cyrshch\cyre\cyrishrt}\ \cyrs\cyrt
401  \cyrr\cyra\cyrn\cyri\cyrc\cyre}%
402  \def\reftextfacebefore{\cyrn\cyra\ \reftextvario{\cyrp\cyrr\cyro
403  \cyrt\cyri\cyrv\cyro\cyrp\cyro\cyrl\cyro\cyrzh\cyrn\cyro\cyrishrt}%
404  {\cyrp\cyrr\cyre\cyrd\cyrery\cyrd\cyru\cyrshch\cyre\cyrishrt}\
405  \cyrs\cyrt\cyrr\cyra\cyrn\cyri\cyrc\cyre}%
406  \def\reftextafter{\cyrn\cyra\ \cyrs\cyrl\cyre\cyrd\cyru\cyryu\cyrshch
407  \cyre\cyrishrt\ \cyrs\cyrt\cyrr\cyra\cyrn\cyri\cyrc\cyre}%
408  \def\reftextbefore{\cyrn\cyra\ \reftextvario{\cyrp\cyrr\cyre\cyrd
409  \cyrsh\cyre\cyrs\cyrt\cyrv\cyru\cyryu\cyrshch\cyre\cyrishrt}%
410  {\cyrp\cyrr\cyre\cyrd\cyrery\cyrd\cyru\cyrshch\cyre\cyrishrt}%
411  \ \cyrs\cyrt\cyrr\cyra\cyrn\cyri\cyrc\cyre}%
412  \def\reftextcurrent{\cyrn\cyra\ \reftextvario{\cyrerev\cyrt\cyro
413  \cyrishrt}{\cyrd\cyra\cyrn\cyrn\cyro\cyrishrt}\ \cyrs\cyrt\cyrr
414  \cyra\cyrn\cyri\cyrc\cyre}%
415  \def\reftextfaraway#1{\cyrn\cyra\ \cyrs.~\pageref{#1}}%
416  \def\reftextpagerange#1#2{\cyrn\cyra\ \cyrs\cyrt\cyrr\cyra\cyrn
417  \cyri\cyrc\cyra\cyrh~\pageref{#1}--\pageref{#2}}%
418  \def\reftextlabelrange#1#2{\cyrs~\ref{#1}\ \cyrp\cyro~\ref{#2}}%

419 }}

420 \DeclareOption{slovak}
421 {\vref@excuse{slovak}%
422 \vref@addto\extrasslovak{%
423  \def\reftextfaceafter {on the \reftextvario{facing}{next} page}%
424  \def\reftextfacebefore{on the \reftextvario{facing}{preceding}
425  page}%
426  \def\reftextafter     {on the \reftextvario{following}{next} page}%
427  \def\reftextbefore    {on the \reftextvario{preceding page}{page
428  before}}%
429  \def\reftextcurrent   {on \reftextvario{this}{the current} page}%
430  \def\reftextfaraway#1{on page~\pageref{#1}}%
431  \def\reftextpagerange#1#2{on pages~\pageref{#1}--\pageref{#2}}%
432  \def\reftextlabelrange#1#2{\ref{#1} to~\ref{#2}}%

433 }}

434 \DeclareOption{slovene}
435 {\vref@excuse{slovene}%
436 \vref@addto\extrasslovene{%
437  \def\reftextfaceafter {on the \reftextvario{facing}{next} page}%

438  \def\reftextfacebefore{on the \reftextvario{facing}{preceding}
439  page}%
440  \def\reftextafter     {on the \reftextvario{following}{next} page}%
441  \def\reftextbefore    {on the \reftextvario{preceding page}{page
442  before}}%
443  \def\reftextcurrent   {on \reftextvario{this}{the current} page}%
444  \def\reftextfaraway#1{on page~\pageref{#1}}%
445  \def\reftextpagerange#1#2{on pages~\pageref{#1}--\pageref{#2}}%
446  \def\reftextlabelrange#1#2{\ref{#1} to~\ref{#2}}%

447 }}

Text for spanish defaults was contributed by Julio Sanchez (jsanchez@gmv.es).

448 \DeclareOption{spanish}
449 {\vref@addto\extrasspanish{%
450  \def\reftextfaceafter {en la p\'agina siguiente}%
451  \def\reftextfacebefore{en la p\'agina anterior}%
452  \let\reftextafter     \reftextfaceafter
453  \let\reftextbefore    \reftextfacebefore
454  \def\reftextcurrent   {en esta p\'agina}%
455  \def\reftextfaraway#1{en la p\'agina~\pageref{#1}}%
456  \def\reftextpagerange#1#2{on pages~\pageref{#1}--\pageref{#2}}%
457  \def\reftextlabelrange#1#2{\ref{#1} to~\ref{#2}}%

458 }}

The text for swedish was contributed by Mats Dahlgren (matsd@sssk.se).

459 \DeclareOption{swedish}
460 {\vref@addto\extrasswedish{%
461  \def\reftextfaceafter {p\aa\ \reftextvario{motst\aa ende}%
462  {n\"{a}sta} sida}%
463  \def\reftextfacebefore{p\aa\ \reftextvario{motst\aa ende}%
464  {f\"{o}reg\aa ende} sida}%
465  \def\reftextafter     {p\aa\ \reftextvario{f\"{o}ljande}%
466  {n\"{a}sta} sida}%
467  \def\reftextbefore    {p\aa\ f\"{o}reg\aa ende sida}%
468  \def\reftextcurrent   {p\aa\ denna sida}%
469  \def\reftextfaraway#1{p\aa\ sidan~\pageref{#1}}%
470  \def\reftextpagerange#1#2{p\aa\
471  sidorna~\pageref{#1}--\pageref{#2}}%
472  \def\reftextlabelrange#1#2{\ref{#1} till~\ref{#2}}%

473 }}

474 \DeclareOption{turkish}
475 {\vref@excuse{turkish}%
476 \vref@addto\extrasturkish{%
477  \def\reftextfaceafter {on the \reftextvario{facing}{next} page}%
478  \def\reftextfacebefore{on the \reftextvario{facing}{preceding}
479  page}%
480  \def\reftextafter     {on the \reftextvario{following}{next} page}%
481  \def\reftextbefore    {on the \reftextvario{preceding page}{page
482  before}}%
483  \def\reftextcurrent   {on \reftextvario{this}{the current} page}%
484  \def\reftextfaraway#1{on page~\pageref{#1}}%

485  \def\reftextpagerange#1#2{on pages~\pageref{#1}--\pageref{#2}}%
486  \def\reftextlabelrange#1#2{\ref{#1} to~\ref{#2}}%

487 }}

We support francais as an alternative to french since people might be used to it, but we discurrage it.

488 \DeclareOption{francais}{%
489 \typeout{Please use `french' instead of `francais'}%
490 \ExecuteOptions{french}}
491 \DeclareOption{germanb}{%
492 \typeout{Please use `german' instead of `germanb'}%
493 \ExecuteOptions{german}}

During document preparation errors due to \vref are usually of not much interest. For this reason the draft option turns them into warnings by issuing a \vrefwarning command. The final option (default) stops with an error message when varioref detects a possible loop.

494 \DeclareOption{draft}{\AtEndOfPackage\vrefwarning}
495 \DeclareOption{final}{\AtEndOfPackage\vrefshowerrors} % the default

After all options are declared we execute the default option english to set up the defaults and then call \ProcessOptions* to handle any option passed to the package. The star form is used since we want to execute the options in the order they are given (in case this isn’t used together with the babel package).

496 \ExecuteOptions{english,final}
497 \ProcessOptions*

7.2 Defining the main macros

\vr@cnt To generate new labels we use a counter and construct the internal label names by prefixing the current counter value with the string vr@. We need a L^{A}TEX counter to get \includes handled correctly.

498 \newcounter{vrcnt}

\vpageref \vp@geref The command \vpageref generates the appropriate string by first parsing the optional arguments (if any) and then compares the internal and the user label. This command should be robust since the user might put it anywhere.

We now start by scanning for a star which means: do not add a space in front of the generated text—ever.

499 \DeclareRobustCommand\vpageref{\@ifstar
500  {\let\vref@space\relax\vp@geref}%
501  {\let\vref@space\space\vp@geref}}
502 \def\vp@geref{\@ifnextchar[%

The default for the first optional argument is the string hidden in the macro

\reftextcurrent.
503 \@vpageref{\@vpageref[\reftextcurrent]}%

504 }

\@vpageref More parsing…

505 \def\@vpageref[#1]{\@ifnextchar[%

The default for the second optional argument is a space which is prefixed by \unskip to get rid of any leading space inserted already.

506 {\@@vpageref{#1}}{\@@vpageref{#1}[\unskip\vref@space]}}

\@@vpageref The \@@vpageref macro finally generates the references by comparing the value of an internal label with the value of the user label.

507 \def\@@vpageref#1[#2]#3{%

First it switches to horizontal mode if necessary and also removes any leading space.

508 \leavevmode\unskip

Then it advances the counter \c@vrcnt by one which is used to generate internal label names.

509 \global\advance\c@vrcnt\@ne

Then it checks if for the current value of \c@vrcnt a label command was issued in the last run. If not it pretends that there was one with the value {??}{??}. Thereafter it stores the the pageref value for this label in the macro \@tempa.

510 \vref@pagenum\@tempa{\the\c@vrcnt @vr}%

Here we check that the generated text is not going across a page boundary.

511 \vref@pagenum\@tempb{\the\c@vrcnt @xvr}%
512 {\let\@currentlabel\@empty

In version 1.0l the label string has been turned around so that the number comes first. This was done to allow easy explicit expansion of the number before it is passed to the \label command. In the babel system the argument of label was not expanded with the result that wrong label strings have been generated. This is a general problem that might need a completely different solution in babel but for now the change below (and in some other places in the code) should solve the problem for this special combination of packages.

513 \expandafter\label\expandafter{\the\c@vrcnt @xvr}}%

Inside displays of the amsmath package the \label command is redefined which makes the test for loops incorrect if a \vref is used in, say, \intertext. So we test this condition first and only do the test if \label doesn’t have a special meaning.

514 \ifx\label\label@in@display\else
515  \ifx\@tempa\@tempb\else

If both points do not fall onto the same page with either issue an error or a warning message.

516  \vref@err{\noexpand\vref at page boundary
517  \@tempb-\@tempa\space (may loop)%

518 }%

519  \fi
520 \fi

Same game for the user requested label; this time the page reference is saved in

\@tempb.
521 \vrefpagenum\@tempb{#3}%

Now after the internal label has served its purpose if would be nice to free the memory it occupies by using something like

 \global\expandafter\let
        \csname r@\the\c@vrcnt @vr\endcsname\@empty

But this is not possible because it would result in getting ‘Labels may have changed… ’ warnings for every run. Now we are ready to produce the textual strings. Since we have removed any leading space we now insert a space (unless this was the star form of the command) and then compare the two page references.

522 \vref@space
523 \ifx\@tempa\@tempb

If they are the same we insert the first argument into the input stream. Otherwise we insert the second argument. Recall that those are the two optional arguments of .... However, if the user explicitly requested an empty text string using the optional argument we cancel any preceeding space.

524  \def\@tempc{#1}%
525  \ifx\@tempc\@empty
526  \unskip
527  \else
528  #1%
529  \fi
530 \else
531  #2%

Now we check if the page number of the referenced object (stored in \@tempb is a single positive number.

532  \is@pos@number\@tempb

533 {%

If so, we check if the current position (stored in \@tempa) is a positive number. If this is the case we assign this number to the counter register \@tempcnta and add one to it.

534  \is@pos@number\@tempa
535  {\@tempcnta\@tempa
536  \advance\@tempcnta\@ne

537 }%

If it is not a positive number we assign the largest possible number to \@tempcnta and thereby pretending that label and reference are miles away from each other.

538  {\@tempcnta\maxdimen}%

Now we are ready to check if reference and object are on nearby pages. \@tempb will expand to the page number of the object (and we know that this is a number) and \@tempcnta is either one higher than the reference page or completely out of bounds. So if both represend the same value then the object lies one page after its reference.

539  \ifnum \@tempb =\@tempcnta

Thus if the object falls onto an odd page then the reference is on the facing even page (and so we insert \reftextfaceafter), otherwise the object can not be seen from the reference (and we in insert \reftextafter). Don’t be surprised if we are not using \@tempb in the check. Since \@tempcnta has the same value it is faster to use the register instead of parsing the macro contents anew.

540  \ifodd\@tempcnta

In fact we are going to use \reftextfaceafter only if we are doing two-sided printing, otherwise \reftextafter is always used. Since the value of \if@twoside is evaluated before reading in packages we could do better (saving some tokens) by defining the current macro in dependence of this boolean.

541  \if@twoside
542  \reftextfaceafter
543  \else
544  \reftextafter
545  \fi
546  \else
547  \reftextafter
548  \fi

If the object is not on the page following the reference we check if it is on the page before the reference. In L^{A}TEX this situation is not too common, for example with floats it normally does not occur, but of course it isn’t impossible if you more than one reference to the same object, or if you have back references to sections, theorems, etc. To test this we now substract two from the current value of \@tempcnta (which was set to one higher as the reference page number). Note, that substraction is also possible if the value was \maxdimen — we still get something that is much larger than any sensible page number.

549  \else
550  \advance\@tempcnta-2

If now \@tempb and \@tempcnta have the same value then the object lies one page before the reference.

551  \ifnum \@tempb =\@tempcnta

Again we have to check for odd or even pages to distinguish between the facing and the non-facing situation.

552  \ifodd\@tempcnta
553  \reftextbefore
554  \else

Again the \reftextfacebefore only if @twoside evaluates to true.

555  \if@twoside
556  \reftextfacebefore
557  \else
558  \reftextbefore
559  \fi
560  \fi

If the above test also returns false then we have object and references on pages which are far away or don’t contain simple page numbers. Therefore we generate the \reftextfaraway string. Recall that this is a macro which has the user label as an argument.

561  \else
562  \reftextfaraway{#3}%
563  \fi
564  \fi

565 }%

We do the same if our first test (that the page with the object has a positive page number) turns out to be false.

566  {\reftextfaraway{#3}}%
567 \fi

Finally we generate the internal label so that it can be check on the next run. This means that we compare the position after the string with the position of the referenced object. There is one thing to note: to conserve space we locally make \@currentlabel empty since we are only interested in the page number value of this internal label.

568 {\let\@currentlabel\@empty

The setting of \df@label is a concession to the amsmath package which might redefine \label and expects this macro to be empty in certain circumstances.

569 \let\df@label\@empty
570 \expandafter\label\expandafter{\the\c@vrcnt @vr}}%

571 }

\vrefwarning \vrefshowerrors \vref@err When the \vref command detects a possible crossing over a page boundary it will call \vref@err to generate an error message. During document preparation one can turn such errors into warnings by issuing a \vrefwarning declaration.

572 \def\vrefwarning{\def\vref@err{\PackageWarning{varioref}}}
573 \def\vrefshowerrors{\def\vref@err##1{\PackageError{varioref}{##1}%
574  {Please check the pages in question. You might need to replace
575  the \string\vref\MessageBreak
576  or \noexpand\vpageref by a normal \noexpand\(page)ref to
577  stop LaTeX running forever.}}}

\vref We start by scanning for a star form in which case we do not put any space before the generated text (only remove some).

\vr@f
578 \DeclareRobustCommand\vref{\@ifstar
579 {\let\vref@space\relax\vr@f}%
580 {\let\vref@space\nobreakspace\vr@f}}

And now we do the real work…

581 \def\vr@f#1{%

The use of\nobreakspace or ~ after the \unskip means that this command will always produce a normal space while something like \nobreak\space will react to settings of \nonfrenchspacing. Unfortunately the latter will also act in cases where one really wants a normal space, e.g., in Fig.~\vref{..}, resulting in a large extra space after the dot. For this reason the first solution is used.

582 \leavevmode\unskip\vref@space
583 \ref{#1}%
584 \@vpageref[\unskip]{#1}}

\is@pos@number The utility macro \is@pos@number takes three arguments: a string that is tested for being a valid integer and the actions to be taken in case the test comes out true or false.

We start by passing the string to the macro \is@pos@num after prefixing it with a 0 and adding a space after it. To have a well defined ending point we also add \@nil at the end.

585 \def\is@pos@number#1{\is@pos@num0#1\space\@nil}

Now we parse the enlarged string into a counter register. To get control back after everything that is regarded by TEX as a number is put into that register we assign \is@pos@num@ to the \afterassignment token. Since the string started with 0, we can be sure that the register assignment will be carried out without an error message. In case of counter assignments leading zeros are discarded.

586 \def\is@pos@num{\afterassignment\is@pos@num@ \count@}

Now we have to check whether or not the whole string was parsed into that register or some remainder was left over. Since we have added a \@nil token at the very end we can use that to delimit the argument of \is@pos@num@. Note, that the added space in \is@pos@number above gets parsed away by the counter assignment. If it would be missing, and the full string would consist of a number, TEXwould try to replace \@nil by its definition to see if it would contain additional digits and thus we would be in trouble at this point.

587 \def\is@pos@num@#1\@nil{%

Now we have to check if the argument is empty. We can do this with an \if test (i.e., in an expandable way) since we know for sure that it can not start with a digit

588 \if0#10%

Depending on the result we execute the second or third argument of the command \is@pos@number using a technique that removes the \else or \fi from the input stream first, so that in principle input stream parsing could be done from within the arguments.

589  \expandafter\@firstoftwo \else
590  \expandafter\@secondoftwo  \fi}

\reftextvario This macro gives a little bit of random variation in the text because the outcome depends on the number of \vref commands seen before.

591 \def\reftextvario#1#2{\ifodd\c@vrcnt #1\else#2\fi}

\fullref And here is the primitive command that always produces a \ref and a \pageref.

592 \def\fullref#1{\ref{#1} \reftextfaraway{#1}}

7.2.1 Supporting ranges

\vref@pagenum This command takes a label as #1 extracts the page number associated with it and saves the result in a csname which is passed as #1. If the label isn’t associates with a page number it essentially associates it with ?? for both the label number and the page, thus defining the label this way. This means that no label warning is generated for this label in case it is undefined.

593 \def\vref@pagenum#1#2{%
594 \@ifundefined{r@#2}{\@namedef{r@#2}{{??}{??}}}{}%
595 \edef#1{\expandafter\expandafter\expandafter
596  \@cdr\csname r@#2\endcsname
597  \@nil}%

598 }

\vrefpagenum Same as the above but this time we generate a warning for undefined labels.

599 \def\vrefpagenum#1#2{%
600 \@ifundefined{r@#2}%
601  {\G@refundefinedtrue
602  \@latex@warning{Reference `#2' on page \thepage \space
603  undefined}}{}%
604 \vref@pagenum{#1}{#2}}

\vpagerefrange \@vpagerefrange \@svpagerefrange If both labels refer to the same page then we produce a \vpageref. Otherwise we

produce the result of \reftextpagerange.

One could think of making this command even more clever by producing a special string if the difference between the two page number is one (but this is not done).

We need two versions to implement a star form:

605 \newcommand\@svpagerefrange[3][\reftextcurrent]{%
606 \vrefpagenum\@tempa{#2}%
607 \vrefpagenum\@tempb{#3}%
608 \ifx\@tempa\@tempb
609  \vpageref*[#1]{#2}%
610 \else
611  \reftextpagerange{#2}{#3}%
612 \fi

613 }

And the same but using \vpageref:

614 \newcommand\@vpagerefrange[3][\reftextcurrent]{%
615 \vrefpagenum\@tempa{#2}%
616 \vrefpagenum\@tempb{#3}%
617 \ifx\@tempa\@tempb
618  \vpageref[#1]{#2}%
619 \else

620  \reftextpagerange{#2}{#3}%
621 \fi

622 }

And here we put things together.

623 \newcommand\vpagerefrange{\@ifstar\@svpagerefrange\@vpagerefrange}
\vrefrange
624 \newcommand\vrefrange[3][\reftextcurrent]
625 {\reftextlabelrange{#2}{#3} \vpagerefrange[#1]{#2}{#3}}

626 ⟨/package⟩