Skip to content

Draw a Bot's maths as formulas - #1724

Merged
davidmckayv merged 2 commits into
mainfrom
feat/render-maths
Nov 16, 2026
Merged

davidmckayv merged 2 commits into
mainfrom
feat/render-maths

Conversation

@davidmckayv

Copy link
Copy Markdown
Contributor

What changes

Maths a Bot writes is drawn as formulas, not LaTeX source. Asked for a loan payment, a Bot answered "calculated using (P r / [1-(1+r)^{-n}])" and a block of \frac{...} between bare brackets: markdown had eaten the backslashes of \( and \[ and drawn the rest as text.

  • One renderer for every place Bot text is drawn. A small wrapper, BotMarkdown in lib/markdown.tsx, is Streamdown plus its maths plugin (@streamdown/math: remark-math and KaTeX). It replaces Streamdown in replies, task threads, a task's result line in the conversation that asked for it, card text, interface markdown, group chats, tool results, run output and learned skills.
  • The model's delimiters, in the ones the plugin reads. lib/maths.ts rewrites \( \) to inline $$ $$, and \[ \] on lines of its own to a $$ block at the indent it was written at. Fenced code, inline code and $$ already written are left exactly as they are. A single $ is never maths (Streamdown's default), so "$25,000" and "$5 to $10" stay prices. A $ inside a formula is escaped for KaTeX, and a thousands comma (25,000) is written 25{,}000 so it does not draw as "25, 000". An opener with no closer in its paragraph stays as written, so while a reply streams a formula draws once it is closed.
  • Loaded with the first formula. The plugin, KaTeX and KaTeX's stylesheet are a separate chunk fetched the first time a message has maths in it, as the code plugin already is. KaTeX's fonts are served from the app's own origin (the desktop's local one included), never a CDN.
  • Matches the chat. Formulas take the colour of the text round them in light and dark, at 1.1em rather than KaTeX's 1.21em so a fraction does not loosen the line. A formula set apart is a block of its own that scrolls sideways when it is wider than the column; the page never does.

Versions

@streamdown/math 1.0.3 (Streamdown's own maths plugin, released 2026-09-30, about 3.6 million downloads a week) on the installed streamdown 2.6.0. It brings katex ^0.16.27 through rehype-katex 7.0.1, which allows only the 0.16 line, so katex is pinned at 0.16.47 (2026-05-16, about 44 million downloads a week), the version already in the lockfile, for its stylesheet.

Bundle

Production build, before and after:

before after
Entry chunk (every page) 1,538,563 B raw, 478,903 B gzip 1,541,457 B raw, 479,941 B gzip (+1.0 KB gzip)
Entry stylesheet 55,534 B gzip 55,593 B gzip
Maths chunk (first formula only) none 273,410 B raw, 81,389 B gzip, plus KaTeX's stylesheet 7,960 B gzip
All JavaScript in the build 31,064,472 B 31,066,739 B (+2.3 KB)

KaTeX's code, stylesheet and fonts were already in the build behind diagrams; the maths chunk now shares them.

Tests

  • app/tests/maths-rendering.test.tsx (real browser, added to the CI browser job): one answer with inline \( \), a wide \[ \] block, inline $$, prices and a code block holding $ and \(, drawn as a reply, a task's thread, the task's result line and a card's text, in light and dark at 1440 and 390. Every formula is drawn by KaTeX in KaTeX's font, the set-apart one as a block that scrolls sideways at 390, no delimiter or \frac is left on screen, the prices and the code are exactly as written, and the page never scrolls sideways. On main: 16 of 16 fail. On this branch: 32 of 32 pass in Chromium and WebKit.
  • app/tests/maths-delimiters.test.ts: the rewrite on the answers models write, including the loan answer above, prices, code, escaped backslashes and a reply mid-stream.

Seen live

A full stack with a local Intelligence, the General Assistant on a real model asked for the monthly payment on a $25,000 loan, once handed to a task and once directly. The same conversation in the app on main and in this branch, in Chromium and WebKit, light and dark, at 1440 and 390 (64 screenshots, all looked at):

  • Main: every formula as source between bare brackets, \frac{...}, \boxed{...} and all, in the reply, the task's thread and the result line.
  • This branch: 11 formulas in the conversation and 4 in the thread drawn by KaTeX in every shot, the model's \boxed{} total as a box, the wide ones scrolling inside their own blocks at 390, no page overflow, no font or stylesheet request failing. While the direct answer streamed, it drew prose first and each formula once it closed.

A Bot asked for a loan payment answered with LaTeX, and the chat drew it as source with the backslashes of \( and \[ eaten by markdown. Bot text now goes through Streamdown's maths plugin (remark-math and KaTeX) on every surface that draws it: replies, task threads, a task's result line in the conversation that asked, card text, interface markdown, group chats, tool results, run output and learned skills. The model's \( \) and \[ \] are rewritten to the $$ the plugin reads; code, existing $$ maths and single-dollar prices are left as written. The plugin and KaTeX's stylesheet and fonts load with the first formula, as the code plugin does. A set-apart formula scrolls sideways in its own block.
@davidmckayv
davidmckayv merged commit b825ff5 into main Nov 16, 2026
30 of 33 checks passed
@davidmckayv
davidmckayv deleted the feat/render-maths branch November 16, 2026 19:28
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