14

For example, the first equation of Section 1 in a tex file is numbered in the form (1.1), When we use the commands \autoref and \eqref for referring it, the compiled pdf file will show Equation 1.1 and (1.1), respectively.

Now the problem is that how can I just use the command \autoref to implement the same effect when use the command \eqref.

By the way, please do not use the package cleveref!

N.N.
  • 36,163
user13637
  • 171
  • 1
    Would you please add a MWE? – egreg Apr 18 '12 at 13:04
  • 1
    Although you should know best of course, let me raise the inescapable style question. "(1.1)" is a way of saying "Equation 1.1", so "Equation (1.1)" looks clearly wrong to me. – Stephan Lehmke Apr 18 '12 at 15:52
  • 2
    @StephanLehmke: Would the following sentence meet with your approval? "As may be seen by the expressions given in equations 1 and 2, 3 out of 4 dentists recommend Brand-X tooth paste to their patients." To me, not placing parentheses around the first two numerals looks clearly wrong... – Mico Apr 18 '12 at 19:36

3 Answers3

23

Macro \autoref of package hyperref calls \<counter>autorefname followed by a unbreakable space ~ and the number. Finally \null is appended (some inheritance from LaTeX). This can be used to define \equationautorefname as macro that catches the equation number in a parameter.

\documentclass{article}
\usepackage{amsmath,hyperref}
\hypersetup{colorlinks=true}

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

\begin{document}
\begin{equation}\label{eq:pythagoras}
a^2+b^2=c^2\,.
\end{equation}
As may be seen in equation \eqref{eq:pythagoras} or, alternatively,
in \autoref{eq:pythagoras}, \ldots
\end{document}

Result with red links

Heiko Oberdiek
  • 271,626
10

Rather than fiddle with the definition of \autoref, you can use the \tagform@ command (provided by the amsmath package) to insert parentheses automatically around cross-referenced equation numbers. This works regardless of the cross-referencing command (\ref, \autoref, etc) you employ. The following MWE shows how this may be done:

\documentclass{article} 
\usepackage{amsmath,hyperref}
\hypersetup{colorlinks=true}

    % First, save the current form of `\theequation`
    \let\oldtheequation\theequation
    \makeatletter
    \def\tagform@#1{\maketag@@@{\ignorespaces#1\unskip\@@italiccorr}}
    \renewcommand{\theequation}{(\oldtheequation)}
    \makeatother 

\begin{document} 
\begin{equation}\label{eq:pythagoras}
a^2+b^2=c^2\,.
\end{equation}
Looking at equation \ref{eq:pythagoras}, equation \eqref{eq:pythagoras}, or 
\autoref{eq:pythagoras}, \ldots
\end{document} 

enter image description here

Mico
  • 506,678
  • This works great for regular equations, but for sub equations it changes it from (1.1a) to (1.1)a. Is there a way to get the subequation letter inside the parenthesis? – Astor Florida Jan 20 '20 at 22:37
  • @axsvl77 - Aside: Observe that I wrote this answer nearly eight years ago. For cross-referencing subequations, I wouldn't use the route laid out above. Instead, I would load the cleveref package and use its \cref and \labelcref directives. If you choose the cleveref-based approach, you wouldn't have to change the tagform@ macro. – Mico Jan 21 '20 at 12:54
  • Thanks. Perhaps I should ask a new question. – Astor Florida Jan 21 '20 at 13:44
3

I think that getting \autoref to put parentheses around equation numbers but not around section numbers, etc. would require a lot of hacking (I could be wrong though). However, you can create a new command \eqautoref, say, which can be used in place of \eqref.

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\numberwithin{equation}{section}
\makeatletter
\renewcommand\equationautorefname{\@gobble}
\makeatother
\newcommand\eqautoref[1]{(\autoref{#1})}
\begin{document}
\section{A section}
\begin{equation}
a = b \label{eqn}
\end{equation}
\eqautoref{eqn}
\end{document}

The \@gobble macro eliminates the space that would normally go between whatever \equationautorefname expands to ('Equation' by default) and the equation number.

Ian Thompson
  • 43,767
  • Will this new command preserve \autoref's "habit" of automatically inserting the appropriate label name (in this case, "Equation") before the number in question? – Mico Apr 18 '12 at 15:10
  • @Mico --- No. I thought the OP wanted hyperlinked references that looked the same as the output of \eqref. – Ian Thompson Apr 18 '12 at 15:18
  • Interesting -- I thought the OP's question was directed only at the automatic insertion of parentheses. You may be correct in your interpretation... – Mico Apr 18 '12 at 15:20