11

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!

  • 1
    Would you please add a MWE? – egreg Apr 18 '12 at 13:04
  • 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
17

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

  • When you first load babel, then hyperref, this solution somehow does not work. For me, the the code within the \makeatletter commands given in this answer solved the problem. – Martin J.H. Sep 17 '15 at 15:49
  • @MartinJ.H.Package babel should be loaded before hyperref. And babel uses special language name dependent macros for setting up language related stuff. For example, something like the following is needed: \addto\extrasenglish{\def\equationautorefname~#1\null{Equation~(#1)\null}}. – Heiko Oberdiek Sep 17 '15 at 18:04
  • What's the \null used for? – nn0p Aug 22 '16 at 17:46
  • 1
    @nn0p It's the end marker for the argument. The second \null in the definition text just reinserts the token again, but it could also be omitted. – Heiko Oberdiek Aug 22 '16 at 17:48
  • Got it. Thanks for your quick response. @HeikoOberdiek – nn0p Aug 22 '16 at 17:50
9

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

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.

  • 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

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.