Search code examples
rlatexr-markdownchunksbeamer

Change representation (background-color & frame) of chunks in RMarkdown (with Beamer-Presentations)


Background: I'm using the metropolis theme which slides background colour is the same as the default background colour of the displayed code in chunks in beamer.

Problem: I want to change the representation of the chunks output. There should be a different type of representation for source-code & results to distinguish them. The source-codes background should be in a slightly darker grey than the metropolis slides background and the results should be surrounded by a black line as a frame.

What I already tried: This threat answers my question for HTML-outputs (YAML: output: html_document), but I didn't figured out how to get it working in beamer_presentation.

This is my "minimal" working example:

---
title: "The Influence of the German Statutory Minimum Wage's Introduction on Individuals' Health"
author: "Simon Ress | Ruhr-Universität Bochum"
institute: "Conference: 56. Jahrestagung der DGSMP, Leipzig, 2021"
date: "September 22, 2021"

output:
  beamer_presentation:
    keep_md: true
    keep_tex: no
    latex_engine: xelatex
    #theme: metropolis
    slide_level: 2 # which header level should be printed as slides
    incremental: no
header-includes:
  - \usetheme[numbering=fraction]{metropolis}
#Define footer:
  - \definecolor{beaublue}{rgb}{0.74, 0.83, 0.9}
  - \setbeamertemplate{frame footer}{\tiny{\textcolor{beaublue}{Conference 56. Jahrestagung der DGSMP, 2021 | SIMON RESS}}}
#hide footer on title page:
  - |
    \makeatletter
    \def\ps@titlepage{%
      \setbeamertemplate{footline}{}
    }
    \addtobeamertemplate{title page}{\thispagestyle{titlepage}}{}
    \makeatother
#show footer on section's start/title pages:
  #overwrite "plain,c,noframenumbering" in section pages definition -> enables footer:
  - |
    \makeatletter
    \renewcommand{\metropolis@enablesectionpage}{
      \AtBeginSection{
        \ifbeamer@inframe
          \sectionpage
        \else
          \frame[c]{\sectionpage}
        \fi
      }
    }
    \metropolis@enablesectionpage
    \makeatother
  #define footer of section pages:
  - |
    \makeatletter
    \def\ps@sectionpage{%
      \setbeamertemplate{frame footer}{\tiny{\textcolor{beaublue}{Conference 56. Jahrestagung der DGSMP, 2021 | SIMON RESS}}}
    }
    \addtobeamertemplate{section page}{\thispagestyle{sectionpage}}{}
    \makeatother
#add secrtion numbers to TOC:
  - |
    \setbeamertemplate{section in toc}{
    \leavevmode%
    \inserttocsectionnumber. 
    \inserttocsection\par%
    }
    \setbeamertemplate{subsection in toc}{
    \leavevmode\leftskip=2.5em\inserttocsubsection\par}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## Content
\tableofcontents[]


# Slide with R Output
```{r cars, echo = TRUE}
summary(cars)
```

Solution

    • you can change the colour of the code with \definecolor{shadecolor}{RGB}{148,248,248} (choose whatever colour you like)

    • adding a frame around the output is a bit more hacky. rmarkdown automatically loads all kinds of packages to format verbatim code, like the fancyverb package, but then it goes ahead and ignores them and uses the normal latex verbatim endvironment for the output. Makes no sense at all, but you can use this dirty hack to redefine the environment like this to use the fancyverb package which provides an option to add a frame:

      \let\verbatim\undefined
      \let\verbatimend\undefined
      \DefineVerbatimEnvironment{verbatim}{Verbatim}{frame=single}
      

    ---
    title: "The Influence of the German Statutory Minimum Wage's Introduction on Individuals' Health"
    author: "Simon Ress | Ruhr-Universität Bochum"
    institute: "Conference: 56. Jahrestagung der DGSMP, Leipzig, 2021"
    date: "September 22, 2021"
    
    output:
      beamer_presentation:
        keep_md: true
        keep_tex: yes
        latex_engine: xelatex
        #theme: metropolis
        slide_level: 2 # which header level should be printed as slides
        incremental: no
    header-includes:
      - \usetheme[numbering=fraction]{metropolis}
    #Define footer:
      - \definecolor{beaublue}{rgb}{0.74, 0.83, 0.9}
      - \setbeamertemplate{frame footer}{\tiny{\textcolor{beaublue}{Conference 56. Jahrestagung der DGSMP, 2021 | SIMON RESS}}}
    #hide footer on title page:
      - |
        \makeatletter
        \def\ps@titlepage{%
          \setbeamertemplate{footline}{}
        }
        \addtobeamertemplate{title page}{\thispagestyle{titlepage}}{}
        \makeatother
    #show footer on section's start/title pages:
      #overwrite "plain,c,noframenumbering" in section pages definition -> enables footer:
      - |
        \makeatletter
        \renewcommand{\metropolis@enablesectionpage}{
          \AtBeginSection{
            \ifbeamer@inframe
              \sectionpage
            \else
              \frame[c]{\sectionpage}
            \fi
          }
        }
        \metropolis@enablesectionpage
        \makeatother
      #define footer of section pages:
      - |
        \makeatletter
        \def\ps@sectionpage{%
          \setbeamertemplate{frame footer}{\tiny{\textcolor{beaublue}{Conference 56. Jahrestagung der DGSMP, 2021 | SIMON RESS}}}
        }
        \addtobeamertemplate{section page}{\thispagestyle{sectionpage}}{}
        \makeatother
    #add secrtion numbers to TOC:
      - |
        \setbeamertemplate{section in toc}{
        \leavevmode%
        \inserttocsectionnumber. 
        \inserttocsection\par%
        }
        \setbeamertemplate{subsection in toc}{
        \leavevmode\leftskip=2.5em\inserttocsubsection\par}
        \definecolor{shadecolor}{RGB}{148,248,248}
        \let\verbatim\undefined
        \let\verbatimend\undefined
        \DefineVerbatimEnvironment{verbatim}{Verbatim}{frame=single}
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    ```
    
    ## Content
    \tableofcontents[]
    
    
    # Slide with R Output
    ```{r cars, echo = TRUE}
    summary(cars)
    ```
    

    enter image description here