Skip to content

LaTeX backend strips the first-line indentation of verbatim and lstlisting code #4501

Closed

Description

@ankit2235

Bug

Same problem as #4485 (fixed for Markdown in #4487), in the LaTeX backend. _extract_verbatim_content (docling/backend/latex/utils/text.py) returns match.group(1).strip(), which removes the leading indentation of the first code line while later lines keep theirs. The relative indentation changes, so the code itself is altered.

It affects verbatim and lstlisting (both go through this function). Code whose first line is not indented is unaffected.

Steps to reproduce

from io import BytesIO

from docling.datamodel.base_models import DocumentStream, InputFormat
from docling.document_converter import DocumentConverter

tex = (
    "\\documentclass{article}\n\\begin{document}\n"
    "\\begin{verbatim}\n  key: 1\n  sub:\n    x: 2\n\\end{verbatim}\n"
    "\\end{document}\n"
)
doc = DocumentConverter(allowed_formats=[InputFormat.LATEX]).convert(
    DocumentStream(name="a.tex", stream=BytesIO(tex.encode()))
).document
print([t.text for t in doc.texts if t.label == "code"])

Actual: ['key: 1\n sub:\n x: 2']
Expected: [' key: 1\n sub:\n x: 2']

Docling version

2.132.0 (main @ a1c5ff2)

Python version

3.12.14

I'd like to send a PR, using the same trimming as #4487.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions