Skip to content

Support all math rendering engines from Pandoc in HTML documents - #1940

Merged
cderv merged 35 commits into
rstudio:mainfrom
atusy:math
Jan 31, 2022
Merged

cderv merged 35 commits into
rstudio:mainfrom
atusy:math

Conversation

@atusy

@atusy atusy commented Oct 30, 2020 •

Copy link
Copy Markdown
Collaborator

This PR adds

  • support for all the math rendering engines supported by Pandoc even if self contained.
output:
  html_document:
    math: katex
output:
  html_document:
    math:
      katex: https://cdn.jsdelivr.net/npm/[email protected]/dist
  • support for user templates
    • note that template must avoid $if(math)$$math$$endif$
    • user templates need self_contained: FALSE following the current implementation.
output:
  html_document:
    mathjax: MathJax-2.7.9/MathJax.js?config=TeX-AMS-MML_HTMLorMML

@atusy

atusy commented Oct 31, 2020

Copy link
Copy Markdown
Collaborator Author

It seems like math rendering fails when runtime is shiny...

@RLesur

RLesur commented Oct 31, 2020

Copy link
Copy Markdown
Contributor

note that template must avoid $if(math)$$math$$endif$

IMO, this is an antipattern choice.
For instance, the Pandoc's default HTML templates use $if(math)$$math$$endif$ (see here and here). It would be awkward that rmarkdown::html_document() would be incompatible with Pandoc's default templates.

@atusy

atusy commented Oct 31, 2020 •

Copy link
Copy Markdown
Collaborator Author

@RLesur
I see...
Currently, html_document() ignores the mathjax argument when template other than "default" is given.
Maybe I should do the same way.

Thanks for the comment.

@py9mrg

py9mrg commented Oct 31, 2020

Copy link
Copy Markdown

Great news! Thanks for doing this. I just happened to find liminal yesterday - wonder if it's any use to get around the issue mentioned in the comments? Maybe completely irrelevant though, my knowledge of all this is almost zero at the moment.

@atusy

atusy commented Oct 31, 2020 •

Copy link
Copy Markdown
Collaborator Author

I dropped the math rendering engine support for user templates with self_contained: TRUE to solve #1940 (comment).
Also, most of the rendering engines now work with runtime: shiny.
I do not understand why, but KaTeX does not work with runtime: shiny...

@RLesur
I fixed the antipattern.
Apart from that, I moved MathJax to <head> and enabled it for self_contained document without a JavaScript trick.
However, we can keep the previous implementation just to make sure the consistent behavior from the older versions.
What do you think?
https://github.com/rstudio/rmarkdown/pull/1940/files#diff-91b59569aab1b937e1e5a6e1559a62de19e04f4c4bad52bfb08b910281e0f2dfR484-R486

@py9mrg
Your welcome. Remark.js uses its own markdown parser, and thus is the different story. Thanks, anyway.
As I mentioned above, I made some improvements to solve the comment.

@py9mrg

py9mrg commented Oct 31, 2020

Copy link
Copy Markdown

Ah yeah sorry, I'm being stupid. I mentioned katex rendering being a solution for offline equation rendering fo xaringan too, I'm getting my wires crossed between the issues. Will post the link there instead.

@jeroen

jeroen commented Jul 7, 2021 •

Copy link
Copy Markdown
Contributor

A new package katex is now on CRAN, which enables server-side math-to-html rendering in R, such that no javascript library is needed in the html document (only the katex css file is needed): https://docs.ropensci.org/katex/reference/katex.html.

@cderv

cderv commented Jul 7, 2021

Copy link
Copy Markdown
Collaborator

Thanks @jeroen ! That is a really great solution. Offering support directly into rmarkdown math: YAML key seems something that would be interesting. I'll look into this.

@cderv cderv left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it has been a long time - sorry for the delay @atusy ! We are focusing on rmarkdown currently so we should get this merge soon.

Improving the math support is important and this is possible an impactful change so I want to go thoroughly through this as there are several options.

I did review the current PR as-is but I have a few generic questions / topic in mind for this PR

Self contained now works better with Pandoc. Including for katex. However, it will create heavy document with longer rendering as it will download the JS lib and insert it in the document. We could then continue to provide some tricks to insert the required JS code. However, we could probably change it.

Patching the template file would be a way to insert it instead of using a JS script to modify the header. We could detect where the $math$ variable in the template is and replace with our content. This way we could support any template with self contained, as long as they have the if clause with the $math$ variable. BTW, this is what is done in Quarto for next generation of this.

Regarding self-contained for KaTeX, we could also default to katex R package (PR #2215) for server side rendering during post processing step. Or just offer a way to opt-in. We need to take this other PR into account for this PR.

Regading the YAML header, I wonder if we should not play with names but have something like this (also Quarto inspired):

output: 
  html_document:
    math: katex
output: 
  html_document:
    math: 
       engine: katex
       url: https://cdn.jsdelivr.net/npm/[email protected]/dist"

Or use exactly like in Pandoc's default file (which Quarto will use)

output: 
  html_document:
    html-math-method:
      method: mathjax
      url: "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"

Doing this could help with transition if we do need to use conversion tool. We could also add cleanly some specific method like katex R package

output: 
  html_document:
    html-math-method: r-katex

I would think the latter to be convenient as this would be how to config with Pandoc directly.

Last topic is the lifecycle of this feature. Mathjax support is here since a long time in rmarkdown and I think we need to think of deprecation. The good and clean practice IMO would be to deprecate mathjax argument properly. For internal function, I think it is also good practice to deprecate just in case it is used somewhere. We don't yet use lifecycle R package for this, but it would be a good opportunity to start IMO. This would help to offer clean lifecycle information.

To be discussed for what to do exactly. I threw ideas here.

Comment thread R/html_document.R Resolved
Comment thread R/html_document_base.R Resolved
Comment thread R/pandoc.R Resolved
Comment thread R/html_document_base.R Resolved
Comment thread inst/rmd/h/default.html Resolved
Comment thread inst/rmd/h/default.html Resolved
Comment thread R/pandoc.R Resolved
Comment thread R/pandoc.R Resolved
Comment thread R/pandoc.R Resolved
@cderv

cderv commented Jan 18, 2022 •

Copy link
Copy Markdown
Collaborator

@atusy Thanks again for this great PR ! Supporting all Maths engine is really important IMO.
Following this review and a discussion with @yihui , is that ok if I take the lead on the PR now ? Does the below seems reasonable to you before I start ?

What we would do:

  • Patch the template now to insert the math part when self_contained = TRUE. Meaning replacing $math$ with some content that we have today in the HTML template. This would allow support custom templates too.

  • Use YAML field to select the method. Are you ok with this names ? Using fixed names seems better to me, all the more because you can't select several math engine.

     ````
     output: 
       html_document:
         math: 
           engine: katex
           url: https://cdn.jsdelivr.net/npm/[email protected]/dist"
    ````
    
     ````
     output: 
       html_document:
         math: mathml
    ````
    
  • Add support for R katex as a specific engine.

     ````
     output: 
       html_document:
         math: r-katex
    ````
    
  • Don't bother with deprecation here. mathjax argument can stay and those internal function can be removed. We'll just need a companion pagedown PR.

@yihui did I miss anything ?

Thanks.

@cderv

cderv commented Jan 25, 2022 •

Copy link
Copy Markdown
Collaborator

I have integrated the first part of the change by refactoring the logic and enabling the YAML syntax described in #1940 (comment)

For now, I believe the change won't break anything as I believe I was cautious to insure backward compatibility

mathjax argument is still taken into account if provided and non default, and a math argument is build based on it. The template as not changed for Mathjax currently.

Next step would be to patch template, meaning putting back

$if(math)$
$math$
$end$

in our template. I don't think other engine use it for now, but it would be better to keep them.
We would be replacing this part of the template by the correct script part, either for MathJaX or for KaTeX.

I don't know how doing that would affect other tooling (like learnr or shiny document) - this would require more tests I believe.

I am currently testing several outputs after adding some unit tests. @yihui @atusy happy to have first review on the new stuff if you have time. Thanks!

@cderv

cderv commented Jan 26, 2022 •

Copy link
Copy Markdown
Collaborator

I have made sure here that math will only be used when mathjax = "default". I need to check in extension format if this is a valide choice or not. I believe this would insure we don't break anything yet. We could take the opportunity to add a deprecation warning to use math argument instead of mathjax (using lifecycle package for example)

Example of message we can get
lifecycle::deprecate_warn(
    "2.11.3",
    "rmarkdown::html_document_base(mathjax)", 
    "rmarkdown::html_document_base(math)")
#> Warning: The `mathjax` argument of `html_document_base()` is deprecated as of rmarkdown 2.11.3.
#> Please use the `math` argument instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.

So I started to check support for other HTML format. I then wondered 'shouldn't we use a htmlDependency() for katex so that it can be easily included in any template ? 🤔 At least any of ours, instead of having to copy paste the HTML part in each template. So I am trying that.

Here are some notes about math support in other outputs that I'll keep updating:

  • html_fragment() sets --mathjax when mathjax = TRUE, should we support other engine using math ? Otherwise backward compatibility
  • ioslides_presentations() is a specific writer which only supports Mathjax. Which should make sure math is not set to something else
  • slidy_presentations() can inherit directly behavior from html_document_base(). If we use a HTML dependency, it makes things a lot simpler. So we're good I think
  • Extensions format like learnr::tutorial() currently does not get out of the box support because of R partial matching in function (see Support all math rendering engines from Pandoc in HTML documents #1940 (comment))

cderv added 2 commits January 26, 2022 16:42
because of R partial mathching of argument in functions. Without this change, passing `math = "katex"` in a format that would pass to `html_document()` in `...` would set `mathjax = "katex"`, which is not expected.
Comment thread R/html_document.R Resolved
@cderv

cderv commented Jan 26, 2022

Copy link
Copy Markdown
Collaborator

Last changes includes

  • Inserting KaTeX HTML in head using a html dependency. This should allow us to bring support to any format as we don't have to modify the Pandoc's template. For example, this works ok now:
output:
  learnr::tutorial:
    math_method: katex
  • For this to work, argument has been renamed to math_method
  • Support has been brought to slidy_presentation() and html_notebook()

I'll see if I can took this opportunity to remove the MathJax part from the template also using a similar trick or do template patching.

A few more tests with shiny runtime and our other formats, and this should be good to merge.

@cderv

cderv commented Jan 28, 2022

Copy link
Copy Markdown
Collaborator

@yihui I tested with more formats and it is working well.

I would suggest the following:

We could merge the current PR which brings support for new engines without changing anything regarding Mathjax. This means that KaTeX dependency will included only when template = "default" is passed to html_document_base(), as for mathajx, which generally support that a custom template is passed with the necessary content.
This is how everything currently works (learnr, flexdashboard, ...) and I think we could improve this in a separate PR.

So I would try to work on improving mathjax integration with a patching process, and no template constraint, probably trying to remove this template constraint, but in another PR.

Is this ok for you merge this one and build the rest upon it ? (R package katex support, github_document() math support, ...)

@cderv
cderv requested a review from yihui January 28, 2022 15:47

@yihui yihui left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think it's a better idea to make the improvements in separate PRs. Thanks!

@cderv cderv changed the title Support variety of math rendering engines on html documents (closes #1283, closes #1724, closes rstudio/bookdown#401) Support all math rendering engines from Pandoc in HTML documents Jan 31, 2022
@cderv
cderv merged commit 9d35270 into rstudio:main Jan 31, 2022
@atusy

atusy commented Feb 1, 2022

Copy link
Copy Markdown
Collaborator Author

Thanks @cderv !

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

next to consider for next release

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Unable to use MathML with rmarkdown MathJax does not render when using a local copy

6 participants