Skip to content

fix(docx): preserve inline equations in headings - #5076

Open
its-amann wants to merge 2 commits into
docling-project:mainfrom
its-amann:fix/docx-heading-inline-equation-5034
Open

its-amann wants to merge 2 commits into
docling-project:mainfrom
its-amann:fix/docx-heading-inline-equation-5034

Conversation

@its-amann

@its-amann its-amann commented Oct 28, 2026 •

Copy link
Copy Markdown

Word headings containing inline equations keep internal <eq> markers in their text. This builds the empty SectionHeaderItem and leading InlineGroup requested by @ceberam in #5034. The group contains the heading text and formula items. Numbering, heading levels and the parent of following body content are preserved.

The empty heading.text is intentional in this rich content representation. JSON retains the content in the inline children. Consumers must read those children or use a serializer instead of treating the heading's text field as the complete title.

Issue resolved by this Pull Request:
Resolves #5034

Checklist:

  • Documentation has been updated, if necessary.
  • Examples have been added, if necessary. Tests create real DOCX inputs.
  • Tests have been added, if necessary.

Validation:

All 312 Word tests pass with the pinned docling-core 2.104.0 and with the companion core fix. The 10 focused parser tests also pass with the minimum supported core, 2.103.1, and fail on the original parser. They check the parsed tree, JSON round trip, equation order, hidden runs and numbering. make validate passes. Reference files are unchanged.

Merge dependency:

Ready for review. Please hold the merge until docling-core#933 is merged and released. That fix is needed for correct heading exports and chunk metadata when the heading also owns body content. Once a released version contains it, the minimum core dependency and lock file must be updated before this PR is ready to merge. No unreleased Git dependency or speculative version pin is added here.

The export and chunking regressions are tested in the core PR. PDF/model tests are outside this validation.

@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @its-amann, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 Merge protection satisfied — ready to merge.

Show 1 satisfied protection

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) bug Something isn't working docling-document docx issue related to docx backend labels Oct 28, 2026
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

Checked commit a3771408b8. The analysis read the diff only. It did not run code from this pull request.

The PR changes the DOCX backend so that headings with inline equations build an empty SectionHeaderItem with an InlineGroup child. The group holds the heading text and the FormulaItem children. The PR also adds equation tests for headings.

CI safety: no concern found. A maintainer must still approve CI.

1 related pull request(s) in the same code
PR State Signal Assessment
#4951 open changes the same code related: Both PRs change msword_backend.py, but 4951 fixes nested list hierarchy across empty spacer paragraphs. This PR fixes inline equations in headings.

The ai:* labels are advisory. They do not approve CI and they do not add tests:full. A new push runs this check again.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI first review (advisory): no blocker found

This review covers the full diff at 2b092d5642. It read the code and did not run it.

The change turns a heading with an equation into an empty SectionHeaderItem with an InlineGroup child. Consumers that read heading.text now get an empty string. The new tests also seem to need an unreleased docling-core change that the lock file does not contain.

A maintainer makes the merge decision. This review never approves the pull request.

hd = doc.add_heading(
parent=self.parents[parent_level],
text=text,
text="" if equations else text,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

question: Headings with equations now have empty text, and the tests may need an unreleased docling-core

A heading with an inline equation is now stored with text="" and orig="", and its content moves to an InlineGroup child. Anything that reads SectionHeaderItem.text now sees an empty title. Examples are JSON consumers, chunkers that use heading text as context, and any code that builds a table of contents. Before this change they saw the heading text with <eq> markers. The author says the Markdown and HTML heading output is correct only with the companion core PR (docling-core#933). The PR also says this core change is needed when a rich heading has body children. The new test test_heading_equation_has_inline_content always adds a body paragraph under the heading, and it asserts "$y=2$" in markdown. I could not read the installed docling-core source. uv.lock pins docling-core 2.104.0 and pyproject.toml allows >=2.103.1. Please answer three points: (1) Does the new test pass on the pinned core version, or does it need the unreleased core PR? If it needs the core PR, raise the docling-core minimum version in this PR, or merge after the core release. (2) Which exports and chunkers did you check with an empty heading.text? (3) Is the empty text intended for every consumer, or only for the Markdown and HTML serializers?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@ceberam the empty heading.text is intentional and follows the representation you requested in #5034. The text and equation remain in the leading inline group, including after a JSON round trip. Direct .text consumers need to read that rich content or use a serializer.

I checked the original 10 tests on pinned core 2.104.0: they pass, but the Markdown assertion only checked that the equation appeared somewhere. Parser tests now check the tree and JSON content. All 312 Word tests pass on 2.104.0, and the focused tests also pass on the minimum 2.103.1. The stronger export tests live in docling-core#933, which now also fixes chunk context and TOC titles. I checked Markdown, HTML, plain text, DocTags, JSON round trips, TOC formats and both Hierarchical/Hybrid chunkers.

I've kept this PR in draft until the core fix is released. The minimum core dependency and lock file will need that released version before merging; I haven't pinned an unpublished version.

@github-actions github-actions Bot added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Oct 28, 2026
@github-actions github-actions Bot removed ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) labels Oct 28, 2026
@its-amann
its-amann marked this pull request as draft October 28, 2026 18:35
@github-actions github-actions Bot added the ai:ci-safe AI triage found no CI-safety concern (advisory) label Oct 28, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI first review (advisory): no blocker found

This review covers the changes since 2b092d5642 at a3771408b8. It read the code and did not run it.

The change makes a heading with an equation have empty text and an InlineGroup child. The installed docling-core Markdown serializer only reads that group when the heading has exactly one child, because it checks len(item.children) == 1. A heading with body content under it has more children, so it will export wrongly until docling-core#933 is released. The reviewer must decide whether to merge before that release and before pyproject.toml requires it. The tests in the last push only check the tree.

Earlier findings

  • open (question): Headings with equations now have empty text, and exports may need an unreleased docling-core

A maintainer makes the merge decision. This review never approves the pull request.

@github-actions github-actions Bot added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Oct 28, 2026
@PeterStaar-IBM

Copy link
Copy Markdown
Member

@its-amann Is this ready for review?

@codecov

codecov Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@its-amann

its-amann commented Nov 6, 2026 •

Copy link
Copy Markdown
Author

Thanks for checking, @PeterStaar-IBM. I've marked this ready for review. The parser changes can be reviewed now. For merge, this still needs a release containing docling-project/docling-core#933 so headings with body content export correctly. Once that release is out, I'll bump the minimum core version and update the lockfile here.

@ceberam

ceberam commented Nov 11, 2026

Copy link
Copy Markdown
Member

@its-amann Indeed, we should first merge docling-project/docling-core#933 , launch a new release of docling-core , and then pin the new version on this PR. I've just added my review on docling-project/docling-core#933. Please, have a look at it whenever you can.

@its-amann

Copy link
Copy Markdown
Author

@ceberam thanks for merging docling core 933. is a core release planned soon? i'll bump the minimum version here right after

This branch has not been deployed

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

Labels

ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) bug Something isn't working docling-document docx issue related to docx backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DOCX: a heading with an inline equation keeps <eq> tags in its text

3 participants