Skip to content

fix(latex): number the items of enumerate lists - #4518

Merged
DanielNg0729 merged 1 commit into
docling-project:mainfrom
pandasuwu:fix/latex-enumerate
Nov 8, 2026
Merged

DanielNg0729 merged 1 commit into
docling-project:mainfrom
pandasuwu:fix/latex-enumerate

Conversation

@pandasuwu

@pandasuwu pandasuwu commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

enumerate lists were converted to unordered lists (#4515). The LaTeX backend adds list items through the generic text path (doc.add_text(label=DocItemLabel.LIST_ITEM)), which creates them with enumerated=False, and nothing in the backend set it for enumerate. Markdown export therefore rendered - bullets, and the repository's ground truth recorded it: example_01.tex.md shows the items "First item in ordered list" and "Second item in ordered list" as bullets.

Input:

\begin{enumerate}
\item First step
\item Second step
\end{enumerate}

Before:

- First step
- Second step

After:

1. First step
2. Second step

Fix. The items of an enumerate are created with enumerated=True; a nested list keeps its own kind.

That alone is not enough, because docling-core numbers an item by its position among the children of its list group, and the backend could add more than one child per \item. Anything that flushes the text buffer started a new sibling in the list:

  • a nested list,
  • a blank line inside an item,
  • display math (\[...\], equation) or a quote, together with the text after it.

With numbering on, each of these would take a number, e.g. 1. First, the formula, 3. continuation, 4. Second for \item First \[x=1\] continuation. The new _process_list_item therefore creates the list item first and adds the whole content of the \item inside it; the leading text then becomes the text of the item itself. Every \item now adds exactly one child to its list, whatever it contains. This is the structure the Markdown and HTML backends already produce for nested lists.

The first paragraph of an item is written into the list item as it is added, whether it comes from the text buffer or is a paragraph between blank lines, and whichever call adds it. So it also becomes the text of the item after a leading blank line or when it is nested in a quote, another environment or a group, and no temporary item is created and deleted. Text added to a list item is split at blank lines, since a blank line inside the item text would end the list in a Markdown export. doc.delete_items() rescans the whole document, and calling it per item made list conversion quadratic; the list code no longer deletes items. List conversion time grows linearly with the number of items, as on main.

An empty \item (no content, or only a \label) used to add no list item, so every later item would take a number one lower than in the typeset document. Every \item now adds a list item, even an empty one. The content before the first \item is processed as before: whitespace, comments and macros such as \setlength add nothing, and stray text becomes a numbered item.

An item that starts with \footnote{...} or \newline keeps its text: a footnote is attached to the item as a child and does not become its text, and a \newline before any content has nothing to break and is dropped.

A display formula that starts an item, as in \item \[x=1\] First, goes on the line of the item: an inline group that also takes the text up to the next blank line or other content, as the Word backend does for an equation in a paragraph. The Markdown export is 1. $x=1$ First instead of 1. followed by the formula on its own line, which a CommonMark parser read as an empty item.

Rebased on main (one commit). Changes to fit the current backend:

Tests. test_latex_list_enumerate only asserted len(list_items) >= 2; it now checks the items are enumerated and the Markdown is 1. Alpha\n2. Beta. Nine new tests, all failing on main:

  • test_latex_enumerate_numbering_with_nested_content covers a nested list, text after it, a second paragraph and a nested enumerate, and checks the outer list is numbered 1 to 4.
  • test_latex_enumerate_item_with_block_content covers an item containing display math, an equation and a quote, and checks the list has two direct items numbered 1 and 2.
  • test_latex_enumerate_item_starting_with_footnote_or_newline checks that an item starting with a footnote or a \newline keeps its text, with the footnote inside the item.
  • test_latex_enumerate_item_starting_with_nested_text covers an item starting with a quote, a quote in a group and a center, and checks the Markdown is 1. First\n2. Second.
  • test_latex_enumerate_item_starting_with_paragraph_break covers an item whose first paragraph follows a blank line, on its own, in a quote and in a group, and checks the item text, its children and the Markdown 1. First\nContinuation\n2. Second.
  • test_latex_enumerate_item_starting_with_formula covers an item starting with \[...\] and with an equation, and checks the line of the item and the Markdown 1. $x=1$ First\n2. Second.
  • test_latex_enumerate_line_of_item_starting_with_formula checks that a nested list ends the line of such an item and a footnote does not.
  • test_latex_enumerate_empty_item_keeps_numbering covers an empty \item and one with only a \label, and checks the item after them is numbered 4.
  • test_latex_enumerate_content_before_first_item covers a comment, \setlength, a \label and stray text before the first \item.

test_latex_enumerate_markdown_of_item_with_block is marked as an expected failure until docling-project/docling-core#830 is released: the document tree keeps block content in its item, but the Markdown serializer writes it at column 0.

Ground truth. Regenerated with DOCLING_GEN_TEST_DATA=1; only list structure and numbering change:

  • example_01.tex: the ordered list is numbered (1., 2.; enumerate in .latex, class="ordered" in .dclg).
  • 2305.03393 (OTSL): the six syntax rules are numbered 1 to 6. The "Cross cell rule" item has a blank line between its title and its text, which used to produce a separate list item; that text is now inside rule 3, so the rules are not numbered 1 to 7. The .md and .dclg exports still write that text outside the item, which is the serializer issue that fix(markdown): indent the block children of a list item to its content column docling-core#830 fixes.

pytest tests/test_latex with the locked docling-core 2.106.0: 218 passed, 1 skipped, 5 expected failures. prek run on the changed files: passed (Ruff, ty, Tach, max lines).

Issue resolved by this Pull Request:
Resolves #4515

Checklist:

  • Documentation has been updated, if necessary.
  • Examples have been added, if necessary.
  • Tests have been added, if necessary.

@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @pandasuwu, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 All 2 merge protections satisfied — ready to merge.

Show 2 satisfied protections

🟢 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)(?:\(.+\))?(!)?:

🟢 Require two reviewer for test updates

When test data is updated, we require two reviewers

  • #approved-reviews-by >= 2

@codecov

codecov Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.17647% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
docling/backend/latex/utils/text.py 88.88% 2 Missing and 5 partials ⚠️
docling/backend/latex/handlers/environments.py 92.30% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@geoHeil geoHeil 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.

Please keep each source item as one list item when it contains a display equation or other content that flushes the text buffer. I confirmed the case below on c147215. All 124 LaTeX tests pass, but they do not cover this case.

@PeterStaar-IBM

Copy link
Copy Markdown
Member

@pandasuwu can you please follow up here?

@pandasuwu

pandasuwu commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor Author

@PeterStaar-IBM Done, could u please see my reply to @geoHeil's review above

@geoHeil geoHeil 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.

The earlier block-content case is fixed, and all 128 LaTeX tests pass on 4181905. The new per-item deletion causes a large speed regression; see below.

@pandasuwu
pandasuwu requested a review from geoHeil October 3, 2026 22:17

@geoHeil geoHeil 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.

The speed regression is fixed: 600 items take 0.016 s on this head, versus 0.016 s on the base. All 135 LaTeX tests pass. One Markdown regression remains for an item that starts with a quote; see below.

@geoHeil geoHeil 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.

The simple quote case is fixed. The block-content and speed fixes also hold. All 138 LaTeX tests pass on 464562c. A leading paragraph break still leaves a text-only item empty; see below.

Ruff, pinned ty 0.0.33 (existing warning policy), Tach, and the lock check pass. The local line check fails on 10 unchanged files. CI fast checks pass; the merge protection still needs two approvals.

Comment thread docling/backend/latex/backend.py Resolved
@pandasuwu
pandasuwu requested a review from geoHeil October 5, 2026 17:42

@geoHeil geoHeil 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.

Re-review of a9f31eb: the paragraph-break case is fixed and all 141 LaTeX tests pass. One P2 remains. See the inline comment.

@pandasuwu
pandasuwu requested a review from geoHeil October 16, 2026 06:17
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) bug Something isn't working markdown issue related to markdown backend labels Oct 17, 2026
@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

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

The PR makes the LaTeX backend number the items of enumerate lists. It adds _process_list_item, so each \item creates exactly one list item and holds all of its content, including nested lists, display math, quotes and links.

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

3 related pull request(s) in the same code
PR State Signal Assessment
#4416 open changes the same code related: That PR adds handling of LatexSpecialsNode for dashes and quotes. It shares only the same files and does not touch lists.
#5192 open changes the same code related: That PR maps text symbol macros such as \S in _char_macro_to_text. It touches the same file but fixes a different problem.
#4544 open changes the same code related: That PR changes the regex in _extract_verbatim_content for minted options. It shares only the file text.py and does not touch lists.

Reference data changes

Change Files
structure 1
text 8
  • structure tests/data/latex/groundtruth/2305.03393_main.tex.json: list_item: 12 → 11; text: 50 → 51
  • text tests/data/latex/groundtruth/2305.03393_main.tex.dclg: +2 / -3 lines
  • text tests/data/latex/groundtruth/2305.03393_main.tex.itxt: +1 / -1 lines
  • text tests/data/latex/groundtruth/2305.03393_main.tex.latex: +3 / -3 lines
  • text tests/data/latex/groundtruth/2305.03393_main.tex.md: +7 / -7 lines
  • text tests/data/latex/groundtruth/example_01.tex.dclg: +1 / -1 lines
  • text tests/data/latex/groundtruth/example_01.tex.json: item attributes changed
  • text tests/data/latex/groundtruth/example_01.tex.latex: +2 / -2 lines
  • text tests/data/latex/groundtruth/example_01.tex.md: +2 / -2 lines

Assessment: expected. The PR sets enumerated=True for enumerate items, so the example_01.tex Markdown and JSON change from bullets to numbers. The moved text in 2305.03393_main.tex and the count change from 12 to 11 list items match the new structure, where each \item holds its content. Only the summary was available, so I could not check the exact lines.

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

DanielNg0729
DanielNg0729 previously approved these changes Oct 18, 2026
@PeterStaar-IBM

Copy link
Copy Markdown
Member

@geoHeil Is this ready to be merged, would like to merge this before mine (#4817)

@geoHeil

geoHeil commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Thanks for the updates. I rechecked cdb2ee16 against a1c5ff2a. The numbering, formula, and speed fixes hold. The LaTeX suite gives 145 passed and 5 expected failures on Python 3.10.19 with locked docling-core 2.99.0.

One existing blocker remains: block content can leave its list item in Markdown. I ran the five cases in test_latex_enumerate_markdown_of_item_with_block with --runxfail; all five fail. They cover theorem, verbatim, lstlisting, tabular, and text followed by verbatim. The document tree keeps the content, but the Markdown parser does not keep the same list structure.

docling-core #888 is still open. Please keep this PR on hold until that fix is released and required here. Then remove the expected-failure marker and update the affected tests and reference output. The backend changes otherwise look good in the cases I checked.

Ruff, ty with the current warning policy, Tach, dprint, and the lock check pass. make check stops at 10 unchanged files over the line limit; the base has the same failures. These results cover the LaTeX suite and targeted reproductions, not the full local test suite.

@geoHeil geoHeil 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.

see the comment

@pandasuwu

pandasuwu commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Hello @geoHeil, I saw that docling-project/docling-core#888 will be dropped for docling-project/docling-core#830 casue the latter handles two more inline cases. So this PR now has to wait until #830 is merged and released.

@vagenas

vagenas commented Nov 4, 2026

Copy link
Copy Markdown
Member

@pandasuwu a heads-up for your next rebase: since #3891 and docling-core 2.106.0, serializers join InlineGroup runs with no separator, so runs must keep their source whitespace.

With cdb2ee16 and docling-core 2.106.0, \item \[x=1\] First exports as 1. $x=1$First, but test_latex_enumerate_item_starting_with_formula expects 1. $x=1$ First. Both flush_text_buffer and _add_text strip the text that goes on the line of the item, so the space is lost. The locked docling-core 2.99.0 hides this, because it adds a space between runs.

Please keep this space when you rebase.

@PeterStaar-IBM

Copy link
Copy Markdown
Member

Hi @pandasuwu, thank you for this pull request.

It has a merge conflict with main. Please rebase your branch on the latest main and resolve the conflict:

git fetch upstream
git rebase upstream/main
# resolve the conflicts, then:
git push --force-with-lease

We can continue with the review and CI after that.

@PeterStaar-IBM PeterStaar-IBM added the latex issue related to LaTeX backend label Nov 6, 2026
The items of an enumerate are created with enumerated=True, and the
content of each \item is added inside one list item, so every \item
adds exactly one child to its list and the numbering matches the
document. The first paragraph of an item becomes its text; a formula
that starts an item goes on the line of the item, with the text after
it. Empty items are kept.

Rebased on main: a paragraph with inline math in a list item goes on
the line of the item, a footnote in an item stays a child of the item,
and text that continues the line keeps its leading space, since
serializers join InlineGroup runs without a separator.

Resolves docling-project#4515

Signed-off-by: pandasuwu <[email protected]>
@pandasuwu
pandasuwu force-pushed the fix/latex-enumerate branch from cdb2ee1 to 2d4fff9 Compare November 7, 2026 06:47
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) labels Nov 7, 2026
@pandasuwu

Copy link
Copy Markdown
Contributor Author

@PeterStaar-IBM The rebase is done. The branch is now one commit (2d4fff9c) on top of current main (1e26b84c), and the merge conflict is resolved.

Changes to fit current main:

pytest tests/test_latex with the locked docling-core 2.106.0: 218 passed, 1 skipped, 5 expected failures. prek run on the changed files passes. The expected failures are still the Markdown cases that wait for docling-project/docling-core#830, as @geoHeil noted. I updated the PR description with these details.

@PeterStaar-IBM PeterStaar-IBM left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm!

@PeterStaar-IBM
PeterStaar-IBM dismissed geoHeil’s stale review November 7, 2026 12:19

seems all has been addressed

@DanielNg0729
DanielNg0729 merged commit 1d9a79c into docling-project:main Nov 8, 2026
34 checks passed
PeterStaar-IBM pushed a commit that referenced this pull request Nov 19, 2026
Since #4518 the list handler splits a list body into items before it
reads an \input file. The \item lines of the file were then processed
inside the previous item, so they became plain text and the later items
got the wrong numbers.

Read each \input file of a list body first and splice its nodes into the
body, as LaTeX does with plain text inclusion. The file reading moves to
_read_input(), which keeps the path, cycle and depth checks of the
\input handler.

Signed-off-by: Christoph Auer <[email protected]>
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) bug Something isn't working latex issue related to LaTeX backend markdown issue related to markdown backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LaTeX backend converts enumerate lists to unordered (bullet) lists

5 participants