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

3

我正在使用rmarkdown编写一个beamer演示文稿,其中有两种类型的帧应该因其背景而不同。因此,我在LaTeX中编写了两个这样的函数:

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

\setmainstyle 是一条与另一个jpg完全相同的命令。

在YAML中,我已经输入了一个定义函数并调用\settitlestyle的tex文件。它可以工作。但是在第一张幻灯片之后,我想切换到主要样式。当我在Markdown文件中调用\setmainstyle时,什么也没有发生。

1个回答

4

你的\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

enter image description here

或者,如果您想要更改所有后续帧的背景:

\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

enter image description here

更新:

Pandoc现在允许任意框架选项(https://github.com/jgm/pandoc/commit/7fbce82f2f7b69e88b23cf138ea6cd3a86786b91

---
output: 
  beamer_presentation:
header-includes: |

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

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

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

---

# frametitle 

test

# Heading {frameoptions="special"}

test

# frametitle 

test

完美运作。来自pandoc的人说:贝默用户指南第8.1节中描述的所有其他帧属性也可以使用(请参见下一条评论中的列表)。 - undefined
允许换行, 允许分页, 粗体, 斜体, 小字体, 环境, 标签, 普通, 收缩, 突出显示, 无页码, 压缩. - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接