docling icon indicating copy to clipboard operation
docling copied to clipboard

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

Open chaitanya0305 opened this issue 1 week ago • 4 comments

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.

chaitanya0305 avatar Nov 25 '26 17:11 chaitanya0305

I'd like to work on this too. I'll keep the default of the optional argument when the macro is registered and use it when a call has no [value]. Tests will cover \newcommand, \renewcommand and \providecommand.

VanshajPoonia avatar Nov 26 '26 13:11 VanshajPoonia

Thanks! Same here: a fix with tests is already prepared (branch fix/latex-newcommand-optional-arg) and I'll open the PR once a maintainer gives the go-ahead, so please hold off on this one too.

chaitanya0305 avatar Nov 26 '26 13:11 chaitanya0305

Sorry, I missed your branch link. I'll leave this one to you.

VanshajPoonia avatar Nov 26 '26 15:11 VanshajPoonia

Thanks, appreciated!

chaitanya0305 avatar Nov 26 '26 15:11 chaitanya0305