MathJaxSwift icon indicating copy to clipboard operation
MathJaxSwift copied to clipboard

[Bug] v3.5.0 TeX parser regression: "Extra open brace" / "Missing \left" on valid formulas

Open patr1ckzhu opened this issue 7 months ago • 1 comments

Summary

MathJaxSwift v3.5.0 introduces a regression in the TeX input processor. Valid LaTeX formulas like \sum_{i=1}^n, \lim_{x \to 0}, \left(...\right), and even simple cases like a^{10} fail with "Extra open brace or missing close brace" or "Missing \left or extra \right" errors. The same formulas render correctly in v3.4.0.

Likely related to the "Fix fonts." / "More font changes." commits on 2026-04-02, or the switch to toDictionary() option passing in PR #43.

Related downstream report: colinc86/LaTeXSwiftUI#78

Environment

  • MathJaxSwift: 3.5.0 (broken) / 3.4.0 (works)
  • LaTeXSwiftUI: both 1.5.0 and 2.0.0 affected (they transitively depend on MathJaxSwift)
  • iOS 26 / Xcode 26
  • Also reproduces when pinning MathJaxSwift to 3.5.0 with LaTeXSwiftUI 1.5.0 via an exact-version SPM override

Failing Cases

With .errorMode(.error), MathJax returns these errors:

Input Error
$a^{10}$ Extra open brace or missing close brace
$\sum_{i=1}^n x_i$ Extra open brace or missing close brace
$\sum_{n=1}^{\infty} \frac{1}{n^2}$ Extra open brace or missing close brace
$\lim_{x \to 0} \frac{\sin x}{x}$ Extra open brace or missing close brace
$\left( \sum_{i=1}^n a_i \right)^2$ Missing \left or extra \right

All are valid LaTeX — they render correctly on mathjax.org and on MathJaxSwift 3.4.0.

Cases That Still Work (on 3.5.0)

  • $E = mc^2$, $x^2$, $x_i^2$, $\alpha, \beta, \gamma$
  • $\frac{1}{2}$, $\sqrt{x^2+1}$
  • $\int_0^{\infty}$, $\int_{-\infty}^{\infty}$
  • $\lim_{n \to \infty}$ (interestingly, \to \infty works but \to 0 fails!)
  • \begin{bmatrix}, \begin{aligned}, \begin{cases} environments

Reproduction via SPM

Any iOS project using LaTeXSwiftUI 1.5.0 transitively pulls MathJaxSwift (via from: "3.4.0" which resolves to the latest 3.5.0). The failures are immediate. Pinning MathJaxSwift to exact 3.4.0 via a direct SPM dependency completely resolves the issue:

// In project's Package.swift or equivalent SPM config:
.package(url: "https://github.com/colinc86/MathJaxSwift", exact: "3.4.0"),
.package(url: "https://github.com/swhitty/SwiftDraw", exact: "0.20.1"),

Bisect Hint

The regression was introduced between v3.4.0 (2023-07-19) and v3.5.0 (2026-04-03). The suspect commits are:

  • Fix fonts. (2026-04-02)
  • More font changes. (2026-04-02)
  • The switch to toDictionary() / plain-JS-object option passing (PR #43 / fixes #38)

The error type ("Extra open brace") suggests something is being corrupted between the Swift TeXInputProcessorOptions layer and the mathjax-full TeX input processor — possibly in how inlineMath/displayMath delimiters or loadPackages are serialized.

Workaround

Pinning to v3.4.0 fully resolves the issue. No other changes needed.

Thanks

Love the library and I really appreciate the maintenance work on this. Let me know if I can help debug further (happy to test a patch build).

patr1ckzhu avatar Apr 11 '26 00:04 patr1ckzhu

I hit this too and tracked it down — the root cause isn't the options serialization; it's the default digits regex in TeXInputProcessorOptions.

In Sources/MathJaxSwift/Options/Input/TeXInputProcessorOptions.swift:

public static let defaultDigits: String = "^(?:[0-9]+(?:{,}[0-9]{3})*(?:.[0-9]*)?|.[0-9]+)"

MathJax's actual default for this option is:

^(?:[0-9]+(?:\{,\}[0-9]{3})*(?:\.[0-9]*)?|\.[0-9]+)

The backslashes were lost (\{,\} became {,} and \. became .). The unescaped . matches any character, so whenever the TeX tokenizer reads a number, (?:.[0-9]*)? greedily consumes the character that follows the digits.

That's exactly why a^{10} fails: 10} is consumed as a single "number", the closing brace disappears from the token stream, and the parser reports Extra open brace or missing close brace — or Missing \left or extra \right when the eaten brace closed a \left…\right pair.

It also explains the "selective corruption" you observed: only formulas where a digit is immediately followed by a structural character break.

  • \lim_{n \to \infty} — no digit before } → works
  • \sum_{i=1}^n — contains 1} → fails
  • x^{2} — contains 2} → fails

Minimal repro:

let mathjax = try MathJax(preferredOutputFormat: .svg)
_ = try mathjax.tex2svg("x^{2}")   // throws: Extra open brace or missing close brace
_ = try mathjax.tex2svg("x^{2}}")  // "succeeds" — the extra brace compensates for the eaten one

The fix is to restore the escapes:

public static let defaultDigits: String = #"^(?:[0-9]+(?:\{,\}[0-9]{3})*(?:\.[0-9]*)?|\.[0-9]+)"#

Workarounds until then: pass a corrected digits string in your own TeXInputProcessorOptions, or — when you can't reach the options (e.g. going through LaTeXSwiftUI) — insert a space after each digit run; the broken regex eats the space instead of the structural character, and spaces are inert in math mode.

malgapo-bot avatar Jul 06 '26 06:07 malgapo-bot