更改 R Markdown beamer 演示文稿中的背景

Change background in R Markdown beamer presentation

我正在用 rmarkdown 编写 beamer 演示文稿,我有两种类型的框架,它们的背景应该有所不同。 所以我在乳胶中写了两个这样的函数:

\newcommand{\settitlestyle}{
\setbeamertemplate{background canvas}{%
  \includegraphics[width = \paperwidth, height = \paperheight] 
{backgroundtitle.jpg}}
} 

\setmainstyle 是完全相同的命令但是另一个 jpg.

在 YAML 中,我已经输入了一个定义函数和调用的 tex 文件 \settitlestyle。作品。但是在第一张幻灯片之后我想切换到主样式。当我在 markdown 文件中调用 \setmainstyle 时,没有任何反应。

您的 \setmainstyle 命令的问题是它将在框架内使用,因此无效。

为避免此问题,您可以使用与 https://tex.stackexchange.com/questions/173201/beamer-template-with-different-style-options-for-frames 中相同的策略来创建将更改背景的框架选项。

不幸的是,rmarkdown 只是忽略了用户创建的框架选项,只传递了一小部分预定义选项。要欺骗 rmarkdown,可以重新调整 beamer 通常不使用的框架选项的用途,即 standout 框架选项(它仅由 metropolis 主题使用)

---
output: 
  beamer_presentation:
    keep_tex: true
header-includes: |
  \usepackage{etoolbox}

  \defbeamertemplate{background canvas}{mydefault}{%
    \includegraphics[width=\paperwidth,height=\paperheight]{example-image-duck}
  }
  \defbeamertemplate{background canvas}{standout}{%
    \includegraphics[width=\paperwidth,height=\paperheight,page=2]{example-image-duck}
  }

  \BeforeBeginEnvironment{frame}{%
    \setbeamertemplate{background canvas}[mydefault]%
  }

  \makeatletter
  \define@key{beamerframe}{standout}[true]{%
    \setbeamertemplate{background canvas}[standout]%
  }
  \makeatother

---

# frametitle 

test

# frametitle with different background {.standout}

test

# frametitle

test

或者如果您想要更改以下所有帧的背景:

\usepackage{etoolbox}

\defbeamertemplate{background canvas}{mydefault}{%
  \includegraphics[height=\paperheight,page=2]{example-image-duck}
}
\defbeamertemplate{background canvas}{standout}{%
  \includegraphics[height=\paperheight]{example-image-duck}
}

\setbeamertemplate{background canvas}[mydefault]%

\makeatletter
\define@key{beamerframe}{standout}[true]{%
  \setbeamertemplate{background canvas}[standout]%
}
\makeatother