stefano-meschiari / latex2exp

Use LaTeX in R graphics.
Other
185 stars 10 forks source link

Rendering mathematical operators and symbols in ggplot and plot #61

Open Gitmoid opened 1 year ago

Gitmoid commented 1 year ago

I seem to be having troubles displaying mathematical operators and other symbols while using ggplot() and plot() in RStudio. I reinstalled and deleted all R and RStudio folders. I tried opening the graphs in maximized new window. I haven't changed much in settings, only Appearance to Vibrant Ink and Sweave PDF Preview after compile using to RStudio Viewer. Otherwise, all of my RStudios settings should be set to default. Unicode characters work correctly.

> sessionInfo()
R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621) # Incorrect, Windows 11 Home, 22H2, build 22621.1344

Matrix products: default

locale:
[1] LC_COLLATE=Czech_Czechia.utf8  LC_CTYPE=Czech_Czechia.utf8    LC_MONETARY=Czech_Czechia.utf8 LC_NUMERIC=C                   LC_TIME=Czech_Czechia.utf8    

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

other attached packages:
[1] ggplot2_3.4.1   latex2exp_0.9.6

loaded via a namespace (and not attached):
 [1] zip_2.2.2        Rcpp_1.0.10      compiler_4.2.2   pillar_1.8.1     tools_4.2.2      digest_0.6.31    evaluate_0.20    lifecycle_1.0.3  tibble_3.2.0    
[10] gtable_0.3.1     pkgconfig_2.0.3  rlang_1.0.6      openxlsx_4.2.5.2 cli_3.6.0        rstudioapi_0.14  yaml_2.3.7       xfun_0.37        fastmap_1.1.1   
[19] withr_2.5.0      stringr_1.5.0    dplyr_1.1.0      knitr_1.42       generics_0.1.3   vctrs_0.5.2      grid_4.2.2       tidyselect_1.2.0 glue_1.6.2      
[28] R6_2.5.1         fansi_1.0.4      rmarkdown_2.20   farver_2.1.1     magrittr_2.0.3   scales_1.2.1     htmltools_0.5.4  colorspace_2.1-0 labeling_0.4.2  
[37] utf8_1.2.3       stringi_1.7.12   munsell_0.5.0   
library(latex2exp)
library(ggplot2)

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  xlab(TeX(r"($\alpha \cdot \beta$)")) + # incorrect for \cdot, works for alpha and beta
  ylab(TeX(r"($\pm \neg \subset$)")) + # incorrect
  labs(title = "\u03b1 \u2022 \u03b2") # correct

plot(TeX(r"($\alpha \cdot \beta$)")) # \cdot wrong again

image image

bstaton1 commented 1 year ago

I am having a similar problem with the \rightarrow operator, which is very problematic for me because I use it quite a lot.

plot(latex2exp::TeX("$\\alpha \\rightarrow \\beta$"))

gives:

image

This StackOverflow post received an answer that seems to suggest a system font file (*.ttf) may be outdated, but I haven't had luck with that approach.

Any insights on how to remedy this problem would be very useful.

Here's sessionInfo():

R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

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

loaded via a namespace (and not attached):
 [1] compiler_4.2.2  postpack_0.5.4  magrittr_2.0.3  cli_3.5.0      
 [5] tools_4.2.2     glue_1.6.2      rstudioapi_0.14 vctrs_0.5.1    
 [9] stringi_1.7.8   stringr_1.5.0   lifecycle_1.0.3 rlang_1.0.6  
Gitmoid commented 1 year ago

I tried updating to R-Devel build and it has solved the issue. I expect the issue to be fixed in the new version R version 4.3.0 (Already Tomorrow). The release of 4.3.0 (Already Tomorrow) is scheduled for Friday 2023-04-21.

bstaton1 commented 1 year ago

I expect the issue to be fixed in the new version R version 4.3.0 (Already Tomorrow)

I can confirm (with very minimal testing) with the official R v4.3.0 release that this has been fixed -- running the same code I have above now gives:

image

as expected. Thanks!