Skip to content

[Bug]: An unescaped % in a LaTeX formula leaves the whole equation as raw source #29493

Closed

Description

@ElliotDrel

Note

🤖 Claude Opus 5.5 responding on behalf of Elliot

Before submitting

  • I searched existing issues and did not find a duplicate.
  • I included enough detail to reproduce or investigate the problem.

Area

apps/web (the same message on apps/mobile is unverified)

Root cause

An unescaped % makes KaTeX reject the whole formula, and the chat then shows the entire equation as raw TeX.

Models write % in prose-like labels such as \text{3.18% dividend yield}. TeX treats % as a comment, even inside \text{}, so the rest of that line is discarded, including the closing }}. KaTeX then throws Unexpected end of input in a macro argument.

apps/web/src/components/chat/MathTypeset.tsx calls KaTeX with throwOnError: true, catches the error, and renders MarkdownMath's source fallback. That fallback was built for unfinished formulas during streaming, so a completed message stays raw permanently, with no sign that one character caused it.

Steps to reproduce

  1. Turn on Settings → Appearance → Render math.
  2. Render an assistant message containing:
\[
R_e=\underbrace{\frac{1.0176}{32}}_{\text{3.18% dividend yield}}
+\underbrace{6\%}_{\text{growth}}
=\boxed{9.18\%}
\]

Expected behavior

The equation typesets, with the % displayed as a literal percent sign.

Actual behavior

The whole equation displays as raw TeX. An earlier display equation in the same message, which writes 3.18\%, renders correctly.

KaTeX 0.16.47 with the options from MathTypeset.tsx:

Input Result
Equation above, as emitted ParseError: Unexpected end of input in a macro argument, expected '}'
Same equation with 3.18\% Renders

Impact

Minor bug or occasional failure

Version or commit

Code inspected at main @ 8657eec. Math rendering was added in #18735.

Environment

Windows 11, Codex gpt-6.1-sol

Suggested fix

Keep the first KaTeX parse unchanged. If it fails, retry once with every unescaped % escaped, and fall back to source only if the retry also fails. Valid TeX comments keep working because the retry only runs on formulas that already failed, and the copied source stays as authored.

The retry result should go through the existing typesetCache, so a failed formula is not parsed twice on every remount.

Mobile renders through native Enriched Markdown and RaTeX rather than this code path, so it needs its own check. This fix has not been implemented or tested in the app.

Workaround

Tell the agent to always write \% in math, including inside \text{}.

Activity

  1. meutex commented on Nov 2, 2026

    @meutex
    Contributor

    Hi! I'd like to work on this issue. I'll investigate the math rendering fallback in MathTypeset.tsx and work on a fix to handle unescaped % characters in LaTeX formulas. I'll share a PR once I have it ready.

  2. ElliotDrel commented on Nov 4, 2026

    @ElliotDrel
    ContributorAuthor

    Note

    🤖 gpt-6.1-sol responding on behalf of Elliot

    Supersedes the earlier request-changes assessment: the author merged our reviewed multiline/linear-scan/test-completion correction from meutex#2 into #29511 at ff2604b. I verified the merge ancestry and incorporated source; eight required checks pass on this head. One older addressed review thread still awaits repository-authorized resolution; the multiline thread is resolved. The upstream PR is open, so this issue should remain open until that implementation lands. Native mobile and a repeated real-client pass by this agent are not verified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions