![\AtBeginDocument 宏中的图片位置不正确(Beamer 演示文稿)](https://linux22.com/image/438611/%5CAtBeginDocument%20%E5%AE%8F%E4%B8%AD%E7%9A%84%E5%9B%BE%E7%89%87%E4%BD%8D%E7%BD%AE%E4%B8%8D%E6%AD%A3%E7%A1%AE%EF%BC%88Beamer%20%E6%BC%94%E7%A4%BA%E6%96%87%E7%A8%BF%EF%BC%89.png)
我正在尝试创建一个 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}