[Tex/LaTex] Hyperlinks (with autoref) color in beamer

beamerhyperref

Consider the following MWE

\documentclass[leqno]{beamer}
\usepackage{lmodern}

\hypersetup{colorlinks=true,allcolors=blue}
\setbeamertemplate{theorems}[numbered]

\begin{document}
\begin{frame}
  \begin{theorem}
  \label{thm:a_theorem}
    \begin{equation}
      \label{eq:an_equation}
      2 + 2 = 4
    \end{equation}
  \end{theorem}

Equation \ref{eq:an_equation} belongs to \ref{thm:a_theorem}.\\
Equation \eqref{eq:an_equation} belongs to \ref{thm:a_theorem}.\\
Equation \autoref{eq:an_equation} belongs to \autoref{thm:a_theorem}.
\end{frame}
\end{document}

I want i) the parentheses around equation number to be part of the hyperlink (and hence also be highlighted in blue) and ii) get \autoref to work (and hence obtain Equation (1) belongs to Theorem (1) with (1) in Theorem (1) also highlighted in blue).

enter image description here

Edit: Consider the following MWE which incorporates the suggested solution in the unique answer to this question:

\documentclass[leqno]{beamer}
\usepackage{lmodern}

\hypersetup{colorlinks=true,allcolors=blue}
\setbeamertemplate{theorems}[numbered]

\begin{document}
\begin{frame}
  Foo
\end{frame}
\begin{frame}
  \phantomsection
  \begin{theorem}
  \label{thm:a_theorem}
  Bar
  \end{theorem}
\end{frame}
\begin{frame}
  \phantomsection
  \begin{equation}
    \label{eq:an_equation}
    2 + 2 = 4
  \end{equation}
\end{frame}
\begin{frame}
  \hyperref[eq:an_equation]{Equation (\ref*{eq:an_equation})} belongs
  to \hyperref[thm:a_theorem]{Theorem \ref*{thm:a_theorem}}.
\end{frame}
\end{document}

The hyperlinks always point to the first frame and not to the corresponding equation or theorem.

Best Answer

Class beamer loads hyperref with option implicit=false. That means that the loading of hyperref is stopped earlier and important parts are not loaded. Thus the equations do not even generate anchors. Without anchor no link (at least not to the place, where it should go), without anchor name no \autoref.

The workaround consists of two parts:

  • Anchors are set with \phantomsection. The anchor names does not matter anyway, because \autoref is just a stub for \ref with the beamer settings. However, also \phantomsection is a dummy, an empty macro, therefore the example defines a \phantomtarget, which sets an anchor using \hypertarget.
  • \hyperref with optional argument is used to replace the plain reference number by the longer phrase with name and number, both inside the link. \ref* with star prevents an unnecessary nested link.

Full example:

\documentclass[leqno]{beamer}
\usepackage{lmodern}

\hypersetup{colorlinks=true,allcolors=blue}
\setbeamertemplate{theorems}[numbered]

\makeatletter
\newcounter{phantomtarget}
\renewcommand*{\thephantomtarget}{phantom.\the\value{phantomtarget}}
\newcommand*{\phantomtarget}{%
  \stepcounter{phantomtarget}%
  \hypertarget{\thephantomtarget}{}%
  \edef\@currentHref{\thephantomtarget}%
}
\makeatother

\begin{document}
\begin{frame}
  \phantomtarget
  \begin{theorem}
  \label{thm:a_theorem}
  \phantomtarget
    \begin{equation}
      \label{eq:an_equation}
      2 + 2 = 4
    \end{equation}
  \end{theorem}

\hyperref[eq:an_equation]{Equation (\ref*{eq:an_equation})} belongs
to \hyperref[thm:a_theorem]{Theorem \ref*{thm:a_theorem}}.
\end{frame}
\end{document}

Result

Related Question