haozhu233 / kableExtra

Construct Complex Table with knitr::kable() + pipe.

Home Page:https://haozhu233.github.io/kableExtra/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`cell_spec(..., italic = TRUE)` gives incorrect format for PDF table

marklhc opened this issue · comments

Describe the bug
When using cell_spec(..., italic = TRUE) with PDF output, the resulting table is a markdown table with LaTeX format, and is not rendered properly. I first reported this in this quarto issue. I've attached the Rmd source code, but the same happens with qmd output (with knitr).

To Reproduce

---
output: pdf_document
---

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

```{r}
tbl1 <- head(iris)
tbl1[1, 1] <- cell_spec(tbl1[1, 1], bold = TRUE)
tbl1  # `\\textbf{}` is added
knitr::kable(tbl1) # format is fine
```

```{r}
tbl2 <- head(iris)
tbl2[1, 1] <- cell_spec(tbl2[1, 1], italic = TRUE)
tbl2  # `\\em{}` is added
knitr::kable(tbl2)  # format is off
```

This is the resulting table:

image

You should be using kbl(tbl2, escape=FALSE). You probably also want to add booktabs=TRUE, and maybe some of the other options listed on ?kbl.

Thank you for the suggestion, but that does not solve the problem, as in the following picture:

image

You didn't use kbl().

Oh sorry for missing that. Thank you and that works.