# Modifying \autoref to behave (somewhat) like \vref

I would like to modify the \autoref command in the following way: if the \autoref command and the label are on the same page, it behaves like \autoref* (i.e., no hyperlink). Else, it behaves usually.

Thanks, Vishnu

• varioref also has a \labelformat command, which is worth looking into. It adds names of section types (e.g. chapter, section, subsection ...) to the \vrefs output. May 17 '15 at 14:20

Here's a quick&dirty solution that works but throws strange error messages if a label is undefined. If you comment the \pagebreak out, then the link goes away.

\documentclass{article}

\usepackage{hyperref}

\makeatletter
\newcommand*{\aref}[1]{%
\edef\cur@page{\thepage}%
\edef\ref@page{\csname r@#1\endcsname}%
\edef\ref@page{\expandafter\@secondoffive\ref@page}%
\ifx\cur@page\ref@page
\autoref*{#1}%
\else
\autoref{#1}%
\fi }
\makeatother

\begin{document}

We have
$$\label{myeq} a=b.$$

\pagebreak

In~\aref{myeq} \dots

\end{document}


Similar to Hendrik Vogt's answer, but using the expandable \getpagerefnumber from refcount (which is part of the hyperref bundle anyway!) (I used his example, however!)

I've redefined \autoref to provide the feature instead of using a new macro name for this.

\documentclass{article}

\usepackage{xparse}%
\usepackage{blindtext}
\usepackage{letltxmacro}
\usepackage{hyperref}

\makeatletter
\LetLtxMacro\hyperref@@autoref\autoref%

\AtBeginDocument{%
\RenewDocumentCommand{\autoref}{sm}{%
\IfBooleanTF{#1}{%
\hyperref@@autoref*{#2}%
}{%
\edef\@currentpage{\thepage}%
\edef\pageoflabel{\getpagerefnumber{#2}}%
\ifx\@currentpage\pageoflabel
\hyperref@@autoref*{#2}%
\else
\hyperref@@autoref{#2}%
\fi
}%
}
}

\makeatother

\makeatother

\begin{document}

\section{First section}
We have in \autoref{myeq} some important stuff, but see \autoref{secondsection} too!
$$\label{myeq} a=b.$$

\clearpage

In~\autoref{myeq} \dots

\blindtext[5]

\section{Another section}\label{secondsection}

Some reference: \autoref{secondsection}

\blindtext[5]

\end{document}