kableExtra icon indicating copy to clipboard operation
kableExtra copied to clipboard

kable caption can't handle % symbol knitting to pdf

Open davidrjohnson4 opened this issue 2 years ago • 5 comments

Describe the bug The LaTeX gets messed up (and the resulting pdf formatting is thrown off) when a % symbol is passed in a caption string to a kable object. And Rmd won't accept an escaped "%" as input when knitting.

To Reproduce Do the above ^.

# code to reproduce the bug.
kable(x = df, caption = '% of errors, by user group') %>% kable_styling()
...then knit to pdf.

davidrjohnson4 avatar May 13 '22 20:05 davidrjohnson4

Did you try "escaping" the percent symbol using one or two backslashes?

vincentarelbundock avatar Aug 04 '22 13:08 vincentarelbundock

What I get is: escaping with a single \ doesn't work ("Error: '%' is an unrecognized escape in character string [...]"), but using \ results in "%" showing up in the table caption.

On Thu, Aug 4, 2022 at 7:39 AM Vincent Arel-Bundock < @.***> wrote:

Did you try "escaping" the percent symbol using one or two backslashes?

— Reply to this email directly, view it on GitHub https://github.com/haozhu233/kableExtra/issues/708#issuecomment-1205272853, or unsubscribe https://github.com/notifications/unsubscribe-auth/AU6PBKJ34C6NJTVKHPLIAQLVXPB2PANCNFSM5V4O56NQ . You are receiving this because you authored the thread.Message ID: @.***>

davidrjohnson4 avatar Aug 05 '22 17:08 davidrjohnson4

OK, I believe your issue is a duplicate of https://github.com/haozhu233/kableExtra/issues/670

Try knitting this document:

---
output: pdf_document
---

```{r}
library(kableExtra)

kbl(mtcars[1:4, 1:4],
    caption = "\\% of errors, by user group.",
    escape = FALSE,
    format = "latex") |>
    kable_styling()
```

vincentarelbundock avatar Aug 05 '22 17:08 vincentarelbundock

I traced this back to an upstream problem in knitr. I opened an issue there: https://github.com/yihui/knitr/issues/2155

vincentarelbundock avatar Aug 05 '22 17:08 vincentarelbundock

The maintainer of knitr has replied in the Issue I linked to above. This problem will not be fixed upstream.

Since this would be a breaking change, and given kableExtra's mission of adding to knitr::kable, my sense is that the best way forward would be to just add a note in the documentation for the caption parameter, close this and related issues, and live with the knitr quirks.

It is pretty easy for users to escape the offending characters, as long as they know this is necessary.

What do you think @haozhu233?

vincentarelbundock avatar Aug 24 '22 16:08 vincentarelbundock