Skip to content

feat(tui): render LaTeX as Unicode math - #45339

Merged
kitlangton merged 9 commits into
v2from
latex-plugin
Aug 26, 2026
Merged

feat(tui): render LaTeX as Unicode math#45339
kitlangton merged 9 commits into
v2from
latex-plugin

Conversation

@kitlangton

@kitlangton kitlangton commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Why

LaTeX fences currently appear as source code in the terminal, making fractions, matrices, and equations difficult to read. Streaming math also needs to tolerate unfinished commands without replacing a useful preview with broken source on every chunk.

What Changes

Add a built-in Unicode-only math renderer through the same fenced-code API as Mermaid. The parser and layout engine are in-repo source, with no external math package, image generation, or special graphics protocol.

Input or state Behavior
Valid latex, math, or tex fence Render styled Unicode math
Incomplete append-only streaming update Keep the last successful layout
Closed or finished invalid formula Show the original source rather than a stale equation
Wide formula Scroll horizontally instead of wrapping mathematical structure

Colors follow the theme: subdue structural strokes and emphasize relations. Radicals use connected box-drawing stems and hooks, including nested and indexed roots. Scripts sit outside tall expressions; over/underbraces stretch and center their annotations. Matrix variants preserve their delimiters, cases are left-aligned, and simple array l/c/r formats retain their vertical rules. Norm bars, empty alignment cells, \displaylines, and \cfrac alignment options preserve their notation instead of silently changing the formula.

Preview state belongs to each Markdown fence and is released when that fence is removed. The session applies final Markdown content before disabling streaming, avoiding stale parsed tokens when both values change in one batch. OpenTUI 0.5.8 is unmodified; no dependency patch is required.

Package Boundary

LaTeX follows the structure of Mermaid's packages/merman:

Owner Responsibility
packages/latex/src Parser, symbols, input limits, layout, and tests
@opencode-ai/latex/markdown Fenced-code renderer and streaming preview lifecycle
@opencode-ai/latex/plugin Built-in plugin registration
packages/tui Imports the built-in plugin

The new private workspace package has no runtime dependency on the TUI. Its tests run in the workspace's Turbo test pipeline. There is no playground story in the PR.

Demo

latex-feature-corrected.mp4

Matched production TUI recordings at 100x36, with the same simulated response and configured pacing. Before: fedf017e25; after: 9dec8f03fd. Shows a quadratic formula, matrix, and streaming sum. Sequential clips keep the text readable; playback is not accelerated.

Notation And Geometry

latex-corrections.mp4

Three matched pairs compare the pre-review renderer (6a6d80e4da) with 9dec8f03fd: connected radicals and tall powers; norms and equation rows; braces, column alignment, rules, and continued fractions. Real production TUI at 110x42 with a simulated provider. Each pair shows five seconds before, then five seconds after, without acceleration. Temporary capture scripts are not part of the PR.

Scope

Fenced Unicode math only. Inline $...$/$$...$$, full TeX documents, TikZ, and image rendering are out of scope. Unsupported array formats and command options fall back to source rather than being guessed. The package uses the existing plugin API, OpenTUI, and shared string-width dependency; there are no new external dependencies or CLI build-script changes.

Matched Darwin ARM64 builds with Bun 1.3.14, minified without sourcemaps and with the web UI omitted from both:

Base This PR Increase
Executable 111,297,890 bytes 111,330,914 bytes 32.25 KiB
Gzip-9 executable 37,200,551 bytes 37,210,806 bytes 10.01 KiB

Verification

cd packages/latex
bun run test
bun typecheck
bunx turbo test --filter=@opencode-ai/latex

cd ../tui
bun run test test/markdown-streaming.test.tsx test/plugin-markdown.test.ts
bun run test
bun typecheck

cd ../cli
OPENCODE_CHANNEL=next OPENCODE_VERSION=0.0.0-size-check bun run script/build.ts --single --skip-install --skip-web-ui --outdir=/private/var/folders/dd/5fz89drs5p9_r0fk7rwqqnbr0000gn/T/opencode/latex-size-corrected
  • All 123 LaTeX package tests and 4 focused TUI tests pass, including the real Markdown adapter and batched final-text update order.
  • Full local TUI suite: 804 passed, 5 skipped, 2 failed. Both selection failures reproduce on the clean base.
  • Scoped lint, formatting, the host Bun CLI build, and all 33 pre-push workspace typecheck tasks pass.
  • A fresh three-pass simplification review covered reuse, quality, and efficiency; the resulting empty-script and even-width brace regressions are covered by tests.
  • The recorded fixtures exercise the actual production session view with a simulated provider.

@kitlangton kitlangton changed the title feat(tui): render LaTeX math in the terminal feat(tui): render LaTeX as Unicode math Aug 26, 2026
Keep the Unicode parser, symbols, layout, and input limits in the TUI source tree. Remove the math package and its graphics dependencies, and retain parser and layout regression coverage.
Move the engine, Markdown adapter, plugin, and tests into a private workspace package. Remove the standalone license file and dependency patch; apply final Markdown content before completion at the TUI call site.
Preserve norm delimiters, command boundaries, empty cells, array column formats, and continued-fraction options. Size indexed radicals safely, join their strokes, and place scripts and brace annotations around the full expression.
@kitlangton
kitlangton merged commit 6c39235 into v2 Aug 26, 2026
14 of 15 checks passed
@kitlangton
kitlangton deleted the latex-plugin branch August 26, 2026 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant