Problems with kable and escape=FALSE

Hey guys :slightly_smiling_face:
I've created a table called "tbl" in R Markdown which has a Vector c("$T_{m}$", A, B, C, "$m^2$") as rowname. I want the expressions "$T_{m} $", "$m^2$" to be printed as Latex-codes. So first I used the following command within a R-Chunk:

kable(tbl, escape = FALSE, format = 'html', booktabs=TRUE, caption = "Table 1", align = "c")

This works perfectly, but doesn't look very good. Therefore i added some commands:

kable(tbl, escape = FALSE, format = 'html', booktabs=TRUE, caption = "Table 1", align = "c") %>%
kable_styling(latex_options = "scale_down", escape=FALSE) %>%
kable_styling(bootstrap_options = c("striped", "hover"), full_width = F)

So now my table looks good, but doesn't print the Latex-codes in the right way anymore. Instead it prints the codes as they are (e.g. "$T_{m}$" as result).
Whats the problem with the two new commands?

Thanks!

Hi @Peaches,

What output format are you trying to render the document to? PDF or HTML? You seem to be mixing LaTeX and HTML code which may be causing the issue.

In addition, you will find that you get better and faster help if you provide the minimal amount of code or data required to completely reproduce the issue. So in addition to the code you provided, knowing some of the YAML header options can be informative.

I tried to reproduce your issue with this code, but everything seems to be rendering correctly. I removed the first line of kable_styling(latex_options = 'scale_down', escape = FALSE) because it wasn't doing anything. You can't use the latex options on a HTML table.

---
output: html_document
---

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

```{r}
data <- tibble(x = c("$T_{m}$", "$m^2$"))
```

```{r}
kable(data, escape = FALSE, format = 'html', 
      booktabs=TRUE, caption = "Table 1", align = "c")
```

```{r}
kable(data, escape = FALSE, format = 'html', 
      booktabs=TRUE, caption = "Table 1", align = "c") %>%
  kable_styling(bootstrap_options = c("striped", "hover"), full_width = F)
```
1 Like

Hey!
Thank you for your advice. I do try to render the document as html, so i really mix Latex and HTML Code, aren't I? What's the difference between a HTML and a Latex Code?
But I don't see the difference between your example and my work, I have the exact same code and packages :frowning: Help, what am I doing wrong? Sorry, I'm a beginner.

Header options are:

{r, echo=FALSE, message=FALSE, warning=FALSE, results='asis'}

Thank you!

EDIT: I tried your Code on my Computer and it worked, but only (again) without the command "kable_styling(bootstrap_options = c("striped", "hover"), full_width = F)" at the end. Is it possible that i have to install another package?

No problem, there are many of us that are here to help, and I'm sure we will get to the bottom of this.

Do you have the package kableExtra installed? You can make sure it is installed by running this code install.packages('kableExtra'). The kable_styling() function comes from that package.

Yes, it is installed and loaded, but with the following Option:

library(knitr)
library(kableExtra)
options(knitr.table.format = "html")

I tried to delete the "options(knitr.table.format = "html")" but it doesn't make any difference.

Thank you so so much!

Okay, my next recommendation would be to copy and paste enough of your R Markdown document here to allow others to reproduce the issue. Copy the code and place the code inside the code delimiters (back-ticks, found under the escape key).

```
Enter your R Markdown content like this
```

Sharing the minimal amount of code required to reproduce the issue is the best way to work toward a solution.

Okay, here is the Code:


output:
html_document: Default

library(knitr)
library(kableExtra)
options(knitr.table.format = "html")

d <- read.csv2{"Data/table.csv"}

tbl <- t(cbind(
 TK(d$Tm.,'$T_{m}$',s,g),
 TK{d$A, 'A',s,g},
 TK{d$B,'B',s,g},
 TK{d$C, 'C',s,g},
 TK{d$m2, '$m^2$',s,g},
 ))

Info: TK is a written (and sourced) function with the Parameters "d, Name, s, g". Within TK there is the command:

tbl2 <- cbind(1,2,3,4)
 colnames(tbl2) <- c("1","2","3","4")
 rownames(tbl2) <- c(Name)
 return(t(tbl2))

The only interesting Parameter is "Name", because the Vector of rownames is created with it.
Again in the original Markdown Code:

kable(tbl, escape = FALSE, format = 'html', booktabs=TRUE, caption = "Table 1", align = "c") %>% 
  kable_styling(latex_options = "scale_down")  %>%  #can be deleted as you said
  pack_rows("1", 1, 3)%>%
  pack_rows("2", 4,5)%>%
  kable_styling(bootstrap_options = c("striped", "hover"), full_width = F)

Without the last three commands everything works fine, but doesn't look good. With the last three commands it looks good, but the latex-codes are printed as raw-codes.

I've found the solution! I have to write the Codes in the html-Syntax (e.g. T<Sub>m) :slight_smile: Thank you anyway for your advice, it helped me a lot!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.