yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.36k stars 873 forks source link

unexpected behavior when table cell ends with a latex space "\ " #2308

Closed mjsmith037 closed 7 months ago

mjsmith037 commented 7 months ago

Describe the bug

When a table cell ends with an explicit space, specified in latex as "\ ", kable inappropriately prunes the space, resulting in erroneous latex code that cannot be compiled.

To reproduce

library(knitr)
kable(data.frame(x="\\ "), format="latex", escape=FALSE)

produces:

\begin{tabular}{l}
\hline
x\\
\hline
\\\
\hline
\end{tabular}

Note that line 5 of the output should be "\ \\" (with a space between the first and second backslashes).

When attempting to compile this, an erroneous .pdf is produced:

test.pdf

Session info

xfun::session_info('knitr')
R version 4.3.0 (2023-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 20.3, RStudio 2023.9.0.463

Locale:
  LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
  LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: America/Chicago
tzcode source: system (glibc)

Package version:
  evaluate_0.21   graphics_4.3.0  grDevices_4.3.0 highr_0.10      knitr_1.43      methods_4.3.0   stats_4.3.0     tools_4.3.0    
  utils_4.3.0     xfun_0.39       yaml_2.3.7  

Note this issue was previously, erroneously posted in the kableExtra repo.


By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

cderv commented 7 months ago

Yes I believe we are trimming whitespace, no matter the escape settings... 🤔

https://github.com/yihui/knitr/blob/4b47d2a57bd6ac31712d0afafbc4fe968b663c92/R/table.R#L156

@yihui do you think we could have some specific handling in some case ? Either trimming when escape = TRUE only ?

with additional check on whitespace espace sequence depending on format ? 🤔

Or maybe we need a specific marked string to not trim....

Like

data.frame(x=I("\\ "))
yihui commented 7 months ago

Trailing spaces escaped by \ are no longer trimmed. Thanks!

github-actions[bot] commented 4 weeks ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.