Knitr / RMarkdown (Beamer) 中的 LaTeX 数学表达式

LaTeX math expressions in Knitr / RMarkdown (Beamer)

在 Beamer 演示文稿中使用 knitrkableExtra 编译 table 时,我无法正确呈现 LaTeX 数学表达式。我最初使用 xelatex 作为我的引擎,然后切换到 pdflatex 看看这是否能解决问题。我被这个难住了。

TeX 序言

% 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 文档

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

相关输出

只需将 escape = F 添加到 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))
```

-输出