stefano-meschiari / latex2exp

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

Error with two 'leq' in latex2exp::TeX() #38

Closed aursiber closed 2 years ago

aursiber commented 2 years ago

I need to display a text containing two consecutive "less or equal than" symbols in a ggplot. I manage to display one symbol but not two. This bug does not occur with 'less than' symbols.

# NON OK
(text1 <- paste0("for $0\\leq t \\leq t_{c}$"))
(textFormat <- latex2exp::TeX(text1))

# OK : example with a first symbol 'leq' and a second '<'
(text1 <- paste0("for $0\\leq t < t_{c}$"))
(textFormat <- latex2exp::TeX(text1))

# OK : example with a first symbol '<' and a second 'leq'
(text1 <- paste0("for $0<t\\leq t_{c}$"))
(textFormat <- latex2exp::TeX(text1))

# OK : example with two symbols '<'
(text1 <- paste0("for $0<t<t_{c}$"))
(textFormat <- latex2exp::TeX(text1))

# plot
y <- seq(max(length(text)), 0.5, length.out = length(text))
ggplot() +
  annotate("text", x = -1, y = y, size=4.5, hjust = 0, label = paste(textFormat), parse = TRUE) +
  coord_cartesian(xlim = c(-1, 1), ylim = c(-1,max(length(text))+1))

The error send is:

Erreur dans value[[3L]](cond) : Error while converting LaTeX into plotmath.
Original string: for $0\leq t \leq t_{c}$
Parsed expression: 'for '*0 <= t <= t[c]
Error in str2expression(rendered): <text>:1:15: '<=' inattendu(e)
1: 'for '*0 <= t <=
                  ^

I have tried many things to no avail. I would like some help to solve my problem

stefano-meschiari commented 2 years ago

It's a bug -- I have a workaround that currently applies when using =, < and >, but not \leq and \geq. This will get fixed in the next minor release.

In the meantime, a workaround is to surround the first inequality in an invisible group (\left. and \right.), like so:

text <- "for $\\left. 0\\leq t \\right. \\leq t_{c}$"
plot(TeX(text1))

which produces

image
aursiber commented 2 years ago

Thank you for your quick response and your correction ! But it seems that it does not fix the problem when the plot is included in a shiny app. But maybe it has nothing to do with the latex2exp package ...

library(shiny)
library(ggplot2)
shiny::shinyApp(
  ui = fluidPage(
    plotOutput("plot", click = "plot_click")
  ),

  server = function(input, output) {
    output$plot <- renderPlot({ 
      text1 <- "for $\\left. 0\\leq t \\right. \\leq t_{c}$"
      textFormat <- latex2exp::TeX(text1)
      y <- seq(max(length(text)), 0.5, length.out = length(text))
      ggplot() +
        annotate("text", x = -1, y = y, size=4.5, hjust = 0, label = paste(textFormat), parse = TRUE) +
        coord_cartesian(xlim = c(-1, 1), ylim = c(-1,max(length(text))+1))
    })
  }
)
stefano-meschiari commented 2 years ago

No problem!

I ran your Shiny code using the current version of latex2exp on CRAN and it appears to work correctly on my system:

image

I'd recommend attaching the output of sessionInfo() to your bug. The current version of latex2exp published on CRAN and installed on your system should be 0.9.0.

aursiber commented 2 years ago

Thank you for testing on your machine. And thanks for your help. All R packages on my machine are up to date. I suspected that it was not related to your package but rather to a linux library. But I can't find anything that fixes my display bug. Especially since there is no error message to guide me :-(

See below my sessionInfo()

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 LTS

Matrix products: default
BLAS:   /usr/share/R-4.1.2/lib/libRblas.so
LAPACK: /usr/share/R-4.1.2/lib/libRlapack.so

locale:
 [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C               LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8     LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=fr_FR.UTF-8   
 [7] LC_PAPER=fr_FR.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] ggplot2_3.3.5 shiny_1.7.1  

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.1 xfun_0.29        bslib_0.3.1      shinyjs_2.1.0    purrr_0.3.4      latex2exp_0.9.0  colorspace_2.0-2 vctrs_0.3.8      generics_0.1.1   htmltools_0.5.2 
[11] utf8_1.2.2       rlang_1.0.0      later_1.3.0      pillar_1.6.5     jquerylib_0.1.4  glue_1.6.1       withr_2.4.3      DBI_1.1.2        lifecycle_1.0.1  stringr_1.4.0   
[21] munsell_0.5.0    gtable_0.3.0     evaluate_0.14    labeling_0.4.2   knitr_1.37       fastmap_1.1.0    Cairo_1.5-14     httpuv_1.6.5     fansi_1.0.2      Rcpp_1.0.8      
[31] xtable_1.8-4     promises_1.2.0.1 scales_1.1.1     cachem_1.0.6     jsonlite_1.7.3   mime_0.12        farver_2.1.0     digest_0.6.29    stringi_1.7.6    dplyr_1.0.7     
[41] grid_4.1.2       cli_3.1.1        tools_4.1.2      magrittr_2.0.2   sass_0.4.0       tibble_3.1.6     crayon_1.4.2     pkgconfig_2.0.3  ellipsis_0.3.2   assertthat_0.2.1
[51] rmarkdown_2.11   R6_2.5.1         compiler_4.1.2