TransWikia.com

autoref and braces around equation number

TeX - LaTeX Asked on April 11, 2021

I am writing an article for a journal with AMS math equation style, e.g. “Eq. (1)”. I want to hyperref the entire “Eq. (1)” and not merely the “1”. Currently I use renewcommand{equationautorefname}{Eq.}. But this yields an hyperref’ed “Eq. 1”. I prefer a solution in which I can continue using autoref.

A solution is presented at How to use the command autoref to implement the same effect when use the command eqref?, but it uses def and the journal style prohibits the use of Tex’s low-level commands like def, edef, and gdef.

3 Answers

If you're not willing -- or allowed! -- to modify some lower-level TeX macros, you could still achieve your objective of getting parentheses placed automatically around cross-referenced equation numbers by (a) executing the following instruction in the preamble:

usepackage[nameinlink,capitalize]{cleveref}

and (b) using cref instead of autoref to generate cross-references.

enter image description here

documentclass{article}
usepackage[colorlinks]{hyperref}
usepackage[nameinlink,capitalize]{cleveref}
setlengthtextwidth{3in} %% just for this example
begin{document}
begin{equation}label{eq:pythag}
a^2+b^2=c^2
end{equation}
A cross-reference to cref{eq:pythag} via verb+cref+.
end{document}

For more on various cross-referencing packages and, in particular, the capabilities of hyperref and cleveref in this regard, see the posting Cross-reference packages: which to use, which conflict?


Addendum: If you must use autoref and want parentheses placed around the cross-referenced equation numbers, it's necessary to redefine the macro theequation. Assuming you also use the amsmath package, it's also necessary to modify the auxilliary macro tagform@. The following MWE shows how this may be done. (The directives makeatletter and makeatother are needed because some of the code involves the "special" character @.)

A side remark: If the journal's guidelines prohibit the use of def in your document, using renewcommand is probably frowned upon as well. If that's the case, you should probably use cref instead of autoref to create cross-references to equations...

enter image description here

documentclass{article}
usepackage{amsmath}
makeatletter
letoldtheequationtheequation
renewcommandtagform@[1]{maketag@@@{ignorespaces#1unskip@@italiccorr}}
renewcommandtheequation{(oldtheequation)}
makeatother

usepackage[colorlinks]{hyperref}
renewcommand{equationautorefname}{Eq.}

usepackage[nameinlink,capitalize]{cleveref}
%% Need to undo the effect of redefinition of "theequation" to use cref:
creflabelformat{equation}{#2textup{#1}#3}  % No more parentheses around "#1"

setlengthtextwidth{3in} %% just for this example
begin{document}
begin{equation}label{eq:pythag}
a^2+b^2=c^2
end{equation}

A cross-reference to autoref{eq:pythag} via verb+autoref+.

A cross-reference to Eq.~eqref{eq:pythag} via verb+eqref+.

A cross-reference to cref{eq:pythag} via verb+cref+.
end{document}

Correct answer by Mico on April 11, 2021

The following example defines myeqref, which prepends the equation reference by Eq.~ and includes the prefix and the parentheses into the link. The prefix can be overwritten by the first optional argument.

The autoref name macros do not have access to the counter, thus the following definition of equationautorefname is just a dirty hack.

documentclass[a5paper]{article}
usepackage{amsmath}
usepackage{hyperref}
newcommand*{myeqref}[2][Eq.~]{%
  hyperref[{#2}]{#1(ref*{#2})}%
}
defequationautorefname#1#2null{%
  Eq.#1(#2null)%
}

begin{document}
begin{equation}
label{eq:einstein}
E=mc^2
end{equation}
References: myeqref{eq:einstein} and myeqref[equation~]{eq:einstein}.
Reference: autoref{eq:einstein}.
end{document}

Result

Answered by Heiko Oberdiek on April 11, 2021

A bit of a dirty hack, but this works as long as hyperref does not change its autoref implementation.

documentclass{article}

usepackage{hyperref}

makeatletter
  AtBeginDocument{%
    letplain@equationautorefnameequationautorefname
    defequationautorefname{plain@equationautorefname@autoref@insert@tagform}%
    def@autoref@insert@tagform~#1null{~(#1null)}%
  }
makeatother

begin{document}

begin{equation}
  label{foo}
  a + b = c
end{equation}

autoref{foo}

end{document}

MWE output

Answered by schtandard on April 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP