-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Description
KaTeX Inline Math Fails When Followed By Punctuation
Summary
Inline LaTeX math delimited with $...$ fails to parse/render when a punctuation character immediately follows the closing $.
Example: '$\alpha$)' fails, while '$\alpha$ )' works.
This appears to be a tokenizer boundary issue in the web UI markdown pipeline (Marked + KaTeX extension), where the inline math rule requires whitespace/end-of-input after the closing delimiter, so punctuation causes the match to fail or be tokenized incorrectly.
Affected Area
- Web UI markdown rendering (KaTeX)
- File:
packages/ui/src/context/marked.tsx
Repro Steps
- Start the backend and web UI in dev mode.
- Backend:
opencode serve --port 4096 - UI:
bun run --cwd packages/app dev -- --port 4444
- Backend:
- In any place where markdown is rendered (chat message, etc.), enter one of these strings.
Expected
Inline math should render correctly even when punctuation immediately follows the closing $.
For example:
- Input:
$\alpha$) - Output: the Greek letter alpha, then a closing parenthesis.
Actual
Rendering fails and KaTeX does not display the expression. In practice this shows up as a KaTeX parsing error / math not rendered.
The same expression works if an extra space is inserted:
- Works:
$\alpha$ ) - Fails:
$\alpha$)
Root Cause (Hypothesis)
The inline math tokenizer used by the markdown pipeline does not treat punctuation as a valid boundary after the closing delimiter, so $...$) does not match as a complete inline math token.
This is consistent with regexes/lookaheads that only allow whitespace or end-of-input after the closing $.
Proposed Fix Direction
Allow common punctuation characters immediately after the closing delimiter when tokenizing inline math, e.g. ) ] } . , : ; ! ? (and optionally common non-ASCII punctuation).
My Local Patch / Proof-of-Fix
I made a local patch that replaces marked-katex-extension with a small custom Marked extension that:
- Accepts punctuation after the closing
$via a lookahead - Avoids false positives by scanning for
$only at reasonable boundaries
Commit: 575de74b ("Patched parser of katex UI") from my forked opencode repository.
Note: this patch is likely more invasive than desired for upstream (it changes the extension implementation), so I’m opening this as an issue first for maintainers to decide the preferred fix (tweak existing extension, add a more minimal boundary rule, or upstream a fix).
Workaround
Insert a space after the closing delimiter:
$\alpha$ )
This is error-prone in normal writing and not expected by users.
Environment
- OpenCode: local build of
sst/opencode(branch:dev) - UI:
packages/app - OS: macOS
- Browser: Firefox