简体   繁体   中英

Change background in R Markdown beamer presentation

I'm writing on a beamer presentation in rmarkdown and I have two types of frames which should differ by their background. So I wrote two functions like that in latex:

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

\\setmainstyle is exactly the same command but another jpg.

In the YAML I have already input a tex file that defines the functions and calls \\settitlestyle . Works. But after the first slide I want to switch to the mainstyle. When I call \\setmainstyle in the markdownfile nothing happens.

The problem with your \\setmainstyle command is that it will be used inside a frame and thus be void.

To avoid this problem you could use the same strategy as in https://tex.stackexchange.com/questions/173201/beamer-template-with-different-style-options-for-frames to create a frame option which will change the background.

Unfortunately rmarkdown simply ignores user created frame options and only passes on a tiny list of predefined options. To trick rmarkdown one could repurpose a frame option which is normally not used by beamer, the standout frame option (it is only used by the metropolis theme)

---
output: 
  beamer_presentation:
    keep_tex: true
    includes:
      in_header: preamble.tex
---

# frametitle 

test

# frametitle with different background {.standout}

test

# frametitle

test

and preamble.tex

\usepackage{etoolbox}

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

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

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

在此处输入图片说明

or if you want to change the background for all following frames:

\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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM