markdown::markdownToHTML does not correctly handle Mathjax on




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

Here is a simple example:

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

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

#  "mathjax" option is set in both environments
> 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:


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 (works on local windows notebook)
res = markdownToHTML(text = "MATH HERE: $x^2$", fragment.only=TRUE)
#> <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
#>  [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)
#> <p>MATH HERE: \(x^2\)</p>

Created on 2018-07-12 by the reprex

@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 where it does not?


Sorry, I can reproduce the problem on, 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 Unfortunately, debugging this issue requires C skills, which I don't have.