knitr icon indicating copy to clipboard operation
knitr copied to clipboard

`escape=TRUE` should escape LaTeX captions

Open vincentarelbundock opened this issue 3 years ago • 0 comments

Several users have opened issues -- here and on the kableExtra repository -- because they have trouble compiling LaTeX tables when captions include “special” characters:

I believe that the source of the confusion is that the escape=TRUE argument applies to cells as well as to row and column names, but that it does not affect the caption. This is very counterintuitive. If escape=TRUE, I think most users will expect the whole table to be escaped, including the caption.

I volunteer to prepare a Pull Request which ensures that LaTeX captions get escaped when escape=TRUE. Is that something you would potentially be interested in reviewing and merging?

Illustration:

library(knitr)

dat <- data.frame(
    a_1 = 1:2,
    a_2 = 2:3
)

kable(dat,
    caption = "% sign breaks compilation",
    escape = TRUE,
    format = "latex") |>
  cat()
#> \begin{table}
#> 
#> \caption{\label{tab:unnamed-chunk-2}% sign breaks compilation}
#> \centering
#> \begin{tabular}[t]{r|r}
#> \hline
#> a\_1 & a\_2\\
#> \hline
#> 1 & 2\\
#> \hline
#> 2 & 3\\
#> \hline
#> \end{tabular}
#> \end{table}


sessionInfo()
#> R version 4.2.1 (2022-06-23)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 22.04 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices datasets  utils     methods   base     
#> 
#> other attached packages:
#> [1] rmarkdown_2.14  knitr_1.39      nvimcom_0.9-131
#> 
#> loaded via a namespace (and not attached):
#>  [1] magrittr_2.0.3    R.cache_0.16.0    rlang_1.0.4      
#>  [4] fastmap_1.1.0     fansi_1.0.3       stringr_1.4.0    
#>  [7] styler_1.7.0      tools_4.2.1       xfun_0.31        
#> [10] R.oo_1.25.0       utf8_1.2.2        cli_3.3.0        
#> [13] withr_2.5.0       htmltools_0.5.3   yaml_2.3.5       
#> [16] digest_0.6.29     tibble_3.1.8      lifecycle_1.0.1  
#> [19] purrr_0.3.4       vctrs_0.4.1       R.utils_2.12.0   
#> [22] fs_1.5.2          bspm_0.3.9        glue_1.6.2       
#> [25] evaluate_0.15     reprex_2.0.1      stringi_1.7.8    
#> [28] compiler_4.2.1    pillar_1.8.0      R.methodsS3_1.8.2
#> [31] pkgconfig_2.0.3

vincentarelbundock avatar Aug 05 '22 17:08 vincentarelbundock

I totally agree with you, and I deeply regret not having put enough thought in this problem. However, this would be a breaking change. Users who have escaped special characters by themselves would have to remove the backslashes.

yihui avatar Aug 22 '22 18:08 yihui

Thanks. This seems reasonable.

vincentarelbundock avatar Aug 24 '22 16:08 vincentarelbundock