Beamer Table of Contents – Fix \par Within \addcontentsline Breaking List of Frames

beamerline-breakingtable of contents

This answer provides the indispensable feature to add a list of frames to a beamer presentation.

It containts the the line

  \addcontentsline{lbf}{section}{\protect\makebox[2em][l]{%
    \protect\usebeamercolor[fg]{structure}\insertframenumber\hfill}%
  \insertframetitle\par}%

Recently it stopped working with two issues:

  • if \par is included, there is a compilation error

    Paragraph ended before \addcontentsline was complete. \end{frame}
    Extra }, or forgotten \endgroup. \end{frame}

  • The frame number is also included at the end of line, or actually at the beginning of the next line

I thought to substitute \par with \newline with seems to be a workaround for the first issue, but the second issue persists, leading to this unexpected output:

enter image description here

What changed? What broke?

Thank you very much in advance!

Original MWE

\documentclass{beamer}

\newif\ifframeinlbf
\frameinlbftrue
\makeatletter
\newcommand\listofframes{\@starttoc{lbf}}
\makeatother

\addtobeamertemplate{frametitle}{}{%
  \ifframeinlbf
  \addcontentsline{lbf}{section}{\protect\makebox[2em][l]{%
    \protect\usebeamercolor[fg]{structure}\insertframenumber\hfill}%
  \insertframetitle\par}%
  \else\fi
}

\begin{document}

\frameinlbffalse
\begin{frame}
\frametitle{List of Frames}
\listofframes
\end{frame}

\frameinlbftrue
\begin{frame}
\frametitle{Test Frame One}
test
\end{frame}

Best Answer

You could use \endgraf instead of \par to avoid the error. But imho it is odd to add it to the \addcontentsline, I would use a special <type> and define a suitable \l@<type> command, which then can also suppress the "page number":

\documentclass{beamer}

\newif\ifframeinlbf
\frameinlbftrue
\makeatletter
\newcommand\listofframes{\@starttoc{lbf}}
\newcommand\l@frame[2]{#1\par}
\makeatother

\addtobeamertemplate{frametitle}{}{%
  \ifframeinlbf
  \addcontentsline{lbf}{frame}{\protect\makebox[2em][l]{%
    \protect\usebeamercolor[fg]{structure}\insertframenumber\hfill}%
  \insertframetitle}%
  \else\fi
}
\begin{document}

\frameinlbffalse
\begin{frame}
\frametitle{List of Frames}
\listofframes
\end{frame}

\frameinlbftrue
\begin{frame}
\frametitle{Test Frame One}
test
\end{frame}
\begin{frame}
\frametitle{Test Frame two}
test
\end{frame}
\end{document}

enter image description here