Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve support for math rendering with CDN #2833

Merged
merged 11 commits into from
Mar 2, 2025
Merged

Conversation

jayhesselberth
Copy link
Collaborator

@jayhesselberth jayhesselberth commented Dec 20, 2024

This PR includes katex and mathjax dependencies via the head template instead of via cached_dependency().

This simplifies the katex loading, which otherwise would require caching ~60 katex files.

But once katex material moved to the head template, then it doesn't make sense to have a separate approach for mathjax, so that is moved to the head template too.

Closes #2704

Copy link

github-actions bot commented Dec 20, 2024

@jayhesselberth
Copy link
Collaborator Author

@hadley I think the CDN approach for katex is the best we're going to do. We'd have to cache ~60 files to fully support katex outside of CDN.

@nanxstats
Copy link

Thanks @jayhesselberth for this fix. I ran some manual tests on this branch across different math-rendering options in _pkgdown.yml (under bootstrap: 5). Below are the current results:

math-rendering Function reference Articles Notes
"mathml" (default) Unrendered Renders Inconsistent behavior
"katex" Renders Unrendered Inconsistent behavior
"mathjax" Renders Renders MathJaX web fonts didn't load - suboptimal appearance

@jayhesselberth
Copy link
Collaborator Author

jayhesselberth commented Jan 3, 2025

Articles need katex-auto.js to render correctly.

The complex math example (test-math-examples) doesn't render correctly in the netlify preview because we don't set math-rendering: katex in this PR.

@jayhesselberth
Copy link
Collaborator Author

jayhesselberth commented Jan 3, 2025

@nanxstats Can you confirm this fixes katex for you?

What do you mean by "function reference" in the table above — math included in params and examples? I'm not sure why mathml is not rendering that correctly.

@nanxstats
Copy link

@jayhesselberth Yes, "function reference" means the function documentation pages under reference/, where math is written with the \eqn{} \deqn{} syntax.

Overall results

Here is how it looks after the latest patch - KaTeX is now ✨perfect✨

math-rendering Function reference Articles Notes
"mathml" (default) Unrendered Renders Inconsistent behavior
"katex" Renders Renders Perfect
"mathjax" Renders Renders MathJaX web fonts didn't load - suboptimal appearance

Load MathJaX web fonts

You can configure MathJaX via the fontURL JS option to load fonts from CDN like below, similar to the KaTeX method.

In R/render.R, after the out$uses_katex line, add:

out$uses_mathjax <- config_math_rendering(pkg) == "mathjax"

In inst/BS5/templates/head.html, after the {{#uses_katex}} block, add the following {{#uses_mathjax}} block:

{{#uses_mathjax}}
<script>
  window.MathJax = {
      chtml: {
          fontURL: "https://cdn.jsdelivr.net/npm/[email protected]/es5/output/chtml/fonts/woff-v2"
      }
  };
</script>
{{/uses_mathjax}}

On MathML

Yep, under mathml, math on function reference pages is not parsed as math at all, indicating any fix is probably more involved. I won't use the mathml option because it will likely render something suboptimal when any serious math is involved, so not fixing mathml is not a blocker.

I do support considering KaTeX as the future default, like you mentioned in #2704.

@jayhesselberth jayhesselberth changed the title Improve support for katex math rendering. Improve support for math rendering with CDN Jan 6, 2025
@nanxstats
Copy link

Nice, I can confirm MathJax now also loads web fonts properly:

math-rendering Function reference Articles Notes
"mathml" (default) Inconsistent behavior, maybe for future fix
"katex" Perfect
"mathjax" Perfect

I have no more comments.

@jayhesselberth

This comment was marked as resolved.

@jayhesselberth
Copy link
Collaborator Author

The only other thing I would do here is to add an example using mathml directly for testing purposes, ideally something that can't be used by katex and mathjax

@jayhesselberth jayhesselberth changed the title Improve support for math rendering with CDN fix: improve support for math rendering with CDN Mar 2, 2025
@jayhesselberth jayhesselberth merged commit c368449 into main Mar 2, 2025
17 checks passed
@jayhesselberth jayhesselberth deleted the katex-update branch March 2, 2025 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pkgdown 2.1.0 prevents \eqn{} and \deqn{} math rendering on function reference pages under Bootstrap 5
2 participants