1

Using a combination of the packages varioref, hyperref, cleverref and nameref we can get sophisticated cross references (technically 'references' to 'labels'), complete with hyperlinks.

For example, from a Minimum Working Example (MWE) as follows ...

\documentclass{article}

\usepackage{varioref}
\usepackage{hyperref}
\usepackage[nameinlink]{cleveref}
\usepackage{nameref}

\newcommand{\smartref}[1]{\nameref{#1} \vref{#1}}

\begin{document}

\pagebreak
\section{The Start}\label{theStartLabel}


\pagebreak
\section{Section Two}\label{sectionTwoLabel}


\pagebreak
\section{Varioref with hyperref and 
cleverref}\label{variorefWithCleverfAndHyperrefLabel}

Varioref with hyperref and 
cleverref:

See \vref{theStartLabel}\\
See \vref{sectionTwoLabel}\\
See \vref{variorefWithCleverfAndHyperrefLabel}\\
See \vref{theEndLabel}\\

Custom command using varioref, hyperref, cleverref and nameref:

See \smartref{theEndLabel}


\pagebreak
\section{The end}\label{theEndLabel}

\end{document}

... we get ...

enter image description here

How would we change the MWE in order to get the varioref command \vref and the custom command \smartref to have the hyperlink extend to the entire reference.

E.g. So we get ...

...
See section 4 on the next page
...
See The End section 4 on the following page
...

?

4

One solution is to use the \hyperref[label]{text} user macro from the hyperref pacakge.

The solving MWE ...

\documentclass{article}

\usepackage{varioref}
\usepackage{hyperref}
\usepackage[nameinlink]{cleveref}
\usepackage{nameref}

% Store \vref in a variable to enable redefining itself.
% Redefine \vref so the hyperlink is extended to the entire
% reference.
\let\vrefpointer\vref
\renewcommand{\vref}[1]{%
  \hyperref[#1]{\vrefpointer{#1}}%
}

% A custom command \smartref with the hyperlink extended to 
% the entire reference. 
% Plus some grammatic sugar (quote marks and comma)
\newcommand{\smartref}[1]{%
  \hyperref[#1]{\cref{#1}, ``\nameref{#1}", \vpageref{#1}}%
}


\begin{document}

\pagebreak
\section{The Start}\label{theStartLabel}


\pagebreak
\section{Section Two}\label{sectionTwoLabel}


\pagebreak
\section{Varioref with hyperref and 
cleverref}\label{variorefWithCleverfAndHyperrefLabel}

Varioref with hyperref and 
cleverref:

See \vref{theStartLabel}\\
See \vref{sectionTwoLabel}\\
See \vref{variorefWithCleverfAndHyperrefLabel}\\
See (\smartref{theEndLabel})\\

Custom command using varioref, hyperref, cleverref and 
nameref:

See \smartref{theEndLabel}\\

\pagebreak
\section{The end}\label{theEndLabel}

\end{document}

... which produces ... Minimum Working Example solution output.

I came across this solution from https://en.wikibooks.org/wiki/LaTeX/Hyperlinks#.5Chyperref rather than the package documentation. There may well be a more elegant solution (that, for example, exposes a better range of possible customizations) but this one works.

Update01: edited my custom \smartref command to include trailing comment characters (%) in order to prevent rogue spaces bracketing the command output.

Update02: edited solution MWE to have bracketed output See (\smartref{theEndLabel})\\ in order to make clear there are no rogue spaces at the endpoints of my custom \smartref command. Changed the output image to reflect.

Update03: end of line comment characters % to \vref renewcommand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.