Skip to content

upgrade inline math previews to mathjax 4 - #18210

Merged
kevinushey merged 4 commits into
mainfrom
feature/mathjax4
Jul 14, 2026
Merged

kevinushey merged 4 commits into
mainfrom
feature/mathjax4

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

Addresses #8715.

This is phase 1 of the MathJax upgrade discussed in #8715: the IDE's inline LaTeX / math previews (source editor line widgets and popup, and the visual editor's live math preview via PanmirrorUIMath) now render with MathJax 4.1.3, while rendered R Markdown documents are untouched -- rmarkdown's mathjax = "local" mode still uses the bundled MathJax 2.7.9 via RMARKDOWN_MATHJAX_PATH (rmarkdown hardcodes the v2 MathJax.js?config= entry point, so swapping that bundle requires coordinated rmarkdown changes; that's phase 2).

What's included

  • Dependency: install-mathjax now also installs a mathjax-4 bundle. It first tries mathjax-4.1.3.zip from rstudio-buildtools, and falls back to assembling the bundle from npm registry tarballs (dependencies/tools/build-mathjax4, which prunes the mathjax@4 package and adds the @mathjax/mathjax-newcm-font CHTML resources under fonts/mathjax-newcm-font). dependencies/tools/upload-mathjax4.sh builds and uploads the zip.
  • Serving: new external-mathjax4-path session option (default resources/mathjax-4), installed by CMake next to mathjax-27, and served at the /mathjax4/ URI (registered before /mathjax, since URI handlers match by prefix in registration order). The server-side /mathjax proxy forwards /mathjax4/ by prefix, so no server changes are needed.
  • GWT port: MathJaxLoader now sets a v4-style config (inline $/display $$ TeX delimiters, ui/safe, menu/speech/enrichment disabled, output.fontPath pointed at the locally served font so woff2 fonts and dynamic glyph-range data never hit the CDN) and loads mathjax4/tex-mml-chtml.js. MathJaxTypeset is ported from MathJax.Hub.Queue/Typeset/getAllJax to MathJax.typesetPromise, keeping the restore-previous-render-on-error behavior via a data-mathjax-last-text attribute. The v2-era hidden-retry hack is gone.

Notable findings

  • MathJax 4 silently ignores cross-realm configs. Its option merger only accepts objects whose constructor is identity-equal to the host window's Object; a config literal created inside the GWT frame fails that check and the whole config is dropped (symptom: $$ math worked, $...$ didn't, fonts fell back to jsdelivr). The loader now re-creates the config in the host realm via $wnd.JSON.parse(JSON.stringify(config)).
  • v4 failure modes differ from v2: malformed TeX (e.g. unbalanced braces) is skipped by the math finder entirely (no output, no error node), while recoverable TeX errors render mjx-merror nodes; undefined macros render as red text via the bundled noundefined package. Error detection accounts for both.

Action needed before Windows CI can build this

dependencies/windows/install-dependencies.cmd expects mathjax-4.1.3.zip on the rstudio-buildtools S3 bucket; someone with AWS credentials needs to run dependencies/tools/upload-mathjax4.sh once. macOS/Linux builds work without it via the npm-registry fallback.

Developers will need to re-run install-mathjax (or install-dependencies) after pulling, since CMake now validates the mathjax-4 directory.

Testing

  • New Playwright coverage in e2e/rstudio/tests/panes/editor/math_preview.test.ts: MathJax 4 loads from the local bundle with the RStudio config applied, display math renders as a line-widget preview, and inline math renders a popup preview. All pass against the dev desktop build.
  • Standalone browser verification of the bundle: local font/dynamic glyph loading (no CDN requests), TeX error rendering, and the restore-on-error flow.
  • ant javac / ant draft, backend build, and e2e typecheck all clean.

Bundle MathJax 4.1.3 (with the mathjax-newcm font's CHTML resources)
alongside the existing MathJax 2.7.9 bundle, serve it from the new
/mathjax4/ session URI, and port the GWT inline LaTeX / math preview
subsystem (source editor line widgets and popup, visual editor preview)
from the removed MathJax.Hub v2 API to the v4 typesetPromise API.

Rendered R Markdown documents are unchanged: rmarkdown's local mode
still consumes the 2.7 bundle via RMARKDOWN_MATHJAX_PATH.

Addresses #8715.
@kevinushey kevinushey added this to the 2026.08 (Yellow Yarrow) milestone Jul 13, 2026
@posit-snyk-bot

posit-snyk-bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

The script cds into RSTUDIO_TOOLS_ROOT before the MathJax 4 section, so
the lazily computed relative dirname no longer pointed at the script's
own directory. Resolve the script directory up front, and remove the
empty zip a failed buildtools download leaves behind.
While typing, incomplete expressions (e.g. a trailing subscript) briefly
rendered a TeX error before the previous render was restored. Typeset
into a hidden scratch sibling instead, and only swap the output into the
visible element when it typesets cleanly; on failure the previous render
never leaves the DOM. When there is no previous render to preserve, the
error output is still shown.
@github-actions

github-actions Bot commented Jul 14, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

#################### 100% pass rate

Passed Failed Skipped Flaky
1822 0 67 2
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ desktop-linux 458 0 14 1
✅ desktop-macos 463 0 10 0
✅ desktop-windows 459 0 16 0
✅ server-linux 442 0 27 1
View / download the full report

View report online -- opens in the browser, no download needed. Available for 14 days.

Download playwright-report.zip -- extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


All platforms - commit c858c569df183ac04313ef2c867186d26864b746 - Run #162

@kevinushey
kevinushey merged commit 65fc69a into main Jul 14, 2026
27 of 29 checks passed
@kevinushey
kevinushey deleted the feature/mathjax4 branch July 14, 2026 04:46
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.

2 participants