Skip to content

fix(latex): resolve \input and \includegraphics for str sources - #4697

Merged
dolfim-ibm merged 1 commit into
docling-project:mainfrom
usmanmateen:fix-latex-str-source
Oct 9, 2026
Merged

dolfim-ibm merged 1 commit into
docling-project:mainfrom
usmanmateen:fix-latex-str-source

Conversation

@usmanmateen

Copy link
Copy Markdown
Contributor

Issue resolved by this Pull Request:
Resolves #4694

A str source goes through resolve_source_to_stream, so the LaTeX backend gets a BytesIO. The handlers resolved \input, \include, \includegraphics and the TikZ source root only when path_or_stream was a Path, so DocumentConverter().convert("paper/main.tex") dropped every included file and figure image without a log message.

This follows the pattern the HTML, Markdown and AsciiDoc backends already use:

  • LatexBackendOptions gets a source_uri: Optional[PurePath] field. It is a local path only, because the LaTeX backend only reads local files.
  • LatexFormatOption.backend_options_for_input fills source_uri from a str source that is not an http(s) URL, using the same AnyHttpUrl check as resolve_source_to_stream. Unlike HTML, it does this when no backend options are set too, because a Path input already resolves included files by default. User-supplied options are kept, and an explicit source_uri wins.
  • The backend computes _source_dir once, from the Path input or from source_uri, and the three handlers use it instead of checking isinstance(self.path_or_stream, Path).

Path inputs, streams without source_uri, http(s) sources and the CLI (#4532) behave as before. The path-traversal checks are unchanged and now run against _source_dir.

Checklist:

  • Documentation has been updated, if necessary. (the new field has a description; a str source now behaves like a Path)
  • Examples have been added, if necessary. (not needed)
  • Tests have been added, if necessary.

Review evidence:

  • The reproduction from LaTeX: a str source drops \input / \include files and \includegraphics figures #4694 prints str 'Main text.\n\nImage: fig.jpg\n\n<!-- image -->' [False] on main. With this change, the str and Path lines match: the \input section is there and the figure has an image.
  • tests/data/latex/sources/1706.03762/main.tex as a str source: 12,090 characters and no pictures on main; 60,797 characters and 5 figures with images here, the same as a Path source.
  • New tests in tests/test_latex/test_basic.py: a str source (absolute and relative to the working directory) resolves \input and \includegraphics; a stream with source_uri resolves them too; backend_options_for_input sets source_uri only for a local str, not for an http(s) URL or a Path, and keeps other options. All 4 fail on main and pass here.
  • pytest tests/test_latex: 137 passed (133 on main plus the 4 new tests).
  • ruff format --check, ruff check and tach check pass. ty check reports nothing new; the two warnings in latex/backend.py are on lines that are unchanged from main.
  • Not run locally: tests/test_cli.py, because my environment doesn't have torchvision. This PR doesn't change the CLI.

#4696 (CMYK figures) also touches handlers/macros.py, but in a different part of the file, so the two PRs don't overlap.

🤖 Generated with Claude Code

A str source is read into a stream before it reaches the LaTeX backend,
and the handlers resolved \input, \include, \includegraphics and the
TikZ source root only for a Path input. So convert("paper/main.tex")
silently dropped every included file and figure image.

Add source_uri to LatexBackendOptions, like the HTML, Markdown and
AsciiDoc backends have, and let LatexFormatOption fill it from a local
str source. The backend resolves relative files against the directory
of the Path input or of source_uri. http(s) sources stay unchanged, and
the path traversal checks are the same as before.

Resolves docling-project#4694

Signed-off-by: usmanmateen <usmanmateen255@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

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

@mergify

mergify Bot commented Oct 8, 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 CLI issues related to the command line interface (CLI) labels Oct 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

AI triage (advisory)

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

The PR fixes LaTeX \input, \include, and \includegraphics resolution when the source is passed as a str. It adds a source_uri field to LatexBackendOptions, fills it in LatexFormatOption.backend_options_for_input for local string paths, and derives _source_dir from it so the existing path-traversal checks and include handlers work with stream inputs.

Topic labels added: bug, CLI.

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

2 related pull request(s) in the same code
PR State Signal Assessment
#4696 open also references issue #4694; referenced as #4696 related: PR 4696 fixes CMYK JPEG figures in the LaTeX backend by normalising images before creating an ImageRef. It touches the same _process_macro function in macros.py but solves a different problem (image colour-mode conversion, not include resolution).
#4532 merged referenced as #4532 related: PR 4532 fixes the CLI so local LaTeX files are kept in place instead of copied, which preserves relative include paths. This PR fixes the same class of problem for SDK str sources, a path the CLI fix does not cover. The two PRs are complementary, not duplicates.

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 mentioned this pull request Oct 8, 2026
3 tasks done
@usmanmateen

Copy link
Copy Markdown
Contributor Author

cc @DanielNg0729 @adityasasidhar: this is the SDK str source case that #4531 (thanks @wak327) left out of the CLI fix in #4532. It follows the source_uri pattern of the HTML / Markdown backends. Happy to change the approach if you prefer another one.

@PeterStaar-IBM
PeterStaar-IBM self-requested a review October 9, 2026 06:15

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

@codecov

codecov Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.83333% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
docling/backend/latex/handlers/macros.py 83.33% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@dolfim-ibm
dolfim-ibm merged commit b2a59b9 into docling-project:main Oct 9, 2026
33 checks passed
dolfim-ibm pushed a commit that referenced this pull request Oct 14, 2026
MarkdownBackendOptions.source_uri resolves relative image paths, but
MarkdownFormatOption did not fill it in from the input source, so a
local Markdown file converted with fetch_images=True could not load
its own relative images. The other backends that use source_uri, HTML,
AsciiDoc, and LaTeX since #4697, fill it in from the input.

Add the backend_options_for_input override to MarkdownFormatOption, the
same way HTMLFormatOption and AsciiDocFormatOption already do it.

Signed-off-by: Abdel <62770500+hktitof@users.noreply.github.com>
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 CLI issues related to the command line interface (CLI)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LaTeX: a str source drops \input / \include files and \includegraphics figures

3 participants