Skip to content

fix(latex): convert text symbol macros such as \S and \ldots - #5192

Open
Fahmid-Arman wants to merge 2 commits into
docling-project:mainfrom
Fahmid-Arman:fix/latex-text-symbol-macros
Open

Fahmid-Arman wants to merge 2 commits into
docling-project:mainfrom
Fahmid-Arman:fix/latex-text-symbol-macros

Conversation

@Fahmid-Arman

Copy link
Copy Markdown
Contributor

Issue resolved by this Pull Request:
Resolves #5191

_char_macro_to_text converted accent and letter macros to Unicode, but any other macro without arguments fell through and its text was lost, so \S, \ldots, \textbackslash, \copyright, \LaTeX and similar symbols disappeared from paragraphs and headings. A new MACROS_TEXT_SYMBOLS map in constants.py gives the character for the common text-mode symbol macros, and _char_macro_to_text uses it after the accent and letter checks. The map is explicit because latex2text returns an empty string for several of them (\S, \P, \copyright, \pounds, \euro, \LaTeX, \TeX). \textbullet stays out: \item[\textbullet] is read as the default list marker, which test_latex_item_term_without_text relies on.

Checklist:

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

Review evidence:

Source Before After
As shown in \S 3, the path C:\textbackslash tmp holds 1, 2, \ldots, n files. As shown in 3, the path C:tmp holds 1, 2, , n files. As shown in §3, the path C:\tmp holds 1, 2, …, n files.
\copyright{} 2024 ACME. Typeset with \LaTeX. 2024 ACME. Typeset with . © 2024 ACME. Typeset with LaTeX.
  • New test_latex_text_symbol_macros in tests/test_latex/test_macros.py, next to test_latex_accent_macro. It fails on main and passes with the change.
  • No reference data changes: converting every source under tests/data/latex gives the same documents as the groundtruth .json files.
  • pytest tests/test_latex: all pass except test_e2e_latex_conversions, which fails on main here as well because this environment has no renderer for the LaTeX figures (predicted image is None); its Markdown, indented text, DocLang and LaTeX comparisons pass.
  • ruff format and ruff check (v0.17.2) are clean on the changed files.

@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @Fahmid-Arman, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Nov 5, 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 labels Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

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

The PR makes the LaTeX backend convert text-symbol macros such as \S, \ldots, \copyright and \LaTeX to their characters. Before, the text of these macros was lost. A new MACROS_TEXT_SYMBOLS map and a new test are added.

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

1 related pull request(s) in the same code
PR State Signal Assessment
#4416 open changes the same code related: Both PRs edit the same LaTeX text code and the same test file. PR 4416 handles specials (~, dashes, quotes) in _nodes_to_text. This PR handles argument-less symbol macros in _char_macro_to_text. The two fixes do not conflict.

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 f62a169641. It read the code and did not run it.

No blocking issue found. _char_macro_to_text runs before the other macro branches. It skips any name that the document redefines (self._custom_macros). Both the inline path (handlers/macros.py:428) and the text path (utils/text.py:317) call it. Headings, captions and \item terms get the new mapping too.

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 Nov 5, 2026
@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!

@PeterStaar-IBM PeterStaar-IBM added the latex issue related to LaTeX backend label Nov 6, 2026

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

@Fahmid-Arman hey could you run this on a larger latex corpus so that we can look out for edge cases. thank you 😃.

@Fahmid-Arman

Copy link
Copy Markdown
Contributor Author

Sure. I ran this PR, #5188 and #5190 over 27 arXiv papers. I downloaded 30 sources, and 27 have a LaTeX main file. They include BERT, ResNet, Adam, GAN, VAE, GPT-3, ViT, U-Net, InstructGPT, LoRA, chain of thought, T5, LLaMA, Llama 2, DDPM, RoFormer, YOLOv3, XGBoost, the ATLAS Higgs paper, Shor's factoring paper and Perelman's Ricci flow paper. I converted each paper with the commit the three PRs branch from (d59ca4a9) and with each PR, then compared the Markdown and the text items.

  • All 108 conversions finished, with no errors or timeouts.
  • This PR changed 8 papers. Each change adds a character that was dropped before, and no other words change:
    • \S gives §: 55 uses in 3 papers, for example "As we will show in §[sec:exp]" and "In §§3 and 4".
    • \ldots gives …, \textendash gives –, \textasciitilde gives ~ ("~4k prompts"), \textbackslash gives \ ("Q: {question}\nA:"), \LaTeX gives LaTeX, and \textgreater{}= gives >=.
    • In one Llama 2 table, cells that hold only \textendash now show –. Because of that, the Markdown export aligns those columns left instead of right. The numbers are the same.
    • \S inside math ($\S 1$ in Perelman's paper) is still left to the math handling.
  • fix(latex): read \paragraph and \part titles as headings #5188 changed 11 papers. The 171 \paragraph titles in 12 papers are now level-5 headings, and the text after each title is its own paragraph. The word sequence is the same as before in every paper except one: in LLaMA, \paragraph{Rotary Embeddings \citep{GPTNeo}.}\hspace{-3pt}We remove… used to give "[GPTNeo].We remove", and now the title is a heading and "We remove…" starts the paragraph. One paper (1706.02677) redefines \paragraph with \@startsection in its preamble. Its output is the same as on the base commit, where the redefinition also shows up as text, so this PR doesn't change it.
  • fix(latex): keep braces and spaces inside \verb #5190 changed none of the 27 outputs. The 4 \verb uses in these papers (\verb|np.random.pareto|, \verb|[class]| and similar) have no braces or spaces, and those are the only cases the PR changes.

One thing the corpus shows that none of these PRs touch: a ~ before \S or \ref is dropped, so "in~\S3" gives "in§3". That is the ~ handling in the open #4416.

@adityasasidhar

Copy link
Copy Markdown
Contributor

@Fahmid-Arman great!

PeterStaar-IBM
PeterStaar-IBM previously approved these changes Nov 16, 2026
@Fahmid-Arman

Copy link
Copy Markdown
Contributor Author

I merged main to fix the conflict with #5190, which added a test at the same spot in tests/test_latex/test_macros.py. Both tests are kept, and nothing else changed. The push reset the approvals, sorry about that.

@github-actions github-actions Bot removed the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Nov 17, 2026
@maxmnemonic

Copy link
Copy Markdown
Member

I found a regression, when these symbols appear inside custom macro arguments:

\documentclass{article}
\newcommand{\identity}[1]{#1}
\begin{document}
\identity{Path C:\textbackslash tmp.}

\identity{Cost \textdollar 5 today.}
\end{document}

Expected:

  • Path C:\tmp. as text.
  • Cost $5 today. as text.

Actual:

  • Path C:. — tmp is lost.
  • $5 today. becomes a formula item.

The symbols are converted before custom macro expansion finishes. The expansion is then parsed again, so the literal backslash starts a command and the literal dollar sign starts math.

This introduces a regression: the base drops the symbols but preserves tmp and keeps the price as text. Two regression cases pass on the base and fail on this PR.

Please preserve the LaTeX source during argument substitution and decode the symbols after expansion. These two cases should also be added to the tests.

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

can you please check this: #5192 (comment)

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

See my comment above

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LaTeX backend drops text symbol macros such as \S, \ldots and \copyright

4 participants