1

I want to have a fancy ToC in a Beamer presentation.

For that, I want to have a the background filled with a certain color, or even with rounded edges.

In the following code I have two approaches. The approach 1 does not work. The approach 2 does work.

How can I make the approach 1 to work? I mean, without using Beamer-related features as \setbeamertemplate{background}?

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{fit}
\usetikzlibrary{backgrounds}

\defbeamertemplate*{section in toc}{tikz}{%
  \begin{tikzpicture}[remember picture,overlay]
    \global\let\theverylastsectionnumber=\inserttocsectionnumber
    \ifnum\inserttocsectionnumber>1
      \pgfmathtruncatemacro\thelastsection{\inserttocsectionnumber-1}
      \node (tocnode\inserttocsectionnumber) [anchor=north east,inner sep=0pt] at ($(tocnode\thelastsection.south east)-(0pt,1ex)$) {\inserttocsection};
    \else
      \node (tocnode1) [anchor=north east,inner sep=0pt] at (starttocnode) {\inserttocsection};
    \fi
  \end{tikzpicture}
}

\begin{document}

\makeatletter

% Approach 1 does not works
\begin{frame}
  \frametitle{\secname}
  \begin{tikzpicture}[remember picture,overlay]
    \coordinate[shift={(-\beamer@rightmargin,0pt)}] (starttocnode) at (current page.east);
  \end{tikzpicture}
  \tableofcontents
  \IfFileExists{\jobname.toc}{%
    \xdef\Loop{}
    \foreach \n in {1,...,\theverylastsectionnumber} {\xdef\Loop{\Loop(tocnode\n)}}
    \begin{tikzpicture}[remember picture,overlay]
      \begin{pgfonlayer}{background}
        \node (toc) [fill=-yellow!30!blue,fit=\Loop] {};
      \end{pgfonlayer}
    \end{tikzpicture}%
  }{}
\end{frame}


% Approach 2 works
\begingroup
\setbeamertemplate{background}{%
  \IfFileExists{\jobname.toc}{%
    \xdef\Loop{}
    \foreach \n in {1,...,\theverylastsectionnumber} {\xdef\Loop{\Loop(tocnode\n)}}
    \begin{tikzpicture}[remember picture,overlay]
      \node (toc) [fill=-yellow!30!blue,fit=\Loop] {};
    \end{tikzpicture}%
  }{}
}
\begin{frame}
  \frametitle{\secname}
  \begin{tikzpicture}[remember picture,overlay]
    \coordinate[shift={(-\beamer@rightmargin,0pt)}] (starttocnode) at (current page.east);
  \end{tikzpicture}
  \tableofcontents
\end{frame}
\endgroup

\makeatother


\section{Section test 1}
\begin{frame}{\secname}
\end{frame}
\section{Section test 2}
\begin{frame}{\secname}
\end{frame}

\end{document}

1 Answer 1

1

If you don't want to use beamer-related features (why? they are the most fun to use :) ), you could alternatively use \AddToHookNext{shipout/background}{...} from the latex kernel to add your tikz picture to the background (note that you will have to use a transparent background instead of beamer's default white background)

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{fit}
\usetikzlibrary{backgrounds}

\setbeamercolor{background canvas}{bg=}

\defbeamertemplate*{section in toc}{tikz}{%
  \begin{tikzpicture}[remember picture,overlay]
    \global\let\theverylastsectionnumber=\inserttocsectionnumber
    \ifnum\inserttocsectionnumber>1
      \pgfmathtruncatemacro\thelastsection{\inserttocsectionnumber-1}
      \node (tocnode\inserttocsectionnumber) [anchor=north east,inner sep=0pt] at ($(tocnode\thelastsection.south east)-(0pt,1ex)$) {\inserttocsection};
    \else
      \node (tocnode1) [anchor=north east,inner sep=0pt] at (starttocnode) {\inserttocsection};
    \fi
  \end{tikzpicture}
}

\begin{document}

\makeatletter

% Approach 1 does not works
\begin{frame}
  \frametitle{\secname}
  \begin{tikzpicture}[remember picture,overlay]
    \coordinate[shift={(-\beamer@rightmargin,0pt)}] (starttocnode) at (current page.east);
  \end{tikzpicture}
  \tableofcontents
  \IfFileExists{\jobname.toc}{%
    \xdef\Loop{}
    \foreach \n in {1,...,\theverylastsectionnumber} {\xdef\Loop{\Loop(tocnode\n)}}
    \AddToHookNext{shipout/background}{
    \begin{tikzpicture}[remember picture,overlay]
        \node (toc) [fill=-yellow!30!blue,fit=\Loop] {};
    \end{tikzpicture}}%
  }{}
\end{frame}


\section{Section test 1}
\begin{frame}{\secname}
\end{frame}
\section{Section test 2}
\begin{frame}{\secname}
\end{frame}

\end{document}

enter image description here

1
  • It is not that I do not want to use Beamer-related features. I was curious about this interaction between Beamer and Tikz (remember picture and overlay). Also we are always learning something new. I was not aware of AddToHookNext. Thanks. I will wait for some more days, just to see if there are other alternatives, otherwise I will accept your answer.
    – cacamailg
    Commented Nov 20, 2023 at 22:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .