rlatexr-markdownbeamer

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.


Solution

  • 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 moloch theme)

    ---
    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

    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
    

    enter image description here

    Update:

    Pandoc now allows arbitrary frame options (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