[Tex/LaTex] How to cross-reference to sections in beamer

beamercross-referencing

For my lecture slides in beamer I would like to establish a section-framenumber numbering scheme. For this purpose I reset frame counter in \AtBeginSection and redefine the \insertframenumber command to reflect the new numbering scheme:

\renewcommand{\insertframenumber}{\thesection-\theframenumber}
\AtBeginSection{\setcounter{framenumber}{0}}

This works pretty well. However, I have not succeeded in getting cross-references right in this respect, that is to refer to some labeled \frame[label=interesting] as section-framenumber. Apparently, there is no way to, given the label, extract the section number:

\documentclass{beamer}

\renewcommand{\insertframenumber}{\thesection-\theframenumber}
\setbeamertemplate{headline}[text line]{This is frame: \insertframenumber}
\AtBeginSection{\setcounter{framenumber}{0}}

\begin{document}

\section{One}
\begin{frame}{First Frame in Section One}
  Slide content\par
  \pause
  More content
\end{frame}
\begin{frame}[label=interesting]{Second Frame in Section One -- The one to crossref to}
  Slide content
\end{frame}

\section{Two}
\begin{frame}{First Frame in Section Two}

  Now I want to crossref to interesting slide by section-framenumber (that is, somehow generate \textbf{1-2}) 
  \begin{itemize}
    \item \textbackslash{}ref yields: \ref{interesting}, apparently that is the frame number
    \item \textbackslash{}page yields: \pageref{interesting}, apparently that is the PDF page number
    \item \textbackslash{}autoref yields: \autoref{interesting}, apparently that is the frame number again
    \item nothing there that yields \textbackslash{}thesection of our target label?
  \end{itemize}
\end{frame}

\end{document}

I took a look at the generated .aux-file. I am not really good in deciphering .aux-files; however, as I read it, beamer does not even store there the section number (1 in this case) the label belongs to:

...
\newlabel{interesting<1>}{{2}{3}{One\relax }{Doc-Start}{}}
...

So am I out of luck here?

Edit: I am now considering looking into the zref package to solve my problem. The challenge will probably be to get it integrated with the label=mylabel frame option, which implicitly generates those helpful "overlay" labels (mylabel<1>, mylabel<2>, ...) to refer to individual slides of a frame.

Any hints or ideas still are more than welcome 🙂

Best Answer

The label key of the beamer frame uses the \label<> macro internally. This uses the normal \label macro of LaTeX which uses the \@currentlabel macro for the label text. This macro is set by \refstepcounter which is used by sectioning macros, captions and also frames. It uses the content of the corresponding \the... macro, so setting \theframenumber, not \insertframenumber is important.

Note that \insertframenumber is by default set to \@arabic\c@framenumber which is the lower-level version of \arabic{framenumber}, i.e. the default definition of \theframenumber. So my first step was to redefine \theframenumber and make \insertframenumber use it:

\renewcommand{\theframenumber}{\thesection-\arabic{framenumber}}
\renewcommand{\insertframenumber}{\theframenumber}

However, this doesn't work. After some debugging I found out that actually the subsectionslide counter is used for the label key. I have no idea why. Maybe the \refstepcounter{subsectionslide} is processed after the \refstepcounter{framenumber}. The related file is beamerbaseframe.sty. It doesn't like it in the code, but I don't had time to follow all the conditionals in it.

Anyway, the solution is to also(?) set \thesubsectionslide to \thesection-\arabic{framenumber} or \theframenumber. Then the references are displayed as you want. However, references to the subsectionslide (whatever this is exactly) might be wrong.

\documentclass{beamer}

\makeatletter
\renewcommand{\insertframenumber}{\theframenumber}
\renewcommand{\theframenumber}{\thesection-\arabic{framenumber}}
\renewcommand{\thesubsectionslide}{\thesection-\arabic{framenumber}}
\setbeamertemplate{headline}[text line]{This is frame: \insertframenumber}
\AtBeginSection{\setcounter{framenumber}{0}}

\begin{document}

\section{One}
\subsection{a}
\begin{frame}{First Frame in Section One}
  Slide content\par
  \pause
  More content
\end{frame}

\subsection{b}
\begin{frame}[label=interesting]{Second Frame in Section One -- The one to crossref to}
  Slide content
\end{frame}

\section{Two}
\begin{frame}{First Frame in Section Two}

  Now I want to crossref to interesting slide by section-framenumber (that is, somehow generate \textbf{1-2}) 
  \begin{itemize}
    \item \textbackslash{}ref yields: \ref{interesting}, apparently that is the frame number
    \item \textbackslash{}page yields: \pageref{interesting}, apparently that is the PDF page number
    \item \textbackslash{}autoref yields: \autoref{interesting}, apparently that is the frame number again
    \item nothing there that yields \textbackslash{}thesection of our target label?
  \end{itemize}
\end{frame}

\end{document}