Search code examples
rlatexr-markdownbeamerxelatex

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


In a larger report compiled with bookdown I used several kableExtra tables which included LaTex commands (e.g., to add italics, bullets to create a listing, and manually-added footnotes within the table).

When I copy the tables in a LaTex beamer presentation generated with rmarkdown::beamer_presentation, compilation unfortunately fails.

How to twist the kableExtra tables to include the 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 -->

Solution

  • Several LaTex packages had to be loaded for kableExtra table such that LaTex code can well be included inside.

    To fix the column width, column_spec(1, width = "3cm") is better specified in cm instead of em (for the previous specification, table ran beyond the slide).

    tableLaTex

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