LaTeX: custom macros with an optional argument print the raw definition (#1, #2!) instead of expanding

Open
#5,589 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
latex, python
Domain
backend

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] gives Hello, 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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from docling-project/docling

All issues in docling-project/docling

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.