Skip to content

fix(serializer): keep % and # in LaTeX includegraphics paths - #970

Open
shashank11yadav wants to merge 1 commit into
docling-project:mainfrom
shashank11yadav:fix/969-latex-escape-image-path
Open

shashank11yadav wants to merge 1 commit into
docling-project:mainfrom
shashank11yadav:fix/969-latex-escape-image-path

Conversation

@shashank11yadav

Copy link
Copy Markdown

With image_mode=ImageRefMode.REFERENCED, the LaTeX serializer copied the image URI into \includegraphics[width=\linewidth]{...} unchanged. A % in the path starts a TeX comment, which swallows the closing brace. A # gives "Illegal parameter number". The generated .tex file did not compile.

When the image path contains % or #, the \includegraphics line is now wrapped in \begingroup ... \endgroup, with \catcode set to 12 (other) for % and #. This differs from the \% escape suggested in #969: with pdflatex, \% or \# in an \includegraphics file name is an error (Missing \endcsname inserted). Paths without % or # are emitted exactly as before.

Scope:

  • Only % and # are handled. Braces and backslashes in file names are unchanged and left for a follow-up.
  • The catcode change only works where TeX reads the line directly. If the output is put inside another macro's argument (for example \fbox{...}), % and # are already read by then and the line still breaks.

Testing:

  • New test test_latex_referenced_image_path in tests/test_latex_serialization.py checks the exact emitted line for a path with %, a path with #, and a plain path. The % and # cases fail without this change. The plain path case passes before and after.
  • ruff check, ruff format --check and ty check pass.
  • Full pytest tests run: 1268 passed, 12 skipped, 1 xfailed. Five tokenizer-download test modules and two network tests were not run.
  • Compile check: I compiled the serializer's own output with pdflatex (TeX Live 2025) for a path with %, one with # and a plain one, with a real image at each path. With this change all three compile and load the image; on main the % and # cases fail. (nicefrac and ulem were dropped from the preamble for that run because they weren't installed locally; they don't affect images.) XeLaTeX and LuaLaTeX were not tested.

Fixes #969

With image_mode=REFERENCED the image URI was copied as is into
\includegraphics. A "%" in the path starts a TeX comment and swallows the
closing brace, and a "#" gives "Illegal parameter number", so the generated
.tex file no longer compiles.

Backslash escapes do not work in a graphicx file name, so when the path
contains "%" or "#" the line is wrapped in a group that makes both
characters ordinary. Paths without them are emitted unchanged.

Fixes docling-project#969

Signed-off-by: Shashank Yadav <65058495+shashank11yadav@users.noreply.github.com>
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

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

@mergify

mergify Bot commented Nov 3, 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)(?:\(.+\))?(!)?:

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) bug Something isn't working serialization Output serializers: Markdown, HTML, LaTeX, JSON, plain-text, outline labels Nov 3, 2026
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

AI triage (advisory)

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

This PR wraps \includegraphics commands in a TeX group with catcode changes when the image path contains % or #. This prevents compilation errors in generated LaTeX documents.

Topic labels added: bug, serialization.

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

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 added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Nov 3, 2026

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

I found no defect that blocks the merge. I checked that _serialize_image_part is called only from the picture serializer, so the new \begingroup … \endgroup line sits directly in the figure environment and not inside a macro argument. Paths without % or # are emitted as before, and the new test checks the exact emitted line for the %, # and plain cases.

A maintainer makes the merge decision. This review never approves the pull request.

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) ai:review-lgtm AI first review found no blocker or major issue (advisory) bug Something isn't working serialization Output serializers: Markdown, HTML, LaTeX, JSON, plain-text, outline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LaTeXDocSerializer does not escape the image path in \includegraphics, so "%" breaks the output

1 participant