Skip to content

Formula postprocess leaves nested math delimiters when equation numbering follows $...$ #24

Open

Description

@lky8686

Summary

A Formula block returned by the model may contain inline math delimiters followed by an equation number, for example:

$|\Delta T_{0}|_{\max} = |\overline{T_{0j}} - \overline{T_0}|_{\max}$ ……(26)

postprocess_formula() does not remove the $...$ wrapper in this case because its wrapper patterns only match when the closing delimiter is at the end of the entire string.

The Markdown assembly then adds another $$ wrapper, producing nested math delimiters. As a result, the formula is displayed as raw LaTeX instead of being rendered by SoMarkDown/KaTeX in the Demo.

Actual Markdown output

$$
$|\Delta T_{0}|_{\max} = |\overline{T_{0j}} - \overline{T_0}|_{\max}$ ……(26)
$$

Expected behavior

Formula blocks should contain bare LaTeX regardless of trailing equation numbering, and Markdown assembly should own the display-math delimiters:

$$
|\Delta T_{0}|_{\max} = |\overline{T_{0j}} - \overline{T_0}|_{\max} ……(26)
$$

The equation body and numbering should be preserved.

Relevant code

At commit c1a1dcb312ee9f691404f127785510453c0d9350:

  • Formula wrapper cleanup:
    # Formula blocks store bare KaTeX/LaTeX; assembly owns display-math delimiters.
    wrappers = [
    r"^\$\$(.*?)\$\$$",
    r"^\$(.*?)\$$",
    r"^\\\[(.*?)\\\]$",
    r"^\\\((.*?)\\\)$",
    ]
    for pattern in wrappers:
    match = re.match(pattern, text, re.DOTALL)
    if match:
    text = match.group(1).strip()
    break
    text, text_before, text_after = _extract_formula_numbering(text)
    text = space_latex_row_breaks(text)
  • Display-math assembly:
    return f"$$\n{clean_formula_markdown_delimiters(content)}\n$$\n"

Environment

  • Local official OXR parser
  • OXR-1.0 BF16 through MLX on Apple Silicon
  • Demo Preview using SoMarkDown/KaTeX

This appears to be a postprocessing issue rather than a recognition failure: the formula body is present in the JSON response, but the extra delimiters prevent rendering.

Suggested regression cases

  1. $formula$ ……(26)
  2. $$formula$$
  3. Bare LaTeX without wrappers
  4. \tag{26} and other supported equation-number formats
  5. Dollar signs inside formula content, to ensure they are not removed accidentally

Activity

  1. BinyangQiu commented on Oct 29, 2026

    @BinyangQiu
    Contributor

    Nice find. Numbering is extracted after wrapper stripping, so the end-anchored regex misses it. Fixing it now, and we’ll use your test cases. PRs welcome if you beat us to it.

  2. jczeal commented on Dec 3, 2026

    @jczeal

    I reproduced this on main at b2ee4ac through POST /v1/parse/sync, using a generated PNG and local layout and recognition HTTP stubs. With $x+y=3$ ……(26) as the recognition response, JSON reports a Formula block with format: latex and retains the full string. Here is the Markdown returned by the route.

    $$
    $x+y=3$ ……(26)
    $$
    

    The bundled SoMarkDown/KaTeX renderer returned one math error for that Markdown in Chromium. After OXR postprocessing, the controls for bare LaTeX, $...$, $$...$$, \tag{26}, and an escaped dollar all rendered successfully. The generated case covers the parser and renderer deterministically and can serve as a regression test for the route.

    The reproduction package contains the API harness, renderer checks and observed outputs.

    Reproduction package and SHA-256

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