Skip to content

fix(latex): typeset non-breaking spaces, dashes and quotes - #4416

Open
MohammadHijjawi97 wants to merge 4 commits into
docling-project:mainfrom
MohammadHijjawi97:fix/latex-specials-text
Open

MohammadHijjawi97 wants to merge 4 commits into
docling-project:mainfrom
MohammadHijjawi97:fix/latex-specials-text

Conversation

@MohammadHijjawi97

Copy link
Copy Markdown
Contributor

pylatexenc parses ~, --, ---, `` and '' as specials nodes rather than chars, and the LaTeX backend skipped those nodes. Their text vanished and the surrounding words ran together, which is most visible in bibliographies, where author initials and page ranges are written this way.

Input:

See Fig.~1 on pages 10--20, written by Dr.~Smith---a ``famous'' author.

Before:

See Fig.1 on pages 1020, written by Dr.Smitha famous author.

After:

See Fig. 1 on pages 10–20, written by Dr. Smith—a “famous” author.

The fix emits the typeset text for these specials in body text and in text taken from macro/group arguments (e.g. \textbf{1990--2000}), via one SPECIALS_TEXT map in constants.py. ~ becomes a plain space, matching the existing \~ handling in macros.py.

The existing tilde test only checked that "Smith" was present; it now checks the spacing, and a new test covers dashes and quotes (both fail on main). The LaTeX ground truth was regenerated with DOCLING_GEN_TEST_DATA=1; the changes are corrections such as JimmyLei → Jimmy Lei, 770778 → 770–778, etal. → et al. and Figure[fig:model-arch] → Figure [fig:model-arch]. pytest tests/test_latex: 90 passed.

Checklist:

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

@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

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

@mergify

mergify Bot commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🔴 1 of 2 protections blocking · waiting on 👀 reviews

Protection Waiting on
🔴 Require two reviewer for test updates 👀 reviews
🟢 Enforce conventional commit —

🔴 Require two reviewer for test updates

Waiting for

  • #approved-reviews-by >= 2
This rule is failing.

When test data is updated, we require two reviewers

  • #approved-reviews-by >= 2

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

@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

DanielNg0729 added a commit that referenced this pull request Oct 8, 2026
* fix(latex): start a new text item at every \item

pylatexenc gives a plain \item an argument list of [None]. The list is
truthy, so since #4579 every \item takes the term branch, and the text
buffer is no longer flushed at an item. A list environment that is not
itemize, enumerate or description reaches this path as one node list.
Its items then merge into one text item: compactitem with "Alpha" and
"Beta" gives "Alpha\nBeta".

Flush the text buffer at every \item, before the term is added.

Signed-off-by: Daniel Nguyen <[email protected]>

* fix(latex): drop an \item term that converts to empty text

When the optional \item argument converts to empty text, the term
fallback used the raw LaTeX source. Macros that give no text then leak
into the export: \item[\textbullet] bullet macro gives
"\textbullet: bullet macro", and \quad and \hspace{1em} behave the same
way. Before #4579 these items gave "bullet macro".

Read the term in one helper for the inline path and for _nodes_to_text,
and drop a term that converts to empty text. The helper checks the
pylatexenc LatexGroupNode type instead of probing with hasattr. Table
cells use _nodes_to_text, so a list in a cell no longer gets the raw
macro either.

\item[--] now gives "Dash bullet" again, as before #4579. The backend
drops the -- ligature from all text today; #4416 works on dashes.

Signed-off-by: Daniel Nguyen <[email protected]>

* refactor(latex): replace attribute probing with type checks

AGENTS.md asks us to avoid hasattr(...) and getattr(...) probing. The
LaTeX backend probed nodes and cells in 36 places.

- An argument in argnlist has a node list only when it is a
  LatexGroupNode, so check that type.
- Four tree walkers repeated the same probes for the node list and the
  group arguments of a node. iter_child_nodelists() now yields these
  lists, with isinstance checks against the pylatexenc classes that have
  them.
- Every LatexNode has pos, and every LatexMathNode has displaytype, so
  read them directly. Check LatexSpecialsNode for the & cell separator.
- _parse_table set private attributes on TableCell for the spans and
  the multicolumn placeholders, and read them back with getattr. Keep
  (cell, col_span, row_span) in the row instead, and None for a covered
  column. This also removes three type: ignore comments.

The conversion output does not change.

Signed-off-by: Daniel Nguyen <[email protected]>

---------

Signed-off-by: Daniel Nguyen <[email protected]>
@PeterStaar-IBM PeterStaar-IBM added the latex issue related to LaTeX backend label Nov 6, 2026
@PeterStaar-IBM

Copy link
Copy Markdown
Member

Hi @MohammadHijjawi97, 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.

MohammadHijjawi97 and others added 2 commits November 16, 2026 17:45
pylatexenc parses ~, --, ---, `` and '' as specials nodes, not as
chars, and the backend skipped those nodes, so their text vanished and
the words around them ran together: "Fig.~1" became "Fig.1",
"Dr.~Smith" became "Dr.Smith", the page range "10--20" became "1020"
and ``quoted'' text lost its quotation marks. Bibliographies were hit
hardest, since author initials and page ranges are written this way.

Emit the typeset text for these specials, both for body text and for
text taken from macro and group arguments, and regenerate the LaTeX
ground truth accordingly.

Signed-off-by: Mohammad Hijjawi <[email protected]>
PeterStaar-IBM
PeterStaar-IBM previously approved these changes Nov 16, 2026

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

Approach is good, but grouped text with inline math still looses spaces and punctuation.

For example:
\textbf{See Fig.~1 on pages 10--20 and $x$.}

Expected:
See Fig. 1 on pages 10–20 and $x$.

Actual:
See Fig.1 on pages 1020 and $x$.

Please handle LatexSpecialsNode in _append_inline_nodes.
Also add tests for specials inside groups and formatted text that contain inline math.

A group or formatted text that contains inline math goes through
_append_inline_nodes, which skipped specials nodes. So
"\textbf{See Fig.~1 on pages 10--20 and $x$.}" still became
"See Fig.1 on pages 1020 and $x$.". Emit the typeset text for specials
there too, and add tests for plain groups, \textbf, \emph and
\bfseries groups with inline math.

Signed-off-by: Mohammad Hijjawi <[email protected]>
@MohammadHijjawi97

Copy link
Copy Markdown
Contributor Author

@maxmnemonic Thanks for the review. You were right: groups and formatted text that contain inline math go through _append_inline_nodes, and that path skipped LatexSpecialsNode. cfe9150 handles specials there with the same SPECIALS_TEXT mapping, so \textbf{See Fig.~1 on pages 10--20 and $x$.} now gives See Fig. 1 on pages 10–20 and $x$.

I added test_latex_specials_in_group_with_inline_math, which covers \textbf, \emph, a plain {...} group and a {\bfseries ...} group, each with inline math. All four cases failed before the change and pass after it. I also merged the latest main into the branch. tests/test_latex passes locally (228 passed), and ruff and ty are clean for the touched files.

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) bug Something isn't working priority:medium labels Nov 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

AI triage (advisory)

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

This pull request preserves LaTeX special characters during conversion. It emits text for non-breaking spaces, dashes, and quotation marks.

Topic labels added: bug, priority:medium.

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

Reference data changes

Change Files
tables 2
text 28
  • tables tests/data/latex/groundtruth/2310.06825_main.tex.json: table 4: 1 cell(s) changed; 28 text item(s) changed
  • tables tests/data/latex/groundtruth/2310.06825_main.tex.md: +28 / -28 lines; Markdown table rows changed
  • text tests/data/latex/groundtruth/1706.03762_main.tex.dclg: +44 / -44 lines
  • text tests/data/latex/groundtruth/1706.03762_main.tex.itxt: +21 / -21 lines
  • text tests/data/latex/groundtruth/1706.03762_main.tex.json: 35 text item(s) changed
  • text tests/data/latex/groundtruth/1706.03762_main.tex.latex: +37 / -37 lines
  • text tests/data/latex/groundtruth/1706.03762_main.tex.md: +37 / -37 lines
  • text tests/data/latex/groundtruth/2305.03393_main.tex.dclg: +19 / -19 lines
  • text tests/data/latex/groundtruth/2305.03393_main.tex.itxt: +4 / -4 lines
  • text tests/data/latex/groundtruth/2305.03393_main.tex.json: 19 text item(s) changed
  • text tests/data/latex/groundtruth/2305.03393_main.tex.latex: +25 / -25 lines
  • text tests/data/latex/groundtruth/2305.03393_main.tex.md: +19 / -19 lines
  • text tests/data/latex/groundtruth/2310.06825_main.tex.dclg: +29 / -29 lines
  • text tests/data/latex/groundtruth/2310.06825_main.tex.itxt: +12 / -12 lines
  • text tests/data/latex/groundtruth/2310.06825_main.tex.latex: +38 / -38 lines
  • text tests/data/latex/groundtruth/2412.19437_main.tex.dclg: +60 / -60 lines
  • text tests/data/latex/groundtruth/2412.19437_main.tex.itxt: +27 / -27 lines
  • text tests/data/latex/groundtruth/2412.19437_main.tex.json: 63 text item(s) changed
  • text tests/data/latex/groundtruth/2412.19437_main.tex.latex: +73 / -73 lines
  • text tests/data/latex/groundtruth/2412.19437_main.tex.md: +71 / -71 lines
  • text tests/data/latex/groundtruth/2501.00089_main.tex.dclg: +28 / -28 lines
  • text tests/data/latex/groundtruth/2501.00089_main.tex.itxt: +22 / -22 lines
  • text tests/data/latex/groundtruth/2501.00089_main.tex.json: 33 text item(s) changed
  • text tests/data/latex/groundtruth/2501.00089_main.tex.latex: +40 / -40 lines
  • text tests/data/latex/groundtruth/2501.00089_main.tex.md: +28 / -28 lines
  • text tests/data/latex/groundtruth/arXiv-2501.01300v2_main.tex.dclg: +9 / -9 lines
  • text tests/data/latex/groundtruth/arXiv-2501.01300v2_main.tex.itxt: +7 / -7 lines
  • text tests/data/latex/groundtruth/arXiv-2501.01300v2_main.tex.json: 8 text item(s) changed
  • text tests/data/latex/groundtruth/arXiv-2501.01300v2_main.tex.latex: +9 / -9 lines
  • text tests/data/latex/groundtruth/arXiv-2501.01300v2_main.tex.md: +9 / -9 lines

Assessment: expected. The reference data changes add the converted text for LaTeX non-breaking spaces, dashes, and quotes.

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

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) bug Something isn't working latex issue related to LaTeX backend priority:medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants