Skip to content

fix(web): render LaTeX math in chat - #7166

Closed
maximilianigl wants to merge 9 commits into
pingdotgg:mainfrom
maximilianigl:fix/latex-math-rendering-clean
Closed

fix(web): render LaTeX math in chat#7166
maximilianigl wants to merge 9 commits into
pingdotgg:mainfrom
maximilianigl:fix/latex-math-rendering-clean

Conversation

@maximilianigl

@maximilianigl maximilianigl commented Aug 16, 2026

Copy link
Copy Markdown

Assistant messages commonly use LaTeX delimiters, but the chat Markdown pipeline currently renders their contents as plain text because CommonMark consumes the delimiter escapes before remark plugins run.

Readable mathematics is essential across scientific and technical research. It is especially important for AI research, where agent responses routinely need to print loss functions, probability distributions, gradients, matrix operations, and evaluation metrics. Showing that material as raw TeX makes answers harder to inspect, verify, and reuse.

This adds remark-math and KaTeX rendering for inline and display formulas. A source normalizer uses text-node positions reported by the Markdown parser, pairs delimiters, and preserves source length. That keeps task-list offsets valid while leaving code blocks, inline code, link destinations, autolinks, raw HTML attributes, unmatched delimiters, and escaped backslashes untouched.

This is a clean follow-up to #4633 and incorporates the unresolved review feedback there. Thanks to @Varshavia for the original implementation and investigation.

Closes #4585.
Closes #8430.

Verification:

  • 118 focused tests pass across math normalization, ChatMarkdown, clipboard behavior, links, and GitHub alerts
  • @t3tools/web typecheck passes
  • changed files pass lint and formatting
  • integrated dev-client check produced two KaTeX trees with MathML for representative inline and display formulas

Visual confirmation:
Before fix:
image

After fix:
image

Generated with GPT-5.6-Sol using the Codex harness in T3 Code.

Note

Render LaTeX math in ChatMarkdown using KaTeX

  • Adds KaTeX, remark-math, and related unified plugins so ChatMarkdown renders inline and display math in ChatMarkdown.tsx
  • Introduces markdown-math.ts with normalizeLatexMathDelimiters (rewrites \(…\) and \[…\) to remark-math-compatible $$ pairs), remarkPromoteBracketDisplayMath (same-line bracket math gets display styling), and rehypeStripKatexErrorTitle (removes native tooltips from KaTeX errors)
  • Updates markdown-clipboard.ts so copied math serializes back to LaTeX delimiters instead of raw HTML, and clipboard HTML keeps the visible KaTeX branch without duplicate MathML
  • Adds KaTeX base CSS and spacing/scroll styles in index.css
  • Adjusts the sanitizer schema in CHAT_MARKDOWN_SANITIZE_SCHEMA to allow math-inline and math-display code class names and removes global title attributes
  • Risk: CHAT_MARKDOWN_SANITIZE_SCHEMA no longer permits title on any element globally; any component relying on sanitized title attributes from ChatMarkdown output will lose them

Macroscope summarized d29aef9.


Note

Medium Risk
Touches the chat markdown remark/rehype chain and sanitize code class rules; behavior is heavily tested but regressions in markdown edge cases are possible.

Overview
Chat messages can now render inline and display LaTeX instead of showing raw \(...\) / \[...\] text. The pipeline adds remark-math and rehype-katex (with KaTeX CSS), and preprocesses the source via normalizeLatexMathDelimiters so delimiter pairs become $$...$$ using parser text-node positions—skipping code, links, and HTML while keeping length-preserving rewrites for task lists.

Same-line \[...\] is promoted to display math via remarkPromoteBracketDisplayMath, and KaTeX parse errors use the theme destructive color without native title tooltips. Copy/paste serializes KaTeX back to LaTeX in plain text and trims duplicate MathML for rich HTML. List-indentation recovery applies the same math normalization for over-indented bullets. KaTeX runs even when raw HTML parsing is off via a separate rehype plugin set.

Reviewed by Cursor Bugbot for commit d29aef9. Bugbot is set up for automated code reviews on this repo. Configure here.

@coderabbitai

coderabbitai Bot commented Aug 16, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c16b7a6e-d44e-43ce-88aa-84036bdbb78b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added vouch:unvouched PR author is not yet trusted in the VOUCHED list. size:L 100-499 changed lines (additions + deletions). labels Aug 16, 2026

@macroscopeapp macroscopeapp Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed the math rendering changes in apps/web/src/components/ChatMarkdown.tsx for consistency with the existing chat-markdown rendering contracts. Four concrete issues, mostly about math being rendered inconsistently across call sites and about the new generated markup missing the block/copy contracts the other markdown blocks already have.

Posted via Macroscope — UI Consistency

Comment thread apps/web/src/components/ChatMarkdown.tsx Outdated
Comment thread apps/web/src/components/ChatMarkdown.tsx Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Every other rendered-markdown block gets its geometry from the .chat-markdown … rules in index.css (pre gets max-width: 100%; overflow-x: auto, tables get the scroll-fade container, blocks share the 0.65rem vertical rhythm). KaTeX's stylesheet is imported here instead, and it gives .katex-display display: block; margin: 1em 0 with white-space: nowrap on the inner .katex and no overflow handling — so a wide equation overflows the message column and display math spacing does not match sibling blocks.

Suggest importing the vendor sheet from index.css (deterministic order relative to the .chat-markdown rules) and adding the missing block contract next to them, e.g. .chat-markdown .katex-display { margin: 0.65rem 0; overflow-x: auto; overflow-y: hidden; }.

Posted via Macroscope — UI Consistency

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in a0f1693e. The KaTeX vendor stylesheet now loads from index.css, and display equations use the shared 0.65rem block rhythm with max-width and horizontal overflow containment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry, I'm unable to act on this request because you do not have permissions within this repository.

Comment on lines 1814 to 2283

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With output: "htmlAndMathml", KaTeX emits a .katex-mathml subtree that is visually clipped but not aria-hidden, while .katex-html is aria-hidden. The onCopy handler on this container serializes the selection through markdown-clipboard.ts, which skips aria-hidden nodes — so copying a message with math keeps only the MathML branch and yields its glyph text plus the duplicated annotation TeX, with no $/$$ delimiters, breaking the markdown round-trip this serializer exists for.

Suggest teaching the serializer about math: on an element with the katex class, read annotation[encoding="application/x-tex"] and emit $…$ (or $$…$$ inside .katex-display) instead of serializing children.

Posted via Macroscope — UI Consistency

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in a0f1693e. The clipboard serializer now recognizes inline and display KaTeX, reads the application/x-tex annotation, and emits explicit LaTeX delimiters. I used \(…\) and \[...\] rather than single-dollar syntax because single-dollar math is now intentionally disabled. Added round-trip tests for both forms.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry, I'm unable to act on this request because you do not have permissions within this repository.

@maximilianigl
maximilianigl marked this pull request as ready for review August 16, 2026 07:13
Comment thread apps/web/src/markdown-math.ts Outdated

@macroscopeapp macroscopeapp Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One finding on the new .katex-display scroll container in apps/web/src/index.css. The earlier notes on singleDollarTextMath, the missing rehype path for non-raw-HTML rendering, display-math spacing/overflow, and clipboard round-tripping all look addressed.

Posted via Macroscope — UI Consistency

Comment thread apps/web/src/index.css Outdated
Comment thread apps/web/src/markdown-clipboard.ts
Comment thread apps/web/src/markdown-math.ts Outdated
@macroscopeapp

macroscopeapp Bot commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Not approved

Macroscope's review found this PR not approvable — This PR introduces LaTeX rendering across the production chat Markdown pipeline, with substantial new parsing, rendering, sanitization, clipboard, list-recovery, styling, and dependency changes. Its broad impact on existing message rendering exceeds a small isolated fix and merits human review.

You can add or adjust custom eligibility rules. Learn more.

@maximilianigl

Copy link
Copy Markdown
Author

All review findings raised so far have been addressed in a0f1693e, 33c3888d, and 7de1323a:

  • disabled single-dollar math so ordinary currency remains plain text
  • enabled KaTeX consistently for both raw-HTML rendering paths
  • aligned display-math spacing and contained wide equations with usable horizontal scrolling
  • preserved inline/display LaTeX when copying as Markdown and retained the visual KaTeX branch for rich HTML copying
  • preserved same-line \[...\] display semantics without shifting source offsets used by task lists

Verification passes: the full web unit suite (2,569 tests), focused math/clipboard tests, typecheck, lint, formatting, and diff checks.

Two older Macroscope threads may still appear unresolved because the bot reported that it lacks permission to resolve them; both findings were fixed and replied to.

@macroscopeapp macroscopeapp Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Two consistency issues in the new KaTeX rendering path (inline math overflow, and the error branch's hardcoded color + native title). Everything else — the display-math scroll container, the 0.65rem rhythm, the clipboard round-trip, and plugin parity across the parseRawHtml branches — now matches the surrounding .chat-markdown contract.

Posted via Macroscope — UI Consistency

Comment thread apps/web/src/index.css Outdated
Comment thread apps/web/src/components/ChatMarkdown.tsx Outdated
Comment thread apps/web/src/components/ChatMarkdown.tsx

@macroscopeapp macroscopeapp Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed the KaTeX rendering, sanitize-schema, and clipboard changes for UI consistency. The earlier findings (theme error color, missing native title strip, display-math alignment, inline overflow ownership) are addressed. One remaining inconsistency in the new inline math scroll container.

Posted via Macroscope — UI Consistency

Comment thread apps/web/src/index.css Outdated
@maximilianigl
maximilianigl force-pushed the fix/latex-math-rendering-clean branch from 5185eca to ec29d6f Compare August 16, 2026 08:23
Comment thread apps/web/src/markdown-math.ts
@maximilianigl

Copy link
Copy Markdown
Author

Looks like all bots are happy

@QuentinWach

Copy link
Copy Markdown

Thanks for putting this together. I’m affected by the raw-LaTeX chat rendering issue and confirmed the current app still shows formulas as plain text. Could a maintainer take a look when time allows? It would make the app much more usable for technical/ML work.

@Ar-Kareem

Copy link
Copy Markdown

Just adding my two cents. I hope this also adds latex when viewing markdown files in the sidebar.

For example this is a markdown file viewer where the equations are still just text (on the official latest release):

image

@macroscopeapp macroscopeapp Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One finding: the new KaTeX scroll lanes read the unadjusted --border role instead of the contrast-adjusted --contrast-border used by the rest of .chat-markdown. Everything else from the earlier rounds (display-math left alignment, themed error color without a native title, inline overflow ownership with the shared thin-scrollbar treatment, KaTeX round-trip in the clipboard serializer) looks consistent now.

Posted via Macroscope — UI Consistency

Comment thread apps/web/src/index.css Outdated
Comment thread apps/web/src/markdown-math.ts
Comment thread apps/web/src/markdown-math.ts
@maximilianigl

Copy link
Copy Markdown
Author

@Ar-Kareem This PR also covers rendered Markdown files in the sidebar. FilePreviewPanel uses the shared ChatMarkdown renderer, so the same KaTeX pipeline applies there.

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 4784861. Configure here.

Comment thread apps/web/src/markdown-math.ts Outdated
Comment thread apps/web/src/markdown-math.ts Outdated
@t3dotgg

t3dotgg commented Aug 28, 2026

Copy link
Copy Markdown
Member

Note

🤖 GPT-5.6 Sol responding on behalf of Theo

We're closing this PR as we clean up the T3 Code backlog. Thank you for taking the time to put this together.

We do not want to add LaTeX rendering to chat. The same product decision applies to #7739.

If you believe we closed this in error, please reopen the PR and leave a comment explaining what we missed.

@t3dotgg t3dotgg closed this Aug 28, 2026
@QuentinWach

Copy link
Copy Markdown

@t3dotgg Why would you not want LaTeX rendering in the chat? - This is really killing the user experience for all those working in quant. finance, comp. physics & mathematics, etc. for which T3 Code is in principle amazing because we can send off and control simulations across machines easily.

You said yourself that the nice integration of images and graphics is what makes GUIs so superior to TUIs. That logic applies similarly to LaTeX rendering. Codex is winning here by miles at the moment.

@maximilianigl

Copy link
Copy Markdown
Author

Agreed, it seems like an odd choice, especially since all other GUIs support it afaik.

@Rodolphe2005

Copy link
Copy Markdown

So far I loved T3 but I will now stop using it because of that decision. It's hardly understandeable.

@maximilianigl

Copy link
Copy Markdown
Author

I don't think there was a human decision involved, I think the bot just closed it because it thought to be irrelevant. And since Theo and the team are all frontend ppl they don't really run into the need for latex.

@QuentinWach

Copy link
Copy Markdown

I believe so as well. I just wonder how to get this through to @t3dotgg . - For one, this issue and PR should be reopened, I'd say.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Render LaTeX math in chat for research workflows Chat: inline LaTeX math (\( ... \)) renders as raw source instead of formatted math

5 participants