Skip to content

Typeset LaTeX formulas written in <code class="language-math"> - #6165

Merged
max-sixty merged 9 commits into
mainfrom
math-rendering
Nov 16, 2026
Merged

max-sixty merged 9 commits into
mainfrom
math-rendering

Conversation

@max-sixty

@max-sixty max-sixty commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Leaf had no way to show math. A page now writes a formula in LaTeX in <code class="language-math">, the class Markdown renderers already give a ```math fence: inline among words, or alone in a <pre> for a display formula. The runtime typesets it with Temml (vendored, loaded only on a page that has a formula) into MathML, which Chromium, Firefox and WebKit draw natively, in the surrounding text's size and colour in either scheme. A formula Temml can't parse keeps showing its source, and the error reaches the page's log, which page check --render reports.

What the diff doesn't show:

  • Comments. The <math> sits in a shadow root on a span holding the LaTeX source. The page's reading reads an undeclared shadow host by its light children, so the browser and the file both read a formula as its LaTeX, with no fence: a quote can run across a formula, and it names the formula by the source its author can find. Two shared primitives change for this. pageRange now always reads getComposedRanges, so a drag that ends inside a formula takes all of it, a click stays a caret, and Firefox no longer drops the words before a formula a drag crosses. rangeOf puts a formula's words at its host's edges, so travel, pins and the margin find where it is drawn.
  • Known gap. A comment's violet wash doesn't paint over a formula's glyphs, as over an inline image in a quote; the pin and travel do reach it.
  • One dressing pass. Code colouring and typesetting run together as dressCode, which replaces highlightBlocks in widget-api.js. The page presents with formulas in place; until Temml arrives (about 180 ms on a local server) the LaTeX shows as code, as widgets show their source before upgrading. lf-draft now dresses the body it repaints, so a draft keeps its colours and formulas after a save. A display formula keeps a code block's Copy, which copies its LaTeX.

Max asked whether Leaf could show LaTeX, from a page whose formulas were written as plain text. A fresh code review's findings are folded in, and a fresh visual pass checked the renders in three engines. The authoring sentences (page-authoring.md, "Markup"; threads.md) describe a new capability and weren't scored by eval.

Tests: tests/browser/test_render_formulas.py (drawn, quoted across, clicked, wrapped, travelled to, refused), the language-advice model test, pnpm run test:runtime, pnpm run check:browser, and focused selection, mark, copy and draft browser tests.

This was written by Claude Code on behalf of max-sixty

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

widget-api.js stops exporting highlightBlocks, but four consumers in this tree still import or call it, and the PR's head leaves them untouched:

  • examples/pr-walkthrough.page/widgets/lf-pr-brief.js and examples/dynamic-widgets.page/widgets/lf-widget-builder.js import highlightBlocks from the widget API. A missing named export is a link error, so neither widget module loads, and both published examples lose those widgets.
  • tests/browser/test_render_anchors.py::test_code_colouring_keeps_each_blocks_words_and_annotations calls it through __lfWidgetApi(). I ran it on this head and it fails with TypeError: highlightBlocks is not a function.

Root AGENTS.md, "Stage", asks for a contract and every consumer of it to change together. Switching those four call sites to dressCode fixes them. I can push that commit if you'd like.

The new formula tests (tests/browser/test_render_formulas.py) and the language-advice model test pass here.

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The four highlightBlocks consumers named in the earlier review are still unchanged at this head: the two example widgets and test_render_anchors.py.

Comment thread browser/runtime/code-dressing.js Resolved
@max-sixty
max-sixty merged commit 8e1a342 into main Nov 16, 2026
7 checks passed
@max-sixty
max-sixty deleted the math-rendering branch November 16, 2026 18:10
max-sixty added a commit that referenced this pull request Nov 16, 2026
…arkup (#6208)

An agent's thread message is now one Markdown body, and HTML anywhere in
it draws where it is written, as in a GitHub comment: a `<code
class="language-math">` formula mid-sentence, a table between two
paragraphs, an `lf-options` choice. `--markup` is gone from `leaf thread
create` and `leaf thread reply`, along with the event field it filled
and its consumers. Copy as Markdown copies the message's text, HTML
included.

Max asked for this after #6165 left an inline formula needing
`--markup`.

What to weigh:

- **Who reads HTML.** Only text the agent writes with a `leaf thread`
command draws its HTML. A Codex turn's final text, progress lines,
failure receipts and the clerk's messages keep tags as words, since they
are written for a terminal or streamed, and a stray `Option<String>`
would otherwise vanish or refuse the answer. So the log marks a message
whose text holds HTML with `html: true`, set only where keeping HTML
changes what it draws. What the user types stays text.
- **One body, two parsers.** Python renders the message (markdown-it-py)
to check and read it, and the browser renders the same text
(markdown-it) into the authored island. `tests/message_html_cases.json`
holds both to the same elements and words. Two dialect rules make
agents' HTML land where they mean it, both in each runtime: a custom
element opening a line runs to its closing tag, blank lines included, as
`<pre>` does (otherwise a blank line in an `lf-code` body ended the
block), and a `<code>` element's content is literal, so LaTeX keeps its
backslashes and asterisks. A block widget written after words on its
line is refused with advice to start it on its own line. Since the log
keeps only the text, a later change to that reading bumps the protocol
(`layer-registry.md`, "Page protocol"); logging the rendered markup
beside the text instead would bring back a second copy of each message
and draw these messages through a renderer no other message uses.
- **A message holding HTML is frozen whole.** `leaf thread edit` refuses
it, where before only its markup was frozen; the agent posts a
correction instead.
- **Hosted pages.** The browser now addresses the media an HTML message
names, as it already did for Markdown images; the server no longer
rewrites message text, which had altered code examples.
- **Protocol 46**, and deliveries at format 46, since the event and
delivery shapes changed.

`threads.md` and the delivery clauses now teach the one-body form.
Scored on Claude Code with the changed text:
`thread-choice-takes-a-press`, `answer-quotes-what-it-cites` and
`answer-hands-back-the-next-move/turn-writer` passed all 16 runs, and
the choices and `<details>` quotes the agents wrote post through `leaf
thread`. Reviewed by Codex, whose six findings are fixed here, and by
the PR's auto-reviewer, whose design point the protocol note above
answers.

Ran `uv run pytest tests/model tests/process`, `pnpm run test:runtime`,
`pnpm run check`, and the browser and journey tests whose fixtures
changed most (11 ids).

> _This was written by Claude Code on behalf of max-sixty_
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.

2 participants