Skip to content

Minimal example of server-side math in rmarkdown - #2215

Closed
jeroen wants to merge 1 commit into
rstudio:mainfrom
jeroen:master
Closed

jeroen wants to merge 1 commit into
rstudio:mainfrom
jeroen:master

Conversation

@jeroen

@jeroen jeroen commented Sep 14, 2021

Copy link
Copy Markdown
Contributor

This is mostly to test right now (it uses a feature in the katex package that is not yet on CRAN).

Here is a minimal example of what server-side math rendering could look like. I used this example math.Rmd. In the front-matter you can flip katex: true to compare the mathjax output vs katex output.

I'd be interested to hear if this seems to work as expected.

@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.

Thanks! This looks good to me! Let's see if other people have any thoughts.

@yihui
yihui requested a review from cderv September 14, 2021 18:27
@jeroen
jeroen marked this pull request as ready for review September 18, 2021 13:48
@jeroen

jeroen commented Sep 18, 2021

Copy link
Copy Markdown
Contributor Author

The new version of katex is now on CRAN.

@cderv

cderv commented Sep 20, 2021

Copy link
Copy Markdown
Collaborator

Thanks @jeroen !

@yihui I believe we should consider this while also looking at the old PR #1940 about support more math engine.

This current PR here suggest to add a katex argument in the output format function. The other (#1940) suggests a more generic math argument to configure various math engine supported by Pandoc. And I believe more option from Pandoc should be supported. It could easily support also support server-side math using katex R package.

Unless we decide that current mathjax is enough while adding server-side rendering using katex, and for more options one could opt-out and use pandoc_args directly.

What do you think ?

cc @atusy who worked on #1940

@yihui

yihui commented Sep 20, 2021

Copy link
Copy Markdown
Collaborator

@cderv I agree with you. I'll revisit this PR when reviewing #1940. Thanks for the reminder!

@jeroen

jeroen commented Sep 20, 2021 via email

Copy link
Copy Markdown
Contributor Author

@cderv

cderv commented Jan 25, 2022

Copy link
Copy Markdown
Collaborator

@jeroen I am currently working on #1940 for broader math support.

Idea is to have YAML like this

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

or

output:
  html_document: 
    math: mathml

I would bring support for server side using katex R package within this new logic
Either using a new engine name

output:
  html_document: 
    math: r-katex

or leveraging the url part

output:
  html_document: 
    math: 
      engine: katex
      url: local

Maybe you have other idea for this ?

We could also have something more related to self_contained = TRUE:
If self_contained = TRUE and math = "katex" and katex R package is installed, then we activate post processing of HTML using the R package.

@cderv

cderv commented Jan 31, 2022

Copy link
Copy Markdown
Collaborator

@jeroen Global math support has been merged now. This can be provided

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

We should leverage this yaml structure for supporting katex R package for server side rendering.

I can do the update on this PR. Do you have suggestion for the way to call the method ? Thanks

@cderv

cderv commented Feb 3, 2022 •

Copy link
Copy Markdown
Collaborator

So I have looked closer to how to support this with our new logic.

From what I understand, katex server side processing with the R package suppose two things:

  • Pandoc has processed the HTML file with a math flag (ideally --katex or --mathjax
  • Pandoc does not include any library in the head of the HTML document.

In the PR it was working mathjax <- FALSE still had the effect of adding --mathjax to the CLI it seems.
I am not sure about this TBH - if you pass through here and can give insight about what render_math_in_html expect, please do.

Anyway, I'll probably do another PR instead of modifying this one if you don't mind.

@jeroen

jeroen commented Feb 7, 2022

Copy link
Copy Markdown
Contributor Author

The render_math_in_html function queries the HTML document for nodes with class="math inline" and class="math display" and substitutes the corresponding math with html.

Inside math inline nodes it looks for patterns with $...$ or \(...\) and in math display nodes it looks for a patterns with $$...$$ or \[...\]. So it should work for pandoc output, either with --mathjax or without.

@cderv

cderv commented Feb 7, 2022

Copy link
Copy Markdown
Collaborator

Thanks for the precision.

So it should work for pandoc output, either with --mathjax or without.

Without setting a math engine would have been great but Pandoc will warn every time it converts to HTML in the case no math processing is done.

[WARNING] Could not convert TeX math 
  \frac{x}{y}
  , rendering as TeX
[WARNING] Could not convert TeX math \frac{x}{y}, rendering as TeX

So ideally we need Pandoc to process Math but have no JS / CSS added in HTML head.

@cderv

cderv commented Feb 7, 2022 •

Copy link
Copy Markdown
Collaborator

For the record, I don't think render_math_in_html works for an HTML processed by pandoc --katex.
I got these warning for the package

Warning: Did not find '$...$' or '\(...\)' equation in:
\frac{x}{y}

Warning: Did not find '$$...$$' or '\[...\]' equation in:
x + y

Warning: Did not find '$$...$$' or '\[...\]' equation in:
\frac{x}{y}

In fact, Pandoc does not wrap equation in special chars when --katex is set

❯ pandoc -t html --katex
display
$$
\frac{x}{y}
$$

inline $\frac{x}{y}$
^Z
<p>display <span class="math display">
\frac{x}{y}
</span></p>
<p>inline <span class="math inline">\frac{x}{y}</span></p>

This would be the cleaner way IMO to apply the post processing for any document

  • Set --katex so that pandoc process Math and create the correct HTML
  • Remove $math$ part in Pandoc's template if any so that no online JS / CSS are added.

For it to work with current rmarkdown:

  • Pandoc needs to be tricked by setting --mathjax flag (even if we won't use Mathjax)
  • So one need to be sure no Mathjax resources are added into <head> by Pandoc or rmarkdown. Currently done with our default template where we have already removed this part. But for other Pandoc template, it would require some processing (which will be the case for our other HTML formats).

This means that with non default template, setting

html_document:
  math_method: r-katex

will add the CSS and JSS for Mathjax. Not ideal.

Am I right to believe that it would make sense for katex R package to work on the result of pandoc --katex ?
Is this something that could be handled in katex or it is tied to Mathjax like syntax ? I can open an issue

Anyway, I have opened #2304 to implement this based on current rmarkdown main instead of rebasing this PR. Hope it is fine.

@cderv

cderv commented Feb 14, 2022

Copy link
Copy Markdown
Collaborator

Closing this as #2304 has been merged. Thanks!

@cderv cderv closed this Feb 14, 2022
@cderv
cderv removed their request for review February 14, 2022 15:19
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants