Skip to content

fix(tui): render LaTeX math in messages as Unicode - #53418

Closed
moritzWa wants to merge 1 commit into
anomalyco:devfrom
moritzWa:fix/tui-latex-unicode
Closed

moritzWa wants to merge 1 commit into
anomalyco:devfrom
moritzWa:fix/tui-latex-unicode

Conversation

@moritzWa

@moritzWa moritzWa commented Oct 5, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #53648

#40508 tracks LaTeX rendering for the v2 TUI.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Models often write math as \( … \), \[ … \] or $$ … $$ in prose. The TUI passes message text straight to the opentui <markdown> component, which has no math support, so formulas show as raw LaTeX (e.g. \text{saved} = (\text{units …}) \times \text{rate} or \(0.5^5 \approx 3\%\)).

This adds renderMath (packages/tui/src/util/math.ts), a small LaTeX-to-Unicode pass applied to assistant text and reasoning before markdown rendering. The two examples above become saved = (units …) × rate and 0.5⁵ ≈ 3%. It handles \text{}, fractions, square roots, superscripts and subscripts (falling back to _(…) when Unicode has no subscript for a letter), Greek letters, common operators and arrows, \left/\right, \mathbb, and multi-line environments.

What it leaves alone:

  • Code spans and fenced code blocks are left untouched.
  • A single $ is not treated as math, so amounts like "$900M" stay as written.
  • Math that is still open while a reply streams stays raw until the closing delimiter arrives.
  • Unknown commands are kept as written.

#45339 added fenced ```latex rendering to v2. This covers prose math on the current TUI.

How did you verify your code works?

  • Unit tests in packages/tui/test/util/math.test.ts (7 tests), and bun run typecheck in packages/tui.
  • Ran renderMath over every assistant message containing math delimiters in my local session database (16 parts). 3 changed, all correctly, and nothing inside code was modified.

Screenshots / recordings

Rendered with the TUI's <markdown> component and default opencode theme.

Before: the delimiters and commands show raw.

before

After: the same text after renderMath. Inline code is unchanged.

after

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

moritzWa added a commit to moritzWa/opencode that referenced this pull request Oct 5, 2026
@thdxr

thdxr commented Oct 10, 2026

Copy link
Copy Markdown
Member

This branch has been upgraded to V2. We're no longer taking PRs for V1. If you think this is still relevant, please port it over and open it for V2.

— from 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊

@thdxr thdxr closed this Oct 10, 2026
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.

TUI shows LaTeX math in messages as raw source

2 participants