Skip to content

fix(pdf): keep positioned math out of tables - #87

Merged
asionesjia merged 3 commits into
mainfrom
fix/pdf-math-layout
Aug 27, 2026
Merged

asionesjia merged 3 commits into
mainfrom
fix/pdf-math-layout

Conversation

@asionesjia

@asionesjia asionesjia commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Confirmed #77: glyph runs that pdftotext -layout already has were turned into ragged GFM tables, and equation numbers / math lead-ins were promoted to headings. Formulas that are images or paths (empty in pdftotext) stay out of scope.

Changes

  • Reject borderless grids that look like stacked math (operators, ∂/∑, equation numbers)
  • Merge smaller, vertically offset script runs (hᵖ⁺¹, H⁺) without treating the next paragraph as subscripts
  • Join d/dt and ∂L/∂q stacks into d/dt style fractions
  • Do not heading-ize equation numbers, math lines, lowercase fragments, or colon lead-ins

Checked against the issue examples: Lagrange / Richardson formulas stay as text, (3.18) is not a heading, or inversely is not a heading, and real tables (year headers, P-value grids) still convert.

fixes #77


Note

Medium Risk
Heuristic changes affect table detection and heading/script merging across all PDFs; misclassification could hide real tables or alter typography, though tests target the reported regressions.

Overview
Improves PDF→markdown for scientific text so aligned math glyphs stay as prose instead of GFM tables, and display lines are less often turned into headings.

Text merging: Adds mergeStackedMath in the finish pipeline (after script merge) to join vertically stacked pairs like d/dt into d/dt. Superscript/subscript attachment now covers letters and operators (hᵖ⁺¹, H₂), with tighter font-size and vertical-offset rules so baseline hyphens and unrelated lines are not scripted.

Table detection: Borderless table acceptance rejects grids that looksLikeMathLayout (equation numbers, math operators/glyphs, short operator-heavy cells) while still allowing compact numeric tables with ±/−.

Headings: Skips isolated-line and header promotion for equation numbers, math-heavy lines, lowercase lead-ins, colon endings, and similar false positives.

Adds math.test.ts, table/math regression tests, and a small snapshot tweak for endnote superscript attachment.

Reviewed by Cursor Bugbot for commit d468c44. Bugbot is set up for automated code reviews on this repo. Configure here.

Stacked fractions and superscripts in the text layer were detected
as borderless tables, and equation numbers became headings.

fixes #77
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
mdgate-demo d468c44 Aug 27 2026, 04:57 AM

Comment thread packages/pdf/src/pdf.ts
Comment thread packages/pdf/src/tables.ts

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale comment

Left a non-blocking comment. Cursor Bugbot finished as skipped and reported 2 unresolved issues, so this was not approved. Human review is needed; no reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Router and Approver

toScript now normalizes Unicode superscript digits before applying
the vertical-offset script map. The math-layout heuristic no longer
treats ± or typographic minus as operators, so compact numeric grids
stay tables.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Minus-only equations become tables
    • The eqNums path now still treats − and ± as operators, so numbered minus-only formulas are rejected as tables while signed numeric grids stay tables.

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 91d20f3. Configure here.

Comment thread packages/pdf/src/tables.ts

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale comment

Left a non-blocking comment. Cursor Bugbot finished as skipped and left 1 unresolved issue on the current head, so this was not approved. Human review is needed; no reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Router and Approver

Numbered display equations whose only operator is − or ± still
match the eqNums path. Signed numeric tables stay tables.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. Cursor Bugbot passed on the current head and all prior findings are resolved, so no reviewers were assigned.

Open in Web View Automation 

Sent by Cursor Approval Agent: Pull Request Router and Approver

@asionesjia
asionesjia merged commit 247d8d2 into main Aug 27, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PDF: math glyphs are missing or shredded into tables

2 participants