Skip to content

feat(chat): render LaTeX math formulas in assistant messages#886

Merged
hazeone merged 1 commit intomainfrom
cursor/add-latex-math-rendering-c171
Apr 22, 2026
Merged

feat(chat): render LaTeX math formulas in assistant messages#886
hazeone merged 1 commit intomainfrom
cursor/add-latex-math-rendering-c171

Conversation

@hazeone
Copy link
Copy Markdown
Contributor

@hazeone hazeone commented Apr 22, 2026

Summary

Closes #883.

Chat messages now render LaTeX math formulas using KaTeX instead of showing the raw source. Both dollar-sign delimiters ($...$, $$...$$) and OpenAI-style backslash delimiters (\(...\), \[...\]) are supported, matching the delimiter conventions frontier models emit today.

Chat message rendering inline and block LaTeX formulas

What changed

  • Added remark-math, rehype-katex, and katex as dependencies; wired the KaTeX stylesheet import in src/main.tsx.
  • src/pages/Chat/ChatMessage.tsx:
    • Registered remarkMath + rehypeKatex plugins on both the assistant bubble's ReactMarkdown and the thinking-block ReactMarkdown.
    • Introduced normalizeLatexDelimiters() that rewrites \(...\)$...$ and \[...\]$$...$$ so remark-math recognises them. The rewrite skips fenced and inline code spans, so code samples containing \( remain intact.
    • KaTeX is configured with { strict: false, throwOnError: false, output: 'html' } so malformed input from the model degrades to text rather than breaking the render tree.
  • Unit + Electron E2E coverage:
    • tests/unit/chat-message.test.tsx — 5 new cases for inline, display, \(...\), \[...\], and code-span preservation.
    • tests/e2e/chat-latex-rendering.spec.ts — boots Electron with mocked IPC history and asserts .katex and .katex-display elements render for all four delimiter styles.
  • README translations updated in README.md, README.zh-CN.md, README.ja-JP.md, and README.ru-RU.md per the doc-sync rule.

Related Issue(s)

Closes #883 (see also #528).

Type of Change

  • Bug fix
  • New feature
  • Documentation
  • Refactor
  • Other

Validation

  • pnpm run typecheck — passes.
  • pnpm run lint — passes (only a pre-existing unrelated react-hooks/exhaustive-deps warning in src/pages/Chat/index.tsx).
  • pnpm test — 79 files / 517 tests pass (5 new tests cover LaTeX rendering).
  • pnpm exec playwright test tests/e2e/chat-latex-rendering.spec.ts — passes under Xvfb/VNC; full chat-task-visualizer.spec.ts also still passes (regression check).
  • pnpm run build:vite — produces a clean bundle; KaTeX font assets are emitted alongside the app bundle.
  • Manual screenshot (above) captures all four delimiter styles rendered correctly in a live Electron window.

Checklist

  • I ran relevant checks/tests locally.
  • I updated docs if behavior or interfaces changed.
  • I verified there are no unrelated changes in this PR.

To show artifacts inline, enable in settings.

Open in Web Open in Cursor 

Adds KaTeX-powered LaTeX math rendering in chat bubbles so expressions
like $E=mc^2$ and $$\int_0^1 x\,dx$$ are rendered inline / block
instead of being shown as raw text.

- New deps: remark-math, rehype-katex, katex (plus katex CSS imported in
  main.tsx).
- ChatMessage now wires remark-math + rehype-katex into ReactMarkdown for
  both the main assistant bubble and thinking blocks.
- Normalises OpenAI-style backslash delimiters (\(...\) and \[...\])
  to dollar-sign form before feeding remark-math, while skipping fenced
  and inline code spans so literal code samples are preserved.
- Unit + Electron E2E coverage for inline, block, and bracket-style
  delimiters.
- README / zh-CN / ja-JP / ru-RU updated to mention LaTeX rendering.

Closes #883

Co-authored-by: Haze <hazeone@users.noreply.github.com>
@hazeone hazeone marked this pull request as ready for review April 22, 2026 04:35
@hazeone hazeone merged commit 1ed9f77 into main Apr 22, 2026
5 checks passed
@hazeone hazeone deleted the cursor/add-latex-math-rendering-c171 branch April 22, 2026 04:38
Copy link
Copy Markdown
Contributor

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d97d228ff4

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

return input;
}

const parts = input.split(/(```[\s\S]*?```|`[^`\n]*`)/g);
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.

P2 Badge Skip all Markdown code delimiters before LaTeX normalization

The delimiter normalizer only protects ... fences and single-backtick code spans, but CommonMark also allows ~~~...~~~ fences and multi-backtick inline spans (e.g. code). In those valid code blocks/spans, \(...\) and \[...\] are still rewritten into $...$/$$...$$, which mutates code samples and can render them as math instead of literal text; this is user-visible whenever assistant output uses non-triple-backtick code formatting.

Useful? React with 👍 / 👎.

DigitalNomad-Chat added a commit to DigitalNomad-Chat/ClawX that referenced this pull request Apr 26, 2026
… dedupe (ValueCell-ai#821 ValueCell-ai#845 ValueCell-ai#870 ValueCell-ai#873 ValueCell-ai#875 ValueCell-ai#878 ValueCell-ai#880 ValueCell-ai#885 ValueCell-ai#886 ValueCell-ai#887 ValueCell-ai#891 ValueCell-ai#903)

Overhaul execution graph card (collapse/expand, narration steps, web_fetch links),
separate thinking messages, render LaTeX math, dedupe optimistic messages,
hide recoverable gateway timeouts, add startup history recovery.
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.

[Feature]: 希望添加 LaTeX 数学公式渲染功能

2 participants