6

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.

AlexG
  • 54,894
  • some pdf viewer have the feature to go back to the slide the link came from – samcarter_is_at_topanswers.xyz Feb 01 '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 –  Feb 01 '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. –  Feb 01 '18 at 17:51
  • @samcarter Yes, but I don't think that works if I have a long appendix. Moreover, in the presentation mode, these buttons are not theres. (Of course I know that I could put many buttons in my appendix, but this is not too elegant.) –  Feb 01 '18 at 17:54
  • 1
    @marmot: I don't think that macros can be redefined as soon as the pdf is shipped out –  Feb 01 '18 at 17:55
  • @ChristianHupfer Not even with the tricks of the media9 package (which I unfortunately don't understand at all)? That is, could a Java script do the trick? –  Feb 01 '18 at 18:35
  • 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 –  Feb 01 '18 at 18:37
  • @marmot: Apparently AlexG was in chat recently. Perhaps you can ping him there... –  Feb 01 '18 at 18:43
  • Ok , I will try something with JavaScript. (Need some time. Hope it will work.) – AlexG Feb 02 '18 at 09:11
  • Done. And it works ;-). – AlexG Feb 02 '18 at 12:12

2 Answers2

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} 
Ulrike Fischer
  • 327,261
  • 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. –  Feb 01 '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 01 '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. –  Feb 01 '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 01 '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. –  Feb 01 '18 at 20:09
  • Then imho you should consider to insert copies of your appendix - for every section from which you want to jump one. Then you can use different buttons. (A javascript solution that stores the section you can from is probably possible too, but imho makes you rather viewer dependant.) – Ulrike Fischer Feb 01 '18 at 21:09
  • Thanks for your input. I will leave the question open for a while and, if no other solution comes up, accept your answer. Most likely I'll solve the problem in a different way, namely by adding targets to the back button during computation. –  Feb 01 '18 at 23:03
  • Why not placing an \Acrobatmenu{GoBack}{Go Back} on every slide, say, into the footer, e. g. as part of the navigation buttons. It would allow you to jump back from everywhere to the previous view. – AlexG Feb 02 '18 at 08:10
  • @AlexG The problem is that when you jump from a section to the begin of the appendix and then go there through e.g. 5 slides then the section is no longer the "previous view". – Ulrike Fischer Feb 02 '18 at 08:15
  • I see, then a bit of AI is needed to achieve this. – AlexG Feb 02 '18 at 08:43
5

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}
AlexG
  • 54,894