6

I have modified the \eqref macro like this

\let\originaleqref\eqref
\renewcommand{\eqref}{equation~\originaleqref}

as per this answer. I want to have the reference be typeset like equation x or equation (x) and be completely clickable. This MWE illustrates what I want.

\documentclass[preview]{standalone}
\usepackage{amsmath}
\usepackage[linkcolor=blue,colorlinks=true]{hyperref}

\let\originaleqref\eqref
\renewcommand{\eqref}{equation~\originaleqref}

\begin{document}

\begin{equation}\label{eq:some_eq}
   a^2 + b^2 = c^2
\end{equation}

I want to refer to \eqref{eq:some_eq}.
Using \verb+\eqref+ should produce \hyperref[eq:some_eq]{equation 1}.
\end{document}

The result enter image description here

Is there a straightforward way to customise the typesetting of such references or do I need to completely redefine the \eqref macro?

oarfish
  • 1,419

2 Answers2

6

As mentioned by egreg, it's easy using cleveref (which must be loaded after hyperref).

Simply define

\crefformat{equation}{#2equation~#1#3}
\Crefformat{equation}{#2Equation~#1#3}

The arguments #2 and #3 are used to mark the beginning and end of the part of the cross-reference that forms the hyperlink when the hyperref package is used.

Then simply use \cref{eq:some_eq}.

MWE:

\documentclass[preview]{standalone}
\usepackage{amsmath}
\usepackage[linkcolor=blue,colorlinks=true]{hyperref}
\usepackage{cleveref}
\crefformat{equation}{#2equation~#1#3}
\Crefformat{equation}{#2Equation~#1#3}

\begin{document}

\begin{equation}\label{eq:some_eq}
   a^2 + b^2 = c^2
\end{equation}

I want to refer to \eqref{eq:some_eq}.
Using \verb+\eqref+ should produce \cref{eq:some_eq}.
\end{document} 

enter image description here

If you want \eqref to produce that output, simply add

\let\eqref\cref

and with this MWE

\documentclass[preview]{standalone}
\usepackage{amsmath}
\usepackage[linkcolor=blue,colorlinks=true]{hyperref}
\usepackage{cleveref}
\crefformat{equation}{#2equation~#1#3}
\Crefformat{equation}{#2Equation~#1#3}
\let\eqref\cref


\begin{document}

\begin{equation}\label{eq:some_eq}
   a^2 + b^2 = c^2
\end{equation}

I want to refer to \eqref{eq:some_eq}.
Using \verb+\eqref+ should produce \eqref{eq:some_eq}.
\end{document} 

you will obtain:

enter image description here

If you want instead the link to be equation (1) use

\crefformat{equation}{#2equation~(#1)#3}
\Crefformat{equation}{#2Equation~(#1)#3}

You can also modify the behavior for equation ranges with \crefrangeformat and \Crefrangeformat. Take a look at the cleveref manual for more info.

karlkoeller
  • 124,410
4

As Egreg mentions, you can use cleverref, if you do not want to you can use something similar to this.We use the fact that \ref* (aka non-link ref) in hyperref is the macro \@refstar. Thus inside the new \eqref we make the link via \hyperref[target]{text} and make sure that text itself, does not contain any hyperrefs (as nested hyperlinks does not really make any sense)

\documentclass[preview]{standalone}
\usepackage{amsmath}
\usepackage[linkcolor=blue,colorlinks=true]{hyperref}

\let\originaleqref\eqref % or \let\originaleqref\ref to drop parens
\makeatletter
\renewcommand{\eqref}[1]{%
  \begingroup%
  \let\ref\@refstar%
  \hyperref[#1]{%
    equation%
    ~\originaleqref{#1}%
  }%
  \endgroup
}
\makeatother

\begin{document}

\begin{equation}\label{eq:some_eq}
   a^2 + b^2 = c^2
\end{equation}

I want to refer to \eqref{eq:some_eq}.
Using \verb+\eqref+ should produce \hyperref[eq:some_eq]{equation 1}.
\end{document}
oarfish
  • 1,419
daleif
  • 54,450
  • For compatibility with IEEEeqnarray, this seems to be the superior solution. How would I go about removing the parentheses, if I wanted? – oarfish Aug 31 '15 at 12:28
  • Replace \original\eqref with \ref or \ref*, it is the original \eqref that adds the ()'s. I'd say that I would not have it write equation~num each and every time I referred to an equation. Usually we just use the plain \eqref or an edited version that makes the entire \eqref into a hyperlink. BTW: if this is for a journal, then be careful with redefinitions. It might be that you rewrites/redefinitions is against the journal style, and thus might need to be removed later on. – daleif Aug 31 '15 at 12:52
  • Thanks for the tip; it is not for a journal though. – oarfish Aug 31 '15 at 13:28