Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rmarkdown::render errors when compiling tex to pdf, with png figure created using python code in French locale #1498

Closed
tdhock opened this issue Oct 25, 2023 · 3 comments

Comments

@tdhock
Copy link

tdhock commented Oct 25, 2023

Hi! first of all, thanks very much for supporting the reticulate package, which I find very useful.
I first tried filing this as rstudio/rmarkdown#2525 (comment) but @yihui says this should be filed here instead.
I am trying to render Rmd to beamer slides on a computer with LC_NUMERIC=fr_FR.UTF-8 meaning that the decimal separator prints as a comma (one half is 0,5).
I expected that I should be able to render rmarkdown to beamer slides, but I observe that there is an error.
The issue is that there is a line in the generated tex file which looks like \includegraphics[width=4,5in]{figure} and that is problematic because pdflatex interprets the comma as a separator between optional arguments in the [ ].
The issue only happens when dev: png is specified in the yaml header.

The work-around that users can do, is to set LC_NUMERIC=C, and then the compilation works fine.
The fix I would suggest in the reticulate/rmarkdown package(s) is to set LC_NUMERIC=C while generating the \includegraphics[width=4.5in]{figure} line.

Below is a minimal reproducible example R script (of course you have to change the RETICULATE_PYTHON line to wherever you have python with matplotlib installed)

cat('---
title: "matplotlib test"
author: "Toby Dylan Hocking"
output: 
  beamer_presentation:
    dev: png
---\n```{r}
Sys.setenv(RETICULATE_PYTHON="/home/tdhock/miniconda3/envs/2023-08-artificial-intelligence/bin/python")
Sys.setlocale("LC_NUMERIC","fr_FR.UTF-8")
(width <- 4.5)\n```\n```{python fig.width=width}
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.show()\n```
', file="fig-size-bug.Rmd")
rmarkdown::render("fig-size-bug.Rmd")
system("grep 4,5 fig-size-bug.tex")
xfun::session_info("rmarkdown")

When running the code above on my system, the output is shown below:

> rmarkdown::render("fig-size-bug.Rmd")


processing file: fig-size-bug.Rmd
                                                                                                            
output file: fig-size-bug.knit.md

/usr/bin/pandoc +RTS -K512m -RTS fig-size-bug.knit.md --to beamer --from markdown+autolink_bare_uris+tex_math_single_backslash --output fig-size-bug.tex --lua-filter /home/tdhock/lib/R/library/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /home/tdhock/lib/R/library/rmarkdown/rmarkdown/lua/latex-div.lua --highlight-style tango --pdf-engine pdflatex --self-contained 
! Package keyval Error: 5in undefined.

Erreur : LaTeX failed to compile fig-size-bug.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See fig-size-bug.log for more info.
> system("grep 4,5 fig-size-bug.tex")
## [1] 4,5
\includegraphics[width=4,5in]{fig-size-bug_files/figure-beamer/unnamed-chunk-2-1}
> xfun::session_info("rmarkdown")
R version 4.3.1 (2023-06-16)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.3 LTS


Locale: fr_FR.UTF-8

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

Package version:
  base64enc_0.1.3   bslib_0.5.1       cachem_1.0.8      cli_3.6.1        
  digest_0.6.33     ellipsis_0.3.2    evaluate_0.22     fastmap_1.1.1    
  fontawesome_0.5.2 fs_1.6.3          glue_1.6.2        graphics_4.3.1   
  grDevices_4.3.1   highr_0.10        htmltools_0.5.6.1 jquerylib_0.1.4  
  jsonlite_1.8.7    knitr_1.44        lifecycle_1.0.3   magrittr_2.0.3   
  memoise_2.0.1     methods_4.3.1     mime_0.12         R6_2.5.1         
  rappdirs_0.3.3    rlang_1.1.1       rmarkdown_2.25.1  sass_0.4.7       
  stats_4.3.1       stringi_1.7.12    stringr_1.5.0     tinytex_0.48     
  tools_4.3.1       utils_4.3.1       vctrs_0.6.4       xfun_0.40        
  yaml_2.3.7       

Pandoc version: 2.9.2.1

Please note that in the grep command, the first 4,5 is expected, because I set the french locale in my Rmd, so I would like to see that in the generated R code chunk output blocks (but it should be 4.5 in the tex file includegraphics width line).

@tdhock
Copy link
Author

tdhock commented Oct 25, 2023

this is an issue using current github version

> rmarkdown::render("fig-size-bug.Rmd")


processing file: fig-size-bug.Rmd
                                                                                                            
output file: fig-size-bug.knit.md

/usr/bin/pandoc +RTS -K512m -RTS fig-size-bug.knit.md --to beamer --from markdown+autolink_bare_uris+tex_math_single_backslash --output fig-size-bug.tex --lua-filter /home/tdhock/lib/R/library/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /home/tdhock/lib/R/library/rmarkdown/rmarkdown/lua/latex-div.lua --highlight-style tango --pdf-engine pdflatex --self-contained 
! Package keyval Error: 5in undefined.

Erreur : LaTeX failed to compile fig-size-bug.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See fig-size-bug.log for more info.
> system("grep 4,5 fig-size-bug.tex")
## [1] 4,5
\includegraphics[width=4,5in]{fig-size-bug_files/figure-beamer/unnamed-chunk-2-1}
> sessionInfo()
R version 4.3.1 (2023-06-16)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
LAPACK: /home/tdhock/miniconda3/envs/2023-08-artificial-intelligence/lib/libmkl_rt.so.2;  LAPACK version 3.10.1

locale:
[1] fr_FR.UTF-8

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

attached base packages:
[1] stats     graphics  utils     datasets  grDevices methods   base     

loaded via a namespace (and not attached):
 [1] digest_0.6.33          fastmap_1.1.1          xfun_0.40             
 [4] Matrix_1.6-1.1         lattice_0.22-5         reticulate_1.34.0.9000
 [7] knitr_1.44             htmltools_0.5.6.1      png_0.1-8             
[10] rmarkdown_2.25.1       tinytex_0.48           cli_3.6.1             
[13] grid_4.3.1             withr_2.5.1            compiler_4.3.1        
[16] tools_4.3.1            evaluate_0.22          Rcpp_1.0.11           
[19] yaml_2.3.7             rlang_1.1.1            jsonlite_1.8.7        

@t-kalinowski
Copy link
Member

t-kalinowski commented Jan 30, 2024

I'm not sure this is reticulate issue, and it's not clear to me what we can do to fix it in reticulate. In reticulate::eng_python() we only read chunk options options$fig.width and options$fig.height and pass them through to the appropriate python contexts for rendering plots; we don't munge on the options any further, and return them unmodified.

I think this might be a knitr::include_graphics() bug, since that's how reticulate includes python plots, and is a difference between the Python MRE and the R examples in rstudio/rmarkdown#2525 (comment)

@t-kalinowski
Copy link
Member

cc @cderv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants