stefano-meschiari / latex2exp

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

Consecutive Spaces are not working in v0.9.0 #43

Closed shivam7898 closed 2 years ago

shivam7898 commented 2 years ago

Hi, Thank you for your excellent package. It has helped me a lot. Following Code does not work in version 0.9.0. However I have reverted to version 0.5.0 and it works in that.

# #ERROR in latex2exp_0.9.0
c(TeX(r'($\,\,\mu - 3 \sigma$)'))
#
#Error in value[[3L]](cond) : Error while converting LaTeX into plotmath.
#Original string: $\,\,\mu - 3 \sigma$
#Parsed expression: phantom() * phantom(.) *  * phantom(.) * mu - 3*sigma
#Error in str2expression(rendered): <text>:1:27: unexpected '*'
#1: phantom() * phantom(.) *  *
#
# # This modification works in v0.9.0
c(TeX(r'($\,\mu - 3 \sigma$)'))
# #Works in latex2exp_0.5.0
c(TeX(r'($\,\,\mu - 3 \sigma$)'))
#expression(`$\,\,\mu - 3 \sigma$` = paste("", "", 
#    paste(" "), "", paste(" "), "", mu, 
#    , , , phantom() - phantom(), "3", "", sigma, 
#    , , , ""))
#

Edit: Sorry, forgot to add SessionInfo


# #SessionInfo after reverting to version 0.5.0
sessionInfo()
#R version 4.1.2 (2021-11-01)
#Platform: x86_64-w64-mingw32/x64 (64-bit)
#Running under: Windows 10 x64 (build 19042)
#
#Matrix products: default
#
#locale:
#[1] LC_COLLATE=English_India.1252  LC_CTYPE=English_India.1252    LC_MONETARY=English_India.1252 LC_NUMERIC=C                  
#[5] LC_TIME=English_India.1252    
#system code page: 65001
#
#attached base packages:
#[1] stats     graphics  grDevices datasets  utils     methods   base     
#
#other attached packages:
#[1] latex2exp_0.5.0
#
#loaded via a namespace (and not attached):
# [1] compiler_4.1.2  magrittr_2.0.2  bookdown_0.24   fastmap_1.1.0   cli_3.1.1       htmltools_0.5.2 tools_4.1.2    
# [8] yaml_2.2.2      stringi_1.7.6   rmarkdown_2.11  knitr_1.37      stringr_1.4.0   xfun_0.29       digest_0.6.29  
#[15] rlang_1.0.0     evaluate_0.14 

Edit 2: And now I have realised that this might be a duplicate of what you have already handled in the closed issue given Here

stefano-meschiari commented 2 years ago

Thanks for the report!

Could you check if you can still reproduce this bug with 0.9.3 (PR #44)?

You can install it via

devtools::install_github("stefano-meschiari/latex2exp", ref="0.9.3")

Please ensure that the correct version is loaded by checking the output of sessionInfo().

shivam7898 commented 2 years ago

Thanks for the update. It works. However another code throws similar error. (Edit: Added another code with sprintf())

library("latex2exp") 
packageVersion("latex2exp") #From Github
#[1] ‘0.9.3’
# #Original issue has been resolved. Following Code Works.
c(TeX(r'($\,\,\mu - 3 \sigma$)'))
#expression(phantom() * phantom(.) * phantom() * phantom(.) * 
#    mu - 3 * sigma)
# #However this code throws similar ERROR
TeX(r'($\bar{x} \, (\neq x)$)')
#Error in value[[3L]](cond) : Error while converting LaTeX into plotmath.
#Original string: $\bar{x} \, (\neq x)$
#Parsed expression: bar(x) * phantom(.) * ( * {phantom() != phantom()} * x)
#Error in str2expression(rendered): <text>:1:25: unexpected '*'
#1: bar(x) * phantom(.) * ( *
#                            ^
#
# #Version 0.5.0 Output
#expression(`$\bar{x} \, (\neq x)$` = paste("", "", 
#    bar(paste("x")), "", paste(" "), "", 
#    "(", "", phantom() != phantom(), "x", ")", 
#    "", ""))

Edit: Added another testcase

TeX(sprintf("($\\mu$=%.0f) $E(\\bar{x}) \\, =$%.0f; $\\sigma_{\\bar{x}} \\, =$%.0f", 10, 12, 14))
#Error in value[[3L]](cond) : Error while converting LaTeX into plotmath.
#Original string: ($\mu$=10) $E(\bar{x}) \, =$12; $\sigma_{\bar{x}} \, =$14
#Parsed expression: '('mu*'=10) '*E(bar(x)) * phantom(.) * phantom() * {phantom() == phantom()} * '12; '*sigma[bar(x)] * phantom(.) * phantom() * {phantom() == phantom()} * '14'
#Error in str2expression(rendered): <text>:1:4: unexpected symbol
#1: '('mu
#       ^
# #Version 0.5.0 Output
#expression(`($\mu$=10) $E(\bar{x}) \, =$12; $\sigma_{\bar{x}} \, =$14` = paste("", 
#    "(", "", "", mu, , , , "=10", ")", 
#    " ", "E", "(", "", bar(paste("x")), 
#    ")", "", "", paste(" "), phantom() == 
#        phantom(), "12", "; ", "", sigma, , 
#    , , phantom()[{
#        paste("", bar(paste("x")))
#    }], "", paste(" "), phantom() == phantom(), "14"))
sessionInfo()
#R version 4.1.2 (2021-11-01)
#Platform: x86_64-w64-mingw32/x64 (64-bit)
#Running under: Windows 10 x64 (build 19042)
#
#Matrix products: default
#
#locale:
#[1] LC_COLLATE=English_India.1252  LC_CTYPE=English_India.1252    LC_MONETARY=English_India.1252 LC_NUMERIC=C                  
#[5] LC_TIME=English_India.1252    
#system code page: 65001
#
#attached base packages:
#[1] stats     graphics  grDevices datasets  utils     methods   base     
#
#other attached packages:
#[1] latex2exp_0.9.3
#
#loaded via a namespace (and not attached):
#[1] compiler_4.1.2 magrittr_2.0.2 tools_4.1.2    stringi_1.7.6  stringr_1.4.0 
stefano-meschiari commented 2 years ago

Thanks for the report, I will take a look at this today and add it to the test suite!

stefano-meschiari commented 2 years ago

I pushed a new commit of 0.9.3 (PR #44) that should address this edge case.

Essentially, with the previous version you tested, entering unbalanced parentheses will result in an invalid expression. A small example is trying to convert TeX("$)($") into an expression -- TeX would essentially try to create str2expression(")("), which is not valid R syntax (all expressions need to be valid R).

As a workaround, when the code detects that there are unbalanced parentheses, or parentheses in positions that would result in invalid R code (e.g. your example $(\neq x)$ which would result in expression((!= x))), I render parentheses using a different strategy that produces the correct visual output, but makes the expression code itself a bit less tidy. I added your examples to the test suite.

If you have a chance, could you confirm this fixes your issue by reinstalling the 0.9.3 branch?

shivam7898 commented 2 years ago

It works perfectly. Thank You :)

This & This are the sample of images that I am able to generate with the help of your excellent package.

Edit: Thank you for explaining the reasoning behind it. Highly appreciated :)

stefano-meschiari commented 2 years ago

0.9.3 was merged into master. I will post it to CRAN in the near future.

Those are great plots! is there any chance that you could provide the source code for either, so that I could add them to a "Gallery" vignette? No worry if you'd rather not.

shivam7898 commented 2 years ago

Irrespective of your future decision on the plots, your comment already made my day :) Thank You. I would be happy to share the code.

This Repo contains my notes about a course I have undertaken. docs directory contain a single standalone HTML file which can be searched for code of any of the images using the caption (given at bottom right of each image). All other images are in the images directory.

Plots shown in the previous messages has following captions: (You can search in the HTML)

Histogram : Search for "B12P08" in HTML without quotes Density Plot: Search for "B12P02" in HTML without quotes

While the code should be completely reproducible, I might have missed something. Please let me know. I would provide a complete reprex if you need that :)

Edit: (Because I have edited 100% of all of my comments so far in this thread!)

You do not have to search anything. This Folder contains separate codes for both of the images. Let me know if you require anything else.

:)