Skip to content

fix: re-typeset LaTeX content after any update - #40

Merged
pyramation merged 1 commit into
mainfrom
fix/latex-retypeset-on-update
Aug 25, 2026
Merged

fix: re-typeset LaTeX content after any update#40
pyramation merged 1 commit into
mainfrom
fix/latex-retypeset-on-update

Conversation

@pyramation

Copy link
Copy Markdown
Collaborator

Summary

A client-side navigation on a page that already rendered math un-rendered it: equations reverted to raw $$…$$ source. Reproduced on LaTeX2JS.com by fresh-loading the homepage and clicking the "LaTeX2JS" home link — mjx-container count dropped 31 → 13 and the raw source came back.

Two things combined:

  • render() built new LaTeX2HTML5() and re-parsed on every render, so an unrelated re-render produced brand-new element objects; React tore down and rebuilt the dangerouslySetInnerHTML nodes, discarding MathJax's typeset output and restoring raw source.
  • componentDidUpdate only typeset when the content prop changed, so nothing put the math back:
-componentDidUpdate(prevProps) {
-  if (prevProps.content !== this.props.content && this.state.mathJaxLoaded) {
+componentDidUpdate() {
+  if (this.state.mathJaxLoaded) {
     this.typesetMath();

The parse and the resulting child elements are now cached on the instance keyed by content, so an unchanged content re-render reuses the same elements and the DOM is left alone; caching the parse alone was not enough — PSTricks children still churned. Typesetting on every update is the safety net for the cases where the markup does get rewritten (MathJax skips math it has already typeset). The mount path drops its explicit typesetMath() because the mathJaxLoaded setState now flows through componentDidUpdate.

Verified in a browser against LaTeX2JS.com built with this package: 31 mjx-container / 0 raw $$ on fresh load, unchanged after three consecutive home-link navigations.

homepage after three navigations

Link to Devin session: https://app.devin.ai/sessions/e51e8ecb94d241bd8d89e65e642c3e43
Requested by: @pyramation

@devin-ai-integration

Copy link
Copy Markdown

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@pyramation
pyramation merged commit f85e984 into main Aug 25, 2026
4 checks passed
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.

1 participant