markdown::markdownToHTML does not correctly handle Mathjax on rstudio.cloud

Hi,

the function markdownToHTML in the package markdown (current CRAN version 0.8) does not seem to correctly convert mathjax syntax on rstudio.cloud, but it works on my local windows notebook.

Here is a simple example:

library(markdown)
res = markdownToHTML(text = "MATH HERE: $x^2$", fragment.only=TRUE)
cat(res)

On my windows notebook, I get the correct mathjax compatible output:

<p>MATH HERE \(x^2\)</p>

Yet, running the same code on rstudio cloud yields the wrong HTML

<p>MATH HERE: $x<sup>2$</sup></p>

that Mathjax cannot handle. Interestingly, "mathjax" is set as default option on both my local notebook and rstudio.cloud:

#  "mathjax" option is set in both environments
markdownHTMLOptions()
> markdownHTMLOptions()
 [1] "skip_html"      "skip_style"     "skip_images"    "skip_links"     "safelink"      
 [6] "toc"            "escape"         "fragment_only"  "hard_wrap"      "use_xhtml"     
[11] "smartypants"    "base64_images"  "mathjax"        "highlight_code"

Also a possible workaround to directly write the markdown code in native mathjax format, like \(x^2\) instead of $x^2$ does not work. While the RMarkdown package may work, it is much slower than markdown::markdownToHTML and not well suited for my application.

To replicate the problem, you can also run the code in the file mathjax_test.R in the following public cloud project: https://rstudio.cloud/project/46324

So I learned about reprex yesterday (Thanks @mara!). However, I suspect that uses rmarkdown for it's rendering:

# markdownToHTML does not correctly handle mathjax syntax
# on rstudio.cloud (works on local windows notebook)
library(markdown)
res = markdownToHTML(text = "MATH HERE: $x^2$", fragment.only=TRUE)
cat(res)
#> <p>MATH HERE: \(x^2\)</p>
# Should be: <p>MATH HERE \(x^2\)</p>
# but is: <p>MATH HERE: $x<sup>2$</sup></p>

# Yet "mathjax" option is set
markdownHTMLOptions()
#>  [1] "skip_html"      "skip_style"     "skip_images"    "skip_links"    
#>  [5] "safelink"       "toc"            "escape"         "fragment_only" 
#>  [9] "hard_wrap"      "use_xhtml"      "smartypants"    "base64_images" 
#> [13] "mathjax"        "highlight_code"


# Does not help to directly use \( \) syntax
res = markdownToHTML(text = "MATH HERE: \\(x^2\\)", fragment.only=TRUE)
cat(res)
#> <p>MATH HERE: \(x^2\)</p>

Created on 2018-07-12 by the reprex
package
(v0.2.0).

@yihui do you have any ideas as to why the behavior would differ between a local IDE or an RStudio Server, where it behaves as expected, and rstudio.cloud where it does not?

1 Like

Sorry, I can reproduce the problem on rstudio.cloud, but I don't have an idea. I tested it on Ubuntu/macOS locally, and couldn't reproduce it. It seems to be an issue specific to rstudio.cloud. Unfortunately, debugging this issue requires C skills, which I don't have.