rmarkdown::beamer_presentation 中的 kable table 内的 LaTex 代码

LaTex code inside a kable table in an rmarkdown::beamer_presentation

在用 bookdown 编译的较大报告中,我使用了几个 kableExtra table,其中包括 LaTex commands(例如,添加斜体、项目符号以创建列表,并在 table).

中手动添加脚注

当我在用 rmarkdown::beamer_presentation 生成的 LaTex beamer presentation 中复制 table 时,不幸的是编译失败。

如何扭曲 kableExtra table 以包含 LaTex commands

MWE

---
title: "MWE"
subtitle: "Beamer presentation with R-markdown"
author: "Donald Duck"
institute: "some place"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  bookdown::pdf_book:
    base_format: rmarkdown::beamer_presentation
    theme: "THEMENAME"
    latex_engine: xelatex
    toc: false
    slide_level: 2
---

(ref:footnote-a) Text for footnote a
(ref:footnote-b) Text for footnote b

\renewcommand{\arraystretch}{1.3} <!-- increase line spacing for the table -->
```{r table-wLatex, echo=FALSE, fig.align="center", message=FALSE, warning=FALSE, out.width='66%'}
library(kableExtra)
library(dplyr)

# table with manually added footnotes within table
df <- data.frame(
  col1 = c("Category 1", "Category 2"),
  col2 = c(
    "foo and \emph{special foo}$^{a}$", 
    "bar and \n $\boldsymbol{\cdot}$ \emph{random bar}$^{a}$\n $\boldsymbol{\cdot}$ \emph{special bar}$^{b}$")
)

# header: add column names
names(df) <- c("Categories", "Description")

df %>%
  mutate_all(linebreak) %>% # required for linebreaks to work
  kable(
    "latex",
    # escape = FALSE, # needed to be able to include latex commands
    booktabs=TRUE,
    align = "l",
    caption = "Caption Table with LaTex" # short caption for LoT
  ) %>%
  kableExtra::footnote(
    alphabet = c(
      "(ref:footnote-a)",
      "(ref:footnote-b)"
      ),
    threeparttable = TRUE, # important! Else footnote runs beyond the table
    footnote_as_chunk = TRUE, title_format = c("italic", "underline")
  ) %>% 
  column_spec(1, width = "11em") %>% # fix width column 1
  column_spec(2, width = "27em") # fix width column 2
```
\renewcommand{\arraystretch}{1} <!-- reset row height/line spacing -->

必须为 kableExtra table 加载几个 LaTex 包,这样 LaTex 代码就可以很好地包含在里面。

为了固定列宽,column_spec(1, width = "3cm")最好在cm中指定而不是em(对于前面的规范,table 运行超出幻灯片)。

---
title: "LaTex code inside a kable table in an rmarkdown::beamer_presentation"
output:
  bookdown::pdf_book:
    base_format: rmarkdown::beamer_presentation
    latex_engine: xelatex
    toc: false
    slide_level: 2
header-includes:
  - \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}
  # - \usepackage[usenames,dvipsnames]{xcolor} # clashes, as loaded by markdown anyway
---

(ref:footnote-a) Text for footnote a
(ref:footnote-b) Text for footnote b

\renewcommand{\arraystretch}{1.3} <!-- increase line spacing for the table -->
```{r table-wLatex, echo=FALSE, fig.align="center", message=FALSE, warning=FALSE, out.width='30%'}
library(kableExtra)
library(dplyr)

# table with manually added footnotes within table
df <- data.frame(
  col1 = c("Category 1", "Category 2"),
  col2 = c(
    "foo and \emph{special foo}$^{a}$", 
    "bar and \n $\boldsymbol{\cdot}$ \emph{random bar}$^{a}$\n $\boldsymbol{\cdot}$ \emph{special bar}$^{b}$")
)

# header: add column names
names(df) <- c("Categories", "Description")

df %>%
  mutate_all(linebreak) %>% # required for linebreaks to work
  kable(
    "latex",
    escape = FALSE, # needed to be able to include latex commands
    booktabs=TRUE,
    align = "l",
    caption = "Caption Table with LaTex" # short caption for LoT
  ) %>%
  kableExtra::footnote(
    alphabet = c(
      "(ref:footnote-a)",
      "(ref:footnote-b)"
      ),
    threeparttable = TRUE, # important! Else footnote runs beyond the table
    footnote_as_chunk = TRUE, title_format = c("italic", "underline")
  ) %>% 
  kable_styling( # for wide tables: scale them down to fit
    full_width = F,
    latex_options = c(
      "scale_down"
    )) %>% 
  column_spec(1, width = "3cm") %>% # fix width column 1
  column_spec(2, width = "5cm") # fix width column 2
```
\renewcommand{\arraystretch}{1} <!-- reset row height/line spacing -->