Math not rendering correctly in RStudio

I'm using RStudio in Ubuntu, and I'm having trouble getting math to render properly:

It does render in the edit view, oddly enough, although I can't include the image because I'm a new use (one upload only).

A friend tried the same code on Mac and got the same result. Some intial googling didn't turn up anything helpful. Any potential fixes people can think of?

Here's what the view looks like in the editor:

I'm having trouble getting math to render properly:

What is this capture about ? Is this the result of knitting to HTML ? or PDF ?
It seems you got the correct preview in the editor so Math is ok.

I am guessing HTML ; In HTML math is rendered by Mathjax. You may need to check in the browser console (F12) if there are some issue with the loading of the JS library.

Are you using self_contained ?

Thanks for bringing up different formats, I hadn't considered that aspect of it. It's a result of knitting to output: github_document, so a markdown file. When I try some of the other formats here I get

  • html_document: math renders properly
  • pdf_document: math renders properly
  • md_document: I get the following error:
[WARNING] Could not convert TeX math 'F_X(A) \equiv \sum \frac{\text{Cases in set }A}{\text{Total Cases in }X}.', rendering as TeX
[WARNING] Could not convert TeX math '\begin{aligned} 
    A &= \{2, 4, 6\}, \\ 
    X &= \{3, 5, 5, \mathbf{2}, \mathbf{6}, 1, 3, 3\}, \\
    F_X(A) &= \frac{2}{8} = 1/4
    \end{aligned}', rendering as TeX

Which then when viewed in RStudio isn't rendered correctly. So that gives me a way to view these, thanks! Any idea what the problem is there? I looked up self_contained but it appeared to be HTML related.

It makes sense that math in the markdown format is just some formatted markup text, what surprises me is that the RStudio markdown display keeps it in the md format. If I'm understanding your reply right, the Rmarkdown displayer (Pandoc) does that on purpose, which seems odd. I'm sure they have reasons, and it sounds like other people have requested it.

Markdown, Emacs Org mode, ConTeXt, ZimWiki
It will appear verbatim surrounded by ... (for inline math) or $$...$$ (for display math).

Thanks for the explanation!

This may be because of my inexperience with markdown, but when I'm knitting and viewing I think of that as the render step. I think of it the same way I think of LaTeX - there's the plaintext version with all the formatting syntax, then when you render it that is translated to the final visual version. That's what I was expecting with the RStudio knit view, but, like I said, I don't have much experience with markdown.

I agree with this ! However, here we are in a specific case and you have the correct final visual version for the format you are using.

As you choose output: github_document, the final version is a Markdown document not a HTML document, so the final visual version is with markdown syntax.

There is a preview offered that opens in the IDE "asif" it was renderered on github. It won't be exactly the same as when previewed on Github (because Github has its own markdown renderer that will convert a .md file using GFM syntax to a HTML view.
If you want to have the html preview locally, you can set keep_html = TRUE for this format. That way the html file generated for preview won't be in a temp folder (default behavior) but save in the working directory.

If you use another output format (html_document, docx_document, pdf_document, ...), you'll have a final version that is the final format output.

Your question here is highly related to the use of github_document format, which is a variant of md_document which use GFM syntax. It is Markdown as input, and as output but not with the same markdown flavor.

Hope it helps understand.

Ah, thanks, that helped connect all the dots for me. It renders it as github would, and github doesn't render md math, as you mentioned earlier. Thanks for all of the explanations!

1 Like

Math will work in HTML output through the use of Mathjax, and PDF output thanks to Latex.

It won't work in markdown output natively. Because what is Math in the .md format ? Just some markup for a processor to handle. Rmarkdown, by using Pandoc, will keep the math code as is in the md output

So at the end, it will depend on the renderer you use to preview. For example, Github does not support math in markdown with GFM . (See FR:

There is currently a suggestion in Rmarkdown to allow improvement:

So it is not an issue with RStudio, but more a limitation of the format you are trying to use.

I am curious about what you would have expected the output to be in a .md file. Can you share your thought ? I am not sure to see what could be done in a Markdown file apart from converting Math to an image to be included in the output. But you may have other thing in mind. Would you be willing to share ?

About "Rmarkdown displayer", Pandoc is used by rmarkdown to render .Rmd file to .md, .html or .tex file. The result depends on the output format and what Pandoc supports.

Thank you