我正在尝试创建一个 Beamer 模板,其标题页和目录页上有背景图像。我已经设置了背景图像,并且它按预期工作,如下图最后一张幻灯片所示。但后来我想使用 \AtBeginDocument 宏将这些页面自动添加到 beamer 文档中,背景图片被向下移动(前两张幻灯片):
我猜这是因为 Beamer 重写了\includegraphics
宏,但我不确定如何在我的代码中修复它。
\documentclass[aspectratio=169]{beamer}
\defbeamertemplate*{title page}{mytpl}[1][]
{
\color{red}\inserttitle
\color{red}\insertsubtitle
}
\defbeamertemplate*{background}{mydefault}{}
\defbeamertemplate*{background}{titlebg}{\includegraphics[height=\paperheight]{duck}}
\defbeamertemplate*{background}{tocbg}{\includegraphics[height=\paperheight]{duck}}
\BeforeBeginEnvironment{frame}{\setbeamertemplate{background}[mydefault]}
\makeatletter
\define@key{beamerframe}{tocbg}[true]{%
\setbeamertemplate{background}[tocbg]%
\setbeamertemplate{footline}{}
}
\makeatother%
\setbeamertemplate{navigation symbols}{}
\AtBeginDocument{
\begingroup
\begin{frame}[tocbg]
\titlepage
\end{frame}
\endgroup
\begingroup
\begin{frame}[tocbg]
\color{red}{My second slide used for TOC later}
\end{frame}
\endgroup
}
\title{Test 1}
\subtitle{Subtitle}
\begin{document}
\begin{frame}[tocbg]
\color{red}{My second slide used for TOC later}
\end{frame}
\end{document}
你有什么建议吗?提前谢谢!
答案1
你可以尝试使用begindocument/end
钩子。这应该足够晚,因为 beamer 已经计算了它的页面几何形状等。
\documentclass[aspectratio=169]{beamer}
\defbeamertemplate*{title page}{mytpl}[1][]
{
\color{red}\inserttitle
\color{red}\insertsubtitle
}
\defbeamertemplate*{background}{mydefault}{}
\defbeamertemplate*{background}{titlebg}{\includegraphics[height=\paperheight]{duck}}
\defbeamertemplate*{background}{tocbg}{\includegraphics[height=\paperheight]{duck}}
\BeforeBeginEnvironment{frame}{\setbeamertemplate{background}[mydefault]}
\makeatletter
\define@key{beamerframe}{tocbg}[true]{%
\setbeamertemplate{background}[tocbg]%
\setbeamertemplate{footline}{}
}
\makeatother%
\setbeamertemplate{navigation symbols}{}
\AddToHook{begindocument/end}{
\begingroup
\begin{frame}[tocbg]
\titlepage
\end{frame}
\endgroup
\begingroup
\begin{frame}[tocbg]
\color{red}{My second slide used for TOC later}
\end{frame}
\endgroup
}
\title{Test 1}
\subtitle{Subtitle}
\begin{document}
\begin{frame}[tocbg]
\color{red}{My second slide used for TOC later}
\end{frame}
\end{document}