# Hyperlinks in beamer with allowframebreaks

In this answer Heiko Oberdiek showed me how to create anchors that could be later use to generate working hyperlinks in the beamer class. This works very well except in the case where the allowframebreaks option is given. To see this, consider the following MWE:

\documentclass{beamer}
\usepackage{lmodern}
\usepackage{lipsum}

\setbeamertemplate{caption}[numbered]

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

\begin{document}
\begin{frame}[allowframebreaks]
\frametitle{First figure}
\lipsum[3]
See Figure \hyperref[fig:first_figure]{\ref*{fig:first_figure}} and Figure
\hyperref[fig:second_figure]{\ref*{fig:second_figure}}.
\phantomtarget
\begin{figure}
\rule{6cm}{6cm}
\caption{First figure}
\label{fig:first_figure}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{Second figure}
\phantomtarget
\begin{figure}
\rule{6cm}{6cm}
\caption{Second figure}
\label{fig:second_figure}
\end{figure}
\end{frame}
\end{document}


The second figure is correctly hyperlinked but the first figure (which is in the second or "continuation" frame of a frame with the allowframebreaks option) is not correctly hyperlinked. How can this be fixed?

• @cyberSingularity Thanks! That seems to work. Is that the proper/general solution? – petobens Oct 2 '14 at 12:57

Basically you should put \phantomtarget just after the \begin{figure}, instead of just before that line.

### Explanation

The problem in your MWE is that the page break is occurring between the \phantomtarget and the \begin{figure}, so the target is on a different page to the figure itself.

In beamer, \begin{figure} doesn't really do much: it just sets the caption type, starts a new paragraph and a center environment, surrounded (importantly) by use of \nobreak.

Therefore, putting the \phantomtarget just inside the \begin{figure} means it is more likely that the target will remain together with the actual figure content, without a page break in between. There is certainly no disadvantage to moving the \phantomtarget inside the \begin{figure} in beamer. In general, whether or not this will completely solve the problem will depend on what else is inside the figure environment and whether it is breakable across pages etc.

### Fixed MWE

\documentclass{beamer}
\usepackage{lmodern}
\usepackage{lipsum}

\setbeamertemplate{caption}[numbered]

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

\begin{document}
\begin{frame}[allowframebreaks]
\frametitle{First figure}
\lipsum[3]
See Figure \hyperref[fig:first_figure]{\ref*{fig:first_figure}} and Figure
\hyperref[fig:second_figure]{\ref*{fig:second_figure}}.
\begin{figure}
\phantomtarget
\rule{6cm}{6cm}
\caption{First figure}
\label{fig:first_figure}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{Second figure}
\phantomtarget
\begin{figure}
\rule{6cm}{6cm}
\caption{Second figure}
\label{fig:second_figure}
\end{figure}
\end{frame}
\end{document}