4

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.

  • some pdf viewer have the feature to go back to the slide the link came from – user36296 Feb 1 '18 at 17:49
  • Since you're fixing the hypertarget (the anchor) to sec:Appendix:A and jump from various positions to the same target, how should the hyperlink know to which other positions you want to jump back? Perhaps it is possible with some bare bone \pdf... and JavaScript wizardry – user31729 Feb 1 '18 at 17:49
  • @ChristianHupfer Well, as I wrote, it would be great if the \ReturnTo macro would be changed upon clicks on the corresponding button. – user121799 Feb 1 '18 at 17:51
  • 1
    @marmot: I don't think that macros can be redefined as soon as the pdf is shipped out – user31729 Feb 1 '18 at 17:55
  • 1
    @marmot: I have no knowledge in JavaScript, but if your request should work the only way would be JavaScript (if possible at all), as far as I know. AlexG might have some clue for this – user31729 Feb 1 '18 at 18:37
6

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}
  • 3
    That's amazing! – user121799 Feb 2 '18 at 14:10
6

Many pdf-Viewers have an menu option (normally with the shortcut Alt + <) to go back. You can try to call this menu with \Acrobatmenu. This here works for me with the adobe reader (the \ref are there only for the tests):

\documentclass{article}
\usepackage{hyperref}
\begin{document} 
\ref{xxx} page 1\newpage 
\ref{xxx} page 2\newpage 
\ref{xxx} page 3\newpage

\section{xxx}\label{xxx}
\Acrobatmenu{GoBack}{Go Back}
\end{document} 
  • Thanks! This code looks very nice but relies on acrobat. (My main problem with acrobat is that I did not manage to teach it to reload the document after a compilation.) And the go back button does not work elegantly if the appendix has several pages, meaning that one has to go back several times. SoI'd like to keep this question open for the moment. – user121799 Feb 1 '18 at 19:45
  • It works in sumatra too. So it much less relies on acrobat than e.g. a javascript solution. And regarding the "go back several times". How else should it work? You can either go back to a fix location (which you can implement with ref) or back "to where I came from". "Go back where I was ten minute ago" will not work ;-) – Ulrike Fischer Feb 1 '18 at 19:50
  • What I mean is that from say overlay 17 of the appendix I will have to 16 to 15 .... all the way to 1 and then back to the section where I came from. – user121799 Feb 1 '18 at 19:58
  • I know what you mean. But how should the viewer or latex or any code know that you want to go back exactly 16 slides? – Ulrike Fischer Feb 1 '18 at 20:06
  • I want to go back to the section I came from. I know that I could achieve this by placing several buttons at the end of the appendix, one for every button that refers to the appendix. In my example above this would mean one button for each section. It's just not very elegant. What I mean is that your suggestion seems to be really elegant if the appendix is very short. – user121799 Feb 1 '18 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy