haozhu233 / kableExtra

Construct Complex Table with knitr::kable() + pipe.
https://haozhu233.github.io/kableExtra/
Other
688 stars 147 forks source link

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

Closed marklhc closed 5 months ago

marklhc commented 6 months ago

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)
tbl1 <- head(iris)
tbl1[1, 1] <- cell_spec(tbl1[1, 1], bold = TRUE)
tbl1  # `\\textbf{}` is added
knitr::kable(tbl1) # format is fine
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](https://github.com/haozhu233/kableExtra/assets/6175332/3d98a886-195b-4a12-8fc0-e24cd3b0f50b)
dmurdoch commented 5 months ago

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.

marklhc commented 5 months ago

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

image

dmurdoch commented 5 months ago

You didn't use kbl().

marklhc commented 5 months ago

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