# Hyperlinks (with autoref) color in beamer

Consider the following MWE

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

\setbeamertemplate{theorems}[numbered]

\begin{document}
\begin{frame}
\begin{theorem}
\label{thm:a_theorem}
$$\label{eq:an_equation} 2 + 2 = 4$$
\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).

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

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

\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
$$\label{eq:an_equation} 2 + 2 = 4$$
\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.

• 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. Sep 22 '14 at 19:42
• @HeikoOberdiek thanks for the reply and explanation. So is there a workaround that achieves the behavior mentioned in the question? Sep 22 '14 at 19:45

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}

\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
$$\label{eq:an_equation} 2 + 2 = 4$$
\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}


• I've posted a follow-up question to this problem here. I would really appreciate if you could look at it. Thanks! Oct 2 '14 at 3:47