Search code examples
knitrpdflatexkablekableextrabeamer

LaTeX math expressions in Knitr / RMarkdown (Beamer)


I am having difficulty getting LaTeX math expressions to properly render when using knitr and kableExtra to compile a table in a Beamer presentation. I was originally use xelatex as my engine, and then switched to pdflatex to see if this would resolve the issue. I'm stumped on this one.

TeX preamble

% For resizing caption font size
\usepackage{caption}
\captionsetup[figure]{font=tiny}

% Other packages
\usepackage{fancyvrb}
\usepackage{color,xcolor}
\usepackage{framed}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage[normalem]{ulem}
\usepackage{makecell}

% Colour formatting
\definecolor{UBCblue}{rgb}{0.04706, 0.13725, 0.26667} % UBC Blue (primary)
\definecolor{UBCgrey}{rgb}{0.3686, 0.5255, 0.6235} % UBC Grey (secondary)
\setbeamercolor{title}{fg=UBCblue} % slide title
\setbeamercolor{frametitle}{fg=UBCblue} % slide title
\setbeamercolor{structure}{fg=darkgray} % more list stuff, I think
\setbeamertemplate{itemize items}[circle] % for list itemes

% Page numbers
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}[page number]

% Margins
\setbeamersize{text margin left=8mm,text margin right=8mm}

% make code-output smaller
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{fontsize=\footnotesize,commandchars=\\\{\}}

% make console-output smaller:
  \makeatletter
\def\verbatim{\footnotesize\@verbatim \frenchspacing\@vobeyspaces \@xverbatim}
\makeatother
\setlength{\parskip}{0pt}
\setlength{\OuterFrameSep}{-4pt}
\makeatletter
\preto{\@verbatim}{\topsep=-10pt \partopsep=-10pt }
\makeatother

% For better spacing between the list bullets
\renewcommand{\tightlist}{\setlength{\itemsep}{1ex}\setlength{\parskip}{0pt}}

RMarkdown Document

---
title: "My Title"
subtitle: "My Subtitle"
author: Speleo4Life
date: "March 11th, 2021"
output: 
  beamer_presentation:
    latex_engine: pdflatex
    includes:
        in_header: preamble.tex
    theme: "default"
    colortheme: "orchid"
    fonttheme: "serif"
    slide_level: 2
    keep_tex: true
urlcolor: blue
linkcolor: purple
---

```{r setup, eval=TRUE, include=FALSE, echo=FALSE}
library(tidyverse)
library(ggplot2)
library(kableExtra)
library(psych)
```

```{r, eval=TRUE, echo=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(knitr.table.format = "latex")
```
## Review: Null Hypothesis Significance Testing  

```{r, echo=FALSE}
c1 <- c('$\\text{Reject } H_0$', '$\\text{Retain } H_0$')
c2 <- c('$\\text{Type I Error } (\\alpha)$', 'Correct Retention')
c3 <- c('$\\text{Power } (1-\\beta)$', '$\\text{Type II Error } \\beta$')
df <- data.frame(c1,c2,c3)
colnames(df) <- c('$\\textbf{Decision}$', '$\\mathbf{H_0} \\textbf{\\textit{is true}}$',
'$\\mathbf{H_0} \\textbf{\\textit{is false}}$')
kbl(df, booktabs = T) %>%
  add_header_above(c(" ", "State of Reality" = 2))
```

Relevant Output

LaTeX expressions not being rendered


Solution

  • Just add escape = F to kbl:

    ---
    title: "My Title"
    subtitle: "My Subtitle"
    author: Speleo4Life
    date: "March 11th, 2021"
    output: 
      beamer_presentation:
        latex_engine: pdflatex
        includes:
            in_header: preamble.tex
        theme: "default"
        colortheme: "orchid"
        fonttheme: "serif"
        slide_level: 2
        keep_tex: true
    urlcolor: blue
    linkcolor: purple
    ---
    
    ```{r setup, eval=TRUE, include=FALSE, echo=FALSE}
    library(tidyverse)
    library(ggplot2)
    library(kableExtra)
    library(psych)
    ```
    
    ```{r, eval=TRUE, echo=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    options(knitr.table.format = "latex")
    ```
    ## Review: Null Hypothesis Significance Testing  
    
    ```{r, echo=FALSE}
    c1 <- c('$\\text{Reject } H_0$', '$\\text{Retain } H_0$')
    c2 <- c('$\\text{Type I Error } (\\alpha)$', 'Correct Retention')
    c3 <- c('$\\text{Power } (1-\\beta)$', '$\\text{Type II Error } \\beta$')
    df <- data.frame(c1,c2,c3)
    colnames(df) <- c('$\\textbf{Decision}$', '$\\mathbf{H_0} \\textbf{\\textit{is true}}$',
    '$\\mathbf{H_0} \\textbf{\\textit{is false}}$')
    kbl(df, booktabs = T, escape = F) %>%
      add_header_above(c(" ", "State of Reality" = 2))
    ```
    

    -output enter image description here