Skip to content

Add LaTeX math, drawn by a renderer that the app provides - #25

Merged
servercimen merged 2 commits into
feature/code-highlightingfrom
feature/latex-math
Nov 20, 2026
Merged

servercimen merged 2 commits into
feature/code-highlightingfrom
feature/latex-math

Conversation

@servercimen

@servercimen servercimen commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Summary

LaTeX in a reply showed as raw text, and Markdown inside it, such as * or _, changed its style.

  • \( … \) is inline math.
  • A line that starts with \[ or $$ opens display math in the block parser, the way a fence opens code. Its lines stay as written up to \] or $$, even a line that starts with - or #, and it becomes a block of its own. Text after the closer starts a paragraph. If a blank line comes before the closer, it was not a formula, and its lines show as text.
  • $$ … $$ inside a line is display math too. A \[ inside a line stays a Markdown escape, so See \[1\] still shows See [1]. A single $ stays text, so prices do not change.
  • The parser does not read Markdown inside a formula. A backslash escapes the next character, so \\) in a matrix does not close the formula.
  • Each formula is one U+FFFC character with a TranscriptMath span, as images are.
  • TranscriptTextView.mathRenderer and TranscriptText.mathRenderer(_:) take an async renderer. It returns a TranscriptMathImage: an image, its size in points, and its baseline. TranscriptKit calls it off the main thread, once for each formula and font size. It lays the block out again when the image arrives.
  • TranscriptKit uses only the alpha of the image and draws the formula in the text color, so it follows light and dark appearance. A formula that is wider than the text column is scaled down. A paragraph that holds only display math is centered.
  • Without a renderer, or until the image arrives, a formula shows its LaTeX in the code font.
  • Copy gives the LaTeX with its delimiters. VoiceOver reads the LaTeX.
  • The package keeps no dependency: the app supplies the renderer. The README has a Math section.

Closes #9.

Differences from the issue, and behavior changes

  • The issue asks to show the source while the closer has not arrived. This PR shows nothing until the formula closes. Source that then turns into one image would make the text shrink. That update is not an append, so TranscriptKit would redraw every tile and stop the tag animations. An inline opener with no closer within 512 characters shows as plain text, as it does when the text finishes. A line that starts display math is held back until the line is complete.
  • Before this PR, \( was an escaped (. Now \( … \) with a closer is a formula, also when the view has no renderer. $$ … $$ and a line that starts with \[ are formulas too. \[ inside a line is not changed.

Stack

This PR is part of a stack. It is based on #24 (feature/code-highlighting), so its diff shows only this change. Merge the stack in order, starting with #17.

Tests

  • 17 new unit tests. They cover inline and display math, LaTeX kept whole, and prices and an unclosed $$ that stay text. They cover escaped brackets in a line, and display math as a block that keeps lines such as - b and # c, also in a list item and a quote. They cover text after the closer, and a blank line before the closer. Streaming checks that the text only grows at every character and matches a parse at once, also for display math blocks. Other tests cover a stray opener after 512 characters, an empty formula, a checkpointed parse, every split point, copy, the attachment's size and ascent after the renderer, and display math that is centered and drawn in the text color from an RGBA image. swift test: 162 tests pass.
  • The package builds for iOS.
  • On an iPhone 12 mini, a local demo app draws inline math, display math, a matrix and a formula in a heading with iosMath through this hook. They show in the text color in light and dark mode. Without a renderer, each formula shows its LaTeX. While a formula streams in, nothing of it shows, and the finished stream matches the static text. The demo app is not in this PR.

🤖 Generated with Claude Code

servercimen and others added 2 commits November 20, 2026 08:51
The parser reads `\( … \)` as inline math, and `\[ … \]` and `$$ … $$`
as display math. A single `$` stays text, so prices do not change.
The parser does not read Markdown inside a formula, and a backslash
escapes the next character, so `\\)` in a matrix does not close it.

Each formula is one U+FFFC character with a `TranscriptMath` span, as
images are. While a formula streams in, it shows nothing until it
closes, so the text on screen only grows. An opener with no closer
within 512 characters shows as plain text, as it does when the text
finishes.

`TranscriptTextView.mathRenderer` and `TranscriptText.mathRenderer(_:)`
take a renderer that returns an image, its size and its baseline.
TranscriptKit calls it off the main thread, once for each formula and
font size, and lays the block out again when the image arrives. It uses
only the alpha of the image and draws the formula in the text color.
Until then, or without a renderer, a formula shows its LaTeX in the code
font. A paragraph that holds only display math is centered. Copy gives
the LaTeX with its delimiters, and VoiceOver reads the LaTeX.

Closes #9

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`\[` is also a Markdown escape, so "See \[1\]" showed a formula where it
showed "[1]" before. And a line inside display math that started with
"-", "+" or "#", or a blank line, split the paragraph before the inline
parser saw the closer.

A line that starts with `\[` or `$$` now opens display math in the block
parser, the way a fence opens code. Its lines are kept as written up to
the closer, and it becomes a block of its own. Text after the closer
starts a paragraph. A blank line before the closer means it was not a
formula, and its lines show as text. Inside a line, `\[` is an escape
again, and `\( … \)` and `$$ … $$` stay inline math. A line that starts
display math is held back until it is complete, so the text only grows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Render LaTeX math

1 participant