LaTeX: custom macros with an optional argument print the raw definition (#1, #2!) instead of expanding
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
Research direction
Start with docling/backend/latex_backend.py and the regression cases in tests/test_latex/test_macros.py; compare how macro definitions and calls handle required arguments with the optional first argument described here. Run the LaTeX tests and check that omitted and explicit optional values expand correctly, while bracketed text after required arguments stays literal. The issue reports tests/test_latex passing with its proposed fix.
Written by the indexing model from the issue text.
Description
Summary
The LaTeX backend ignores the optional first argument of user-defined macros. For a macro such as \newcommand{\greet}[2][Hello]{#1, #2!}, docling expects two braced arguments:
\greet{World}(no optional argument) prints the unexpanded definition followed by the argument:#1, #2!World.\greet[Hi]{There}prints#1, #2![Hi]There.
Macros with a default first argument are common in paper preambles (notes, todo markers, notation helpers), so placeholder text such as #1 ends up in body text and headings. The same happens with \renewcommand, \providecommand and \newcommand*.
Reproduction
Run on current main (33f33aaf):
from io import BytesIO
from docling.backend.latex_backend import LatexDocumentBackend
from docling.datamodel.base_models import InputFormat
from docling.datamodel.document import InputDocument
src = rb"""\documentclass{article}
\newcommand{\greet}[2][Hello]{#1, #2!}
\begin{document}
\greet{World} and \greet[Hi]{There}.
\end{document}"""
d = InputDocument(path_or_stream=BytesIO(src), format=InputFormat.LATEX,
backend=LatexDocumentBackend, filename="t.tex")
doc = LatexDocumentBackend(in_doc=d, path_or_stream=BytesIO(src)).convert()
print([t.text for t in doc.texts if t.content_layer.value == "body"])
Actual
['#1, #2!World and #1, #2![Hi]There.']
Expected
['Hello, World! and Hi, There!.']
Cause
When a custom macro is registered, the backend stores the number of arguments and the definition, but not the [default] of the optional first argument. At a call it then expects all arguments as braced groups. With one group missing, expansion fails and the raw definition is printed; an explicit [value] stays in the text.
Proposed fix
Store the default of the optional argument when the macro is defined, as raw LaTeX. At a call, use a [value] that directly follows the macro (whitespace allowed, as in LaTeX), or else the default, and then read the remaining braced arguments as before.
- Bracketed text after the arguments is not taken as the optional argument:
\greet{X} [1]givesHello, X! [1]. - For a macro with only an optional argument,
\one[Y].gives<Y>.. - An optional value that contains markup (
[\emph{Hi}], nested brackets) falls back to the current output. That is a known limitation of this change.
I converted all 24 LaTeX sources in tests/data/latex/sources with and without the fix: the Markdown output is identical, so no groundtruth changes. There are regression tests in tests/test_latex/test_macros.py. They fail on main and pass with the fix; tests/test_latex passes. (test_e2e_latex_conversions fails locally on main too, because picture rendering is not available in my environment.)
Related: #3208 / #3223 added expansion of required macro parameters only.
I used an AI coding assistant (Claude) to find and analyse this; I reproduced it and checked the fix myself.
I've tested a fix with a regression test: https://github.com/chaitanya0305/docling/compare/main...chaitanya0305:docling:fix/latex-newcommand-optional-arg. If this is accepted, I'll open the PR straight away.
- Dominant language
- Python
- Stars
- 71k
- Forks
- 5.4k
- Avg merge
- 2d 22h
- Merged PRs (30d)
- 161
Getting set up
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from docling-project/docling
-
Difficulty 2/5 1-3 hours Newbie friendliness 69/100
docling-project/docling#5592 ·
Maintainers usually reply within 2 days
-
`api_image_request` logs and propagates the raw error response body, leaking the request's API key on 401Possibly taken @prassu017 claimed this 7 days ago. Openbug priority:critical
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
docling-project/docling#5486 · 1 comment · 1 reaction ·
Maintainers usually reply within 2 days
-
AFP: export_to_doctags() raises ZeroDivisionError because every page has size 0x0Possibly taken @Krushil-Dhankecha claimed this 12 days ago. Openbug priority:medium
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
docling-project/docling#5355 · 1 comment ·
Maintainers usually reply within 2 days
-
AsciiDoc: thematic breaks and page breaks are kept as text or turned into list itemsPossibly taken @sohailshk claimed this 12 days ago. Openasciidoc bug priority:low
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
docling-project/docling#5331 · 1 comment ·
Maintainers usually reply within 2 days
-
Merged paragraph across pages of different heights: continuation bbox uses the first page's heightPossibly taken @galer1us claimed this 15 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
docling-project/docling#5286 ·
Maintainers usually reply within 2 days
All issues in docling-project/docling
Similar issues
-
app: scripts status: backlog type: housekeeping
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
netbox-community/netbox-scripts#239 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
LmeSzinc/AzurLaneAutoScript#6194 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
micronaut-projects/micronaut-core#14585 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
langgenius/graphon#443 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
Maintainers usually reply within 1 day