Michael S Taylor
Michael S Taylor

Reputation: 491

kableExtra stops evaluating latex code in table

I have an R Markdown table with this \rule{1cm}{0.4pt} LaTeX command in each cell of one column. The table formats just fine with kable if I do not include the kableExtra package. If I do include kabelExtra, the LaTeX command is no longer interpreted. The results are shown below, without and with kableExtra. No other change was made. The top example is my desired result.

I inspected the .tex output. kableExtra seems to format the LaTeX command as literal text: \textbackslash{}rule\{1cm\}\{0.4pt\} instead of the command shown above.

I want to use kableExtra for other features like setting column widths but I need it to interpret the LaTeX commands. I did not find anything in the manual or vignettes that seemed to address included LateX commands. Am I missing something?

Edit

I tried adding format = "latex" to the kable call when using kableExtra but undesired result remained.

MWE

---
title: "Without kableExtra"
output: 
  pdf_document: 
    keep_tex: TRUE
---

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

```{r}
library(tibble)
library(knitr)
#library(kableExtra)

a = seq(1:3)
b = seq(4:6)
tab <- as.tibble(cbind(a,b))
tab <- add_column(tab, c = "\\rule{1cm}{0.4pt}")
```

```{r}
kable(tab,
      booktabs = TRUE, 
      longtable = TRUE)
```

Results

without and with kableExtra

Upvotes: 2

Views: 916

Answers (1)

Rekyt
Rekyt

Reputation: 364

When using kableExtra you should add the argument escape = FALSE to your kable() call. The escape argument let you use LaTeX commands in table.

The following works:

---
title: "Without kableExtra"
output: 
  pdf_document: 
    keep_tex: TRUE
---

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

```{r}
library(tibble)
library(knitr)
library(kableExtra)

a = seq(1:3)
b = seq(4:6)
tab <- as.tibble(cbind(a,b))
tab <- add_column(tab, c = "\\rule{1cm}{0.4pt}")
```

```{r}
kable(tab,
      booktabs = TRUE, 
      longtable = TRUE,
      escape = FALSE)
```

Upvotes: 5

Related Questions