[Tex/LaTex] Dynamical links in beamer

beamerhyperref

Suppose I have an appendix in a beamer document, and I'd like to access it from various locations of the main part. Is there a way to make beamer remember where I came from?

To be more specific, consider the following snippet:

\documentclass{beamer}
\usepackage{lipsum}
\newcommand{\ReturnTo}{sec:2}
\begin{document}
\section{A section}\label{sec:1}
\begin{frame}
\frametitle{Some title}
\begin{overlayarea}{\textwidth}{\textheight}
\lipsum[1]\renewcommand{\ReturnTo}{sec:1}

\hfill\hyperlink{sec:AppendixA}{\beamergotobutton{details}}
\end{overlayarea}
\end{frame}
\section{Another section}\label{sec:2}
\begin{frame}
\frametitle{Another title}
\begin{overlayarea}{\textwidth}{\textheight}
\lipsum[2]\renewcommand{\ReturnTo}{sec:2}

\hfill\hyperlink{sec:AppendixA}{\beamergotobutton{details}}
\end{overlayarea}
\end{frame}
\section{And one more section}\label{sec:3}
\begin{frame}
\frametitle{And one more title}
\begin{overlayarea}{\textwidth}{\textheight}
\lipsum[3]\renewcommand{\ReturnTo}{sec:3}

\hfill\hyperlink{sec:AppendixA}{\beamergotobutton{details}}
\end{overlayarea}
\end{frame}
\appendix
\section{Some useful formulae}\label{sec:AppendixA}
\begin{frame}
\frametitle{Some useful formulae}
\begin{overlayarea}{\textwidth}{\textheight}
\begin{eqnarray*}
 F_{\mu\nu}F^{\mu\nu}&=& E^2+B^2\\
 E&=&m\,c^2\\
 \Rightarrow~F_{\mu\nu}F^{\mu\nu}&=& m^2\,c^4+B^2\quad ;-)
\end{eqnarray*}
\hfill\hyperlink{\noexpand\ReturnTo}{\beamergotobutton{back}}
\end{overlayarea}
\end{frame}
\end{document}

Here I want to be able to jump to the appendix from some of the sections, and go back to where I come from. Of course, the snippet does not achieve this, and I understand why. It would potentially work if I could teach beamer to redefine the \ReturnTo command upon clicking one of the \hyperlink{sec:AppendixA}{\beamergotobutton{details}} buttons.

It would be even nicer if I would not have to set the \ReturnTo by hand, but if it was set automatically to the current section. But I'd also like to keep the flexibility to set \ReturnTo by hand.

Notice that I am aware of the \againframe command. However, here I'd really like to enter an appendix, which is a separate section. And my presentation is so long that I cannot show all sections in the head line. Moreover, every section is long, and I may wish to really jump back to a specific slide/frame of a given section.

Best Answer

The solution that follows makes use of JavaScript for Acrobat (Reader) and hence only works in PDF viewers with a JavaScript engine.

We define

\hLink{<destination>}{<link text>}

which is used exactly like the original \hyperlink command from package hyperref. It is used to create the "Details ..." buttons that take you to the corresponding slides in the appendix.

However, before taking you to the linked destination by clicking the button, the current view is saved in a JavaScript object and pushed onto a stack of saved views.

The "Back" button in the top-left corner of the slides allows you to browse backwards through the history of visited slides where the "Details ..." buttons were clicked. Thus, you may surf away from the current appendix slide you have just jumped to, you may even click another "Details ..." button. Clicking the "Back" button takes you right back to where you clicked "Details ..." before, in reverse order.

\documentclass{beamer}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  \hLink{<destination>}{<link text>}
%
%  used like \hyperlink but saves the current view before jumping to
%  <destination>
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{pdfbase,atbegshi,amssymb,bm}
\ExplSyntaxOn
\newcommand\hLink[2]{
  \leavevmode
  \pbs_pdflink:nn{
    /Subtype/Link/Border[0~0~0]
    /A <<
      %first action: put current view on the stack of views
      /S/JavaScript/JS (
        if(typeof~viewStack==='undefined'){var~viewStack=new~Array();}
        viewStack.push(this.viewState);
      )
      %next: actual GoTo action
      /Next <</D (#1)/S/GoTo>>
    >>
  }{#2}
}
%backlink in the top-left slide corner
\AtBeginShipout{
  \AtBeginShipoutUpperLeftForeground{
    \fboxrule=0pt\fboxsep=1pt
    \raisebox{-\height}{\fbox{
      \pbs_pdflink:nn{
        /Subtype/Link/Border[0 0 0]
        /A <<
          /S/JavaScript/JS (
            try{this.viewState=viewStack.pop();}catch(e){}
          )
        >>
      }{\beamerbutton{$\bm\curvearrowleft$}}
    }}
  }
}
\ExplSyntaxOff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{kantlipsum}

\begin{document}

\part{My talk}
\frame{\partpage}

\begin{frame}{Some title}
  \kant[1]\hLink{frame:AppendixA}{\beamergotobutton{Details A}}
\end{frame}  

\begin{frame}{Another title}
  \kant[2]\hLink{frame:AppendixB}{\beamergotobutton{Details B}}
\end{frame}  

\begin{frame}{Yet another title}
  \kant[3]\hLink{frame:AppendixC}{\beamergotobutton{Details C}}
\end{frame}  

\begin{frame}{Something completely different}
  \kant[4]
\end{frame}  

\appendix
\frame{\partpage}

\begin{frame}{Appendix A}\label{frame:AppendixA}
  \begin{equation}
    \sqrt{16}=4
  \end{equation}
\end{frame}

\begin{frame}{Appendix B}\label{frame:AppendixB}
  \begin{equation}
    1+2=3
  \end{equation}
\end{frame}

\begin{frame}{Appendix C}\label{frame:AppendixC}
  \begin{equation}
    4<5 
  \end{equation}
\end{frame}
\end{document}
Related Question