Repository navigation
Add LaTeX math, drawn by a renderer that the app provides - #25
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
LaTeX in a reply showed as raw text, and Markdown inside it, such as
*or_, changed its style.\( … \)is inline math.\[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, soSee \[1\]still showsSee [1]. A single$stays text, so prices do not change.\\)in a matrix does not close the formula.TranscriptMathspan, as images are.TranscriptTextView.mathRendererandTranscriptText.mathRenderer(_:)take an async renderer. It returns aTranscriptMathImage: 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.Closes #9.
Differences from the issue, and behavior changes
\(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
$$that stay text. They cover escaped brackets in a line, and display math as a block that keeps lines such as- band# 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.🤖 Generated with Claude Code