kableExtra
kableExtra copied to clipboard
Percent (%) sign breaks when escape = FALSE
Hello,
I've been wrapping my head around this bug for a couple days, and finally figured out, not knowing that % is a special character in LaTeX, what was going on.
From what I've seen on Google, I might not have been the only one with this issue.
The bug arise when there are percentages (%) in the table and espace = F
. When LaTeX tries to create the file, since % is a comment-out character, some error is thrown, such as the followings, depending on the other options (like booktabs=T
)
! Extra alignment tab has been changed to \cr. <recently read> \endtemplate
! Misplaced \noalign.
\hline ->\noalign
{\ifnum 0=`}\fi \let \hskip \vskip \let \vrule \hrule \let...
A way to solve this issue is to escape the % sign using \\
, but it should probably be implemented directly within the kable function, or specifying it in the help documents!
library(kableExtra)
library(tidyverse)
cars$bob <- scales::percent(cars$speed / cars$dist) # breaks
# cars$bob <- paste0(round(100 * cars$speed / cars$dist), "\\%") # works fine
cars %>%
kbl(
caption = "test kable",
escape = F,
format = "latex"
)
I traced this back to an upstream problem in knitr
. I opened an issue there: https://github.com/yihui/knitr/issues/2155
Related discussion updated here with recommendation: https://github.com/haozhu233/kableExtra/issues/708