# Combining \autoref and \eqref

Say the equation is

$$r^2 = x^2 + y^2 \text{.} \label{eq:circ}$$


Now, if I use

\autoref{eq:circ}


I get Equation 1. How do I get Equation (1) (the parenthesis that we get while using \eqref) while still maintaining the hyperlink?

Edited in response to an answer below.

I have defined two versions of the command to be used at the beginning and in the middle of a sentence:

\def\chapterautorefname{Chap.}
\def\sectionautorefname{Sec.}
\def\subsectionautorefname{sub--Sec.}
\def\figureautorefname{Fig.}
\def\tableautorefname{Tab.}
\def\equationautorefname{Eq.}

\newcommand{\Autoref}[1]{%
\begingroup%
\def\chapterautorefname{Chapter}%
\def\sectionautorefname{Section}%
\def\subsectionautorefname{Sub--Section}%
\def\figureautorefname{Figure}%
\def\tableautorefname{Table}%
\def\equationautorefname~#1{Equation~(#1)}%
\autoref{#1}%
\endgroup%
}


While using \Autoref it says

Use of \equationautorefname doesn't match its definition \Autoref{eq:llg1}.

How do I fix it?

I found this trick in an old document of mine, but I have a feeling that the idea comes from someone else:

\documentclass{article}

\usepackage{hyperref}

\def\equationautorefname~#1\null{Equation~(#1)\null}

\begin{document}

$$\label{eq:circ} r^2 = x^2 + y^2.$$
\autoref{eq:circ}

\end{document}


• Comment too long. Adding it as a post.
– DKS
Jan 11 '14 at 11:33

The trick described in Svend Tveskæg's answer comes from this answer of Heiko Oberdiek.

If you want it to work with your settings (the command \Autoref) you have to use in this way:

\newcommand{\Autoref}[1]{%
\begingroup%
\def\chapterautorefname~##1\null{Chapter~(##1)\null}%
\def\sectionautorefname~##1\null{Section~(##1)\null}%
\def\subsectionautorefname~##1\null{Sub--Section~(##1)\null}%
\def\figureautorefname~##1\null{Figure~(##1)\null}%
\def\tableautorefname~##1\null{Table~(##1)\null}%
\def\equationautorefname~##1\null{Equation~(##1)\null}%
\autoref{#1}%
\endgroup%
}


Complete MWE

\documentclass{article}

\def\chapterautorefname~#1\null{Chap.~(#1)\null}
\def\sectionautorefname~#1\null{Sec.~(#1)\null}
\def\subsectionautorefname~#1\null{sub--Sec.~(#1)\null}
\def\figureautorefname~#1\null{Fig.~(#1)\null}
\def\tableautorefname~#1\null{Tab.~(#1)\null}
\def\equationautorefname~#1\null{Eq.~(#1)\null}

\newcommand{\Autoref}[1]{%
\begingroup%
\def\chapterautorefname~##1\null{Chapter~(##1)\null}%
\def\sectionautorefname~##1\null{Section~(##1)\null}%
\def\subsectionautorefname~##1\null{Sub--Section~(##1)\null}%
\def\figureautorefname~##1\null{Figure~(##1)\null}%
\def\tableautorefname~##1\null{Table~(##1)\null}%
\def\equationautorefname~##1\null{Equation~(##1)\null}%
\autoref{#1}%
\endgroup%
}

\begin{document}

$$\label{eq:circ} r^2 = x^2 + y^2.$$

\Autoref{eq:circ} some text \autoref{eq:circ}.

\end{document}


Output

In order to get \autoref to insert parentheses around an equation number automatically, i.e., to make it behave like \eqref, you could also do the following (which assumes that the amsmath package is loaded): Modify the macro \tagform@ of the amsmath package:

\let\origtheequation\theequation
\makeatletter
\def\tagform@#1{\maketag@@@{\ignorespaces#1\unskip\@@italiccorr}}
\makeatother
\renewcommand{\theequation}{(\origtheequation)}


Note that because of the presence of @ characters, the redefinition of \tagform@ has be be wrapped in a \makeatletter ... \makeatother pair. The macro \eqref will continue to work as before, by the way.

A full MWE -- observe that it's not necessary to fiddle any further with the definition of \Autoref:

\documentclass{article}
\usepackage{amsmath}
\let\origtheequation\theequation
\makeatletter
\def\tagform@#1{\maketag@@@{\ignorespaces#1\unskip\@@italiccorr}}
\makeatother
\renewcommand{\theequation}{(\origtheequation)}
\def\equationautorefname{Eq.}
\newcommand{\Autoref}[1]{%
\begingroup%
\def\equationautorefname{Equation}%
\autoref{#1}%
\endgroup%
}
\setlength{\textwidth}{3in} % just for this example
\begin{document}
$$\label{eq:1} a^2+b^2=c^2.$$

Here's a reference to \autoref{eq:1}.

\Autoref{eq:1} shows us that \dots
\end{document}

• With dvips and ps2pdf the link in the pdf becomes#equation.(1)%%. Because of the appended %% the links does not work in firefox preview. For dvipdfm it is not a problem. Aug 6 '20 at 19:54