11

Say the equation is

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

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?

lockstep
  • 250,273
DKS
  • 1,309

3 Answers3

13

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}

\usepackage[colorlinks]{hyperref}

\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}

\begin{equation}
  \label{eq:circ}
  r^2 = x^2 + y^2.
\end{equation}

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

\end{document} 

Output

enter image description here

karlkoeller
  • 124,410
11

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}

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

\end{document}

output

5

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:

enter image description here

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

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

\Autoref{eq:1} shows us that \dots
\end{document}
Mico
  • 506,678