rstudio / blogdown

Create Blogs and Websites with R Markdown
https://pkgs.rstudio.com/blogdown/
1.73k stars 335 forks source link

Pandoc rules regarding LaTeX not followed consistently #467

Closed ghost closed 4 years ago

ghost commented 4 years ago

I can create a new blogdown site utilizing the default hugo-lithium theme and the text statement below will not render as a LaTeX equation, which is the behavior I expect.

An amount between $5 and $10.

Pandoc's manual explains the reason why, which I'll paste below with a bold emphasis:

Anything between two $ characters will be treated as TeX math. The opening $ must have a non-space character immediately to its right, while the closing $ must have a non-space character immediately to its left, and must not be followed immediately by a digit. Thus, $20,000 and $30,000 won’t parse as math. If for some reason you need to enclose text in literal $ characters, backslash-escape them and they won’t be treated as math delimiters.

HOWEVER, I can change to other Hugo themes (e.g. hugo-theme-codex) and this exact same text block renders improperly whenever I include math: true in my YAML.

An amount between $5 and $10. ends up displaying as:

latex and rmd

Why is LaTeX invoked, when Pandoc is supposed to follow the rule I quoted above? How can I use "other" hugo themes, still use LaTeX math equations, and have Pandoc follow it's own rules? Escaping the $ unfortunately doesn't do anything, and displays the same result as the un-escaped version.

# Doesn't work
An amount between \$5 and \$10.

latex and rmd

Getting in on the double slash escape action doesn't seem to work either:

# Doesn't work
An amount between \\$5 and \\$10.

double slash image


By filing an issue to this repo, I promise that

cderv commented 4 years ago

Hi,

Can you provide a reproducible example or a real example of this happening ? We need more informations about how you are using this syntax, particularly inside which files exactly : Rmd, Rmarkdown or md files ? Only the first will be processed by pandoc, the other will be processed by hugo markdown renderer (that does not support Latex Math by default). See the comparison: https://bookdown.org/yihui/blogdown/output-format.html

Also, math: true is added in the yaml of your post right ? I see that the theme you use (https://themes.gohugo.io/hugo-theme-codex/) as example (codex) supports math by setting math: true. That means in this theme, katex will be used to render math.

As you see, there are many things to consider when looking at Math support. This is briefly mention in the bookdown book too

Thanks for your help.

ghost commented 4 years ago

Can you provide a reproducible example or a real example of this happening ?

The simplest reproducible example would be to create a generic blogdown site within R Studio (New Project -> Blogdown), and for the theme enter jakewies/hugo-theme-codex. Then go to the default 2015-07-23-r-rmarkdown.Rmd file and enter the text An amount between $5 and $10. anywhere. You should see the strange formatting I report above.

Also, math: true is added in the yaml of your post right ?

Yes, that is correct.

I see that the theme you use (https://themes.gohugo.io/hugo-theme-codex/) as example (codex) supports math by setting math: true. That means in this theme, katex will be used to render math.

So KaTeX is used instead of MathJax I guess? Unfortunately, I'm not knowledgeable enough on these topics to understand the significance of this.

cderv commented 4 years ago

Thank you I can reproduce what you see now. Slight modification though:

The simplest reproducible example would be to create a generic blogdown site within R Studio (New Project -> Blogdown), and for the theme enter jakewies/hugo-theme-codex. Then go to the default 2015-07-23-r-rmarkdown.Rmd file and enter the text An amount between $5 and $10. anywhere. You should see the strange formatting I report above.

Doing just that won't make the strange formating. It appears correctly - the Rmd file is rendered as HTML using pandoc and the inline math is not touch.

However, if you add math: true in the post yaml header, it will appear in the resulting HTML for Hugo processor to deal with. The theme you use makes special usage of that as it will activate a math mode, by including some javascript code and this will make the strange formatting you see. It is explained in https://github.com/jakewies/hugo-theme-codex/blob/master/exampleSite/content/blog/math-typesetting.md

I believe this is what gets you the strange formatting. This is an included feature of the theme you tried and it does not behave as you expect. I believe the inline $ are processed by this JS script : https://github.com/jakewies/hugo-theme-codex/blob/master/layouts/partials/math.html (it seems to be MathJax used not katex as documented, but the math-typesetting.md shows how to use katex)

This is not a blogdown behavior, or bug, nor related to pandoc here. This is just some math support stuff in templates with Mathjax. You can easily modify the layout partial of your theme to adjust the behavior. Either following their math-typesetting.md example, or what is in the blogdown book: https://bookdown.org/yihui/blogdown/templates.html

Hope it helps.

ghost commented 4 years ago

That does help in clarifying. Thank you for that. It seems I'm left with two options using this theme (and many others like it, that also exhibit the same behavior):

  1. Include math: true in YAML, and be able to write LaTeX/KaTeX equations in my blog posts. Unfortunately this option does not allow you to use dollar signs in the body of your blog posts (as plain text), because anything between two or more dollar signs will be rendered as a LaTeX equations. This is particularly problematic for long sentences with two dollar signs in them. Unfortunately escaping the dollar signs does not seem to help the issue.
  2. Include math: false in YAML (or more simply exclude the math argument) and I'm able to include two or more dollar signs in the text of a blog post, without any negative consequences. Although I'm now unable to write any LaTeX/KaTeX equations! Not good.

Do you have any advice on being able to include two or more dollar signs in the body of my text, and being able to write math equations, while using a blogdown theme other than "lithium", "xmin" or "Academic"? I tried a lot of other themes and most seemed to have this issue.

cderv commented 4 years ago

Do you have any advice on being able to include two or more dollar signs in the body of my text, and being able to write math equations, while using a blogdown theme other than "lithium", "xmin" or "Academic"? I tried a lot of other themes and most seemed to have this issue.

I believe you have a third option: You can modify the theme you are using to make it compatible with Mathjax the way described in the blogdown book (https://bookdown.org/yihui/blogdown/templates.html), and the way https://github.com/yihui/hugo-lithium is doing. For your codex theme:



* Then when you add `math: true` in the config file, or in the yaml header, the theme _codex_ with still activate the partials but it will be processed the way described in blogdown book. This mean you'll be able to use math syntax both in Rmd and md file. See here for the syntax: https://bookdown.org/yihui/blogdown/output-format.html

I let you try. It works in my test. 

You can also modify the `math.html` partial loading mathjax to configure it a different way than the theme is doing: 
https://github.com/jakewies/hugo-theme-codex/blob/master/layouts/partials/math.html
You may be able to configure mathjax another way 🤷‍♂️ 

Hope it helps.
ghost commented 4 years ago

@cderv That sure does help, as it solves this issue!

As a side note, I tried newer MathJax releases (eg 2.7.8 and 3.0.5) on this one line

<script async src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML">

replacing the 2.7.5 with 2.7.8 but that seemed to break this solution. Any idea why newer MathJax versions don't work?

yihui commented 4 years ago

@jhuntergit If v2.7.5 works, I guess you can just stay with this version, unless you need newer versions for new features. Anyway, could you provide a minimal reproducible example so we can run it to see the problem with MathJax 2.7.8? You may provide either a Github repo or a zip file uploaded to this issue. Thanks!