# Have varioref output a \nameref instead of a \ref

According to the varioref package documentation:

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.

I would like to use \vref but have it output a \nameref instead of a \ref. I don't think varioref provides an option for this; is it possible to achieve this in a simple way?

• You should take a look at at the cleveref package documentation (cleveref has to be loaded after varioref). – Bernard Jan 13 '17 at 11:38
• Please give a specific example of what you have in mind. – Mico Jan 13 '17 at 12:02
• Please give an example of a \nameref. – Mico Jan 13 '17 at 12:10
• \section{Introduction}\label{intro} [...] Please see section \nameref{intro} on page \pageref{intro} – Grodriguez Jan 13 '17 at 12:17
• @Mico \nameref is provided by the nameref package which is part of the hyperref bundle – clemens Mar 17 '17 at 15:03

I have achieved what I needed by defining a new command \xref as follows:

\newcommand{\xref}[1]{\nameref{#1} \vpageref[\unskip]{#1}}

This outputs a \nameref, and additionally a string similar to 'on page XX' if reference and \label are not on the same page.

You can patch the internal \vr@f (which should be rather safe I believe):

\documentclass{article}
\usepackage{varioref,nameref,etoolbox}

\makeatletter
% replace \ref with \nameref,
% \patchcmd is provided by the etoolbox package:
\patchcmd\vr@f{\ref}{\nameref}{}{}
\makeatother

\begin{document}

\section{Foo bar baz}\label{sec:foo}

\newpage

See section \vref{sec:foo}

\end{document}
• Save is relative, after all it is as you say an internal command and not an interface level one (and it will probably change with the next release, thoug with luck your patch is simple enough to survive). – Frank Mittelbach Jul 13 at 21:11