Fancy Beamer 演示 ToC:添加背景 Tikz 图片

Fancy Beamer 演示 ToC:添加背景 Tikz 图片

我想要在 Beamer 演示文稿中拥有精美的目录。

为此,我希望背景填充某种颜色,甚至带有圆边。

在下面的代码中我有两种方法。方法 1 不起作用。方法 2 起作用。

我怎样才能使方法 1 起作用?我的意思是,不使用与 Beamer 相关的功能\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

如果你不想使用与 beamer 相关的功能(为什么?它们用起来最有趣:)),你也可以使用\AddToHookNext{shipout/background}{...}latex 内核将你的 tikz 图片添加到背景中(请注意,你必须使用透明背景,而不是 beamer 的默认白色背景)

\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}

在此处输入图片描述

相关内容