Repository navigation
feat: added support for parsing LaTeX (.tex) documents - #2890
Conversation
|
✅ DCO Check Passed Thanks @adityasasidhar, all your commits are properly signed off. 🎉 |
Merge ProtectionsYour pull request matches the following merge protections and will not be merged until they are valid. 🟢 Enforce conventional commitWonderful, this rule succeeded.Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
🟢 Require two reviewer for test updatesWonderful, this rule succeeded.When test data is updated, we require two reviewers
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Updated: Improved Test CoverageThe initial submission passed all 25 required CI checks (lint, tests across Python 3.9-3.14, examples, docs, DCO) but missed the codecov threshold (57% vs 73.16% target). I've now added 33 additional unit tests (39 total) to achieve ~86% coverage, well above the target. New tests cover:
Also fixed a validation bug where Awaiting workflow approval to run CI. Thank you! |
PeterStaar-IBM
left a comment
There was a problem hiding this comment.
These are just my first comments, I think we will need a few iterations.
Having that said, really impressive so far, good code quality and love the approach!
|
@PeterStaar-IBM Thank you for the thorough review and kind words! All feedback has been addressed in commit Changes implemented:
Test coverage: 40/40 tests passing, all linters passing ✓ Ready for re-review! Let me know if you'd like any adjustments. PS. Will work on future enhancements through multiple iterations |
|
dang it i forgot to DCO sign off on the commit ( cries internally ) |
|
@PeterStaar-IBM Thank you for the thorough review and kind words! All feedback has been addressed in the commit.
Test coverage: 40/40 tests passing, all linters passing ✓ Ready for re-review! Let me know if you'd like any adjustments. |
|
@adityasasidhar Nice progress. Maybe this is a good point to add some sample latex file into |
|
Will do> @adityasasidhar Nice progress. Maybe this is a good point to add some sample latex file into |
@cau-git @PeterStaar-IBM Thank you for the feedback! I've significantly overhauled the implementation to allow for end-to-end ground-truth validation against real-world scientific papers. To ensure the backend is robust enough for general use, I added the full source code for 4 major arXiv papers (Attention Is All You Need, DeepSeek V3, Mistral 7B, and OTSL) to the test suite and regenerated the ground-truth data. Testing against these complex documents revealed several real-world parsing challenges which I have now resolved: Split-file Documents: Added recursive support for \input{} and \include{} to handle papers that are split across multiple There's still a lot of work to perfect it, I had some ideas reagrding restructuring tables and all using llm's, the user can probably pass some arguments regarding it to use ( just my thoughts ), as you see manual parsing through the latex code leaves us with a lot of gaps and using a small language model to probably assist with the use case might be too complex for now but yeah gave it a bit of thought.....I'll probably give some more attempts at making it perfect but yeah real world use cases ain't much ( cries internally wishing the world was perfect ) |
@vku-ibm Please have a look at this PR. Maybe you can run it over our internal latex collection and give some general feedback. Overall, we can not expect that all features of latex are supported by this PR (there are always additional feat/fix-PR's that can be done). However, we need to ensure a base level of robustness. |
|
committted the changes with proper dco sign off |
|
@adityasasidhar took some random arxiv publications for tests. These are my findings so far:
from pdf:
the above was done serializing to markdown using older methods
EDIT: extraction of figures is supported when modern format of latex is used, the code mentioned above handles graphics provided as pdfs. Old latex documents often use ps and eps formats. There are more findings but I need to clarify and cross check them first. |
Becomes this in docling document: and when serialized, these "breaks" consequently become paragraph separators. |
yes, these should become inline formula's |
|
@vku-ibm @PeterStaar-IBM thank you for the thorough testing! Working on fixes for: |
isn't it better to preserve the scientific notations as we are parsing through academic papers...most probably someone would want the extra bit of context if you are specfically gonna put .tex files..but all in all a project is supposed to be consistent...your call please |
My apologies for not being clear. Yes, we do want to preserve scientific notation and this backend does it. I was pointing out that docling doesn't do it by default with the pdf backend. So this point wasn't the "bug report" but an observation. |
@vku-ibm my bad if I came off accusing, almost done with the fixes, testing it a bit for edge cases...will push it once finished |
@adityasasidhar no worries, we (=docling team) dont take these things personal, just love your enthusiasm! Latex is a real pain sometimes, and we need to get some good rough coverage before we merge. The next iterations can then be improvements. |
|
@vku-ibm @PeterStaar-IBM Thank you for the detailed feedback and testing! I have pushed a new commit that addresses all the reported issues. Here is a summary of the fixes and verification: 🛠️ Fixes Implemented Fix: Adjusted the regex preprocessing to safely strip comments, including files starting with %. Fix: Modified Fix: Refactored Fix: Implemented Expanded Unit Tests: Added 3 new tests specifically for these edge cases. |
|
I've tried latest version and it looks great! Couple of the findings:
example from https://arxiv.org/abs/2501.00089 ,
same document as above.
becomes: The sample is https://arxiv.org/abs/astro-ph/0001001
becomes:
becomes: and later in the same document, another sample that breaks the same way:
The rest of the document didn't get parsed. |
Signed-off-by: Aditya Sasidhar <[email protected]>
|
@vku-ibm hey! I know it’s been a while since my last push (some might say I ran out of Claude credits… hehehe 😄), but I’m back with a major update. I’ve addressed the pending feedback and significantly hardened the LaTeX parsing logic. Below is a summary of what’s included in this merge. ✨ Summary of Changes🔧 Robust Macro Extraction
📝 Inline Text Preservation
🧱 Structural vs Inline Distinction
🔤 Special Character Support
📊 Table Parsing Stability
🧪 End-to-End Testing
can you please run it over some more latex files and update me about the edge cases and things i may have missed out, also i had an idea about handling malformed .tex file by converting them into pdf and running it over the standard docling pdf conversion methods...love to know what you think about it....maybe use a small language model for tweaks...but yeah even that has its own tradeoffs.... |
Signed-off-by: Aditya Sasidhar <[email protected]>
…[email protected]> I, Aditya Sasidhar <[email protected]>, hereby add my Signed-off-by to this commit: 7e03263 I, Aditya Sasidhar <[email protected]>, hereby add my Signed-off-by to this commit: aeba688 Signed-off-by: Aditya Sasidhar <[email protected]>
|
The LaTeX backend now provides robust, structured extraction for well-formed and moderately malformed academic LaTeX sources. For severely malformed inputs, the backend degrades gracefully while preserving content order, mathematical notation, and as much document structure as is reasonably recoverable. Support for legacy LaTeX patterns, EPS graphics, and deeper heuristic repairs is intentionally scoped for follow-up iterations. In hindsight, Your earlier comment was spot on: getting solid rough coverage first is the right strategy. Older LaTeX in particular is extremely inconsistent, and full semantic recovery is not realistically achievable in a single pass. Based on experiments with legacy documents and prior art from tools like Pandoc, the current approach prioritizes structural preservation over aggressive macro expansion. This allows us to salvage context reliably without introducing brittle behavior or silent corruption. Subsequent iterations can focus on targeted improvements where they add clear value. |
sounds good! @vku-ibm If you can run a few more tests |
|
@adityasasidhar please run a few times until all is green. |
|
Testing current version.
also https://arxiv.org/abs/hep-lat/0001005 has the same issue.
and
|
|
Thanks for the continued testing and very detailed feedback. At this point, all remaining failures are concentrated in very early arXiv-era LaTeX sources (roughly 1999–2001). These documents rely heavily on TeX macro programming, ad-hoc preamble logic, malformed environments, and execution time semantics that cannot be reliably recovered without running a full TeX engine. A useful way to think about this: trying to parse and walk pre-2005 LaTeX without executing TeX is a bit like trying to convert into "Peter". You can recognize fragments, infer intent, and recover partial structure, but you're fundamentally reverse-engineering something that was never designed to be transformed without being executed. This explains the observed behavior in legacy sources, including:
I further went ahead and took 100+ papers from the past 26 years and extensively tested variations of papers in each era ( an era is being accounted as 5 years in this case) and the results are promising. These are not regressions in modern papers. The backend behaves correctly on post-2005 LaTeX and degrades deterministically on legacy inputs rather than silently corrupting output. This matches the behavior of existing tools such as Pandoc, which also do not fully recover these documents unless a TeX engine is executed. Attempting to fully support pre-2005 LaTeX where macro expansion is frequently abused and structural conventions are inconsistent, within a single implementation would introduce brittle behavior and risk degrading the backend's reliability on modern documents. Based on extensive testing across papers from the past 21 years ( 2005 to 2026) , the current backend shows fewer than a handful of issues on average for post-2005 sources, and this number is expected to decrease further as dangerous macro expansion paths are reduced. Lessons taken from the current feeback by @vku-ibm: I am currently in the process of replicating the protocol followed by the Pandoc tool which is the most popular tool for universal markup conversion to handle the legacy .tex files to improve the legacy tex code handling Request from my sideCan you please test the current implementations on some more modern .tex papers and report any bugs... Thank you again for your patience, I'm happy to iterate accordingly |
will do |
|
Primarily older tex files are a pain.....I started with a trade off to actually perfect the modern latex papers which are about 95 percent of the tex files in total, and later go ahead and deal with the pre 2005 papers... |
totally agree! For me, it is now just satisfying the linter, simply run a few times |
Signed-off-by: Aditya Sasidhar <[email protected]>
Hey @PeterStaar-IBM, I ran the required |
|
@adityasasidhar I've run a conversion on about 10k of arxiv documents. There were very few problematic ones, I've created a separate issue for that as this PR is at pretty good state to be merged: |
Issue resolved by this Pull Request:
Resolves #2885
This PR introduces initial support for parsing LaTeX (.tex) documents as a structured input format, using
pylatexenc. This allows Docling to convert native LaTeX files directly to Markdown while preserving semantic structure.Key changes:
\section,\subsection,abstract) to Docling's model.$) and display ($$,\[) math, plus environments likeequation,align,gather, andmultline(including starred variants).tabularenvironments, including handling of escaped characters and\hlinecleanup.\cite) and references (\ref).docling convert file.tex.Checklist: