docling icon indicating copy to clipboard operation
docling copied to clipboard

LaTeX: \multicolumn and \multirow spans are lost and their arguments leak into cells (2cBLEU, phantom columns)

Open chaitanya0305 opened this issue 1 week ago • 4 comments

Summary

The LaTeX backend loses \multicolumn and \multirow spans in real documents. pylatexenc does not know these two macros, so the table parser re-reads their arguments from the environment's source using the node's position. That position counts from the start of the whole document, while the source string starts at the environment.

  • When anything comes before the table (a preamble, a paragraph), the spans are dropped and the raw arguments end up in the cell text: Model becomes 2*Model, BLEU becomes 2cBLEU.
  • Even when the table is at the very start of the input, the argument groups are read a second time as an extra cell, which adds phantom columns.

Grouped table headers are common in papers, so spanned headers, and every table built from them, come out misaligned in Markdown, HTML and JSON exports. In the test corpus, the results table of 1706.03762 (the Transformer paper) has 43 columns instead of 6.

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

def conv(src):
    d = InputDocument(path_or_stream=BytesIO(src), format=InputFormat.LATEX,
                      backend=LatexDocumentBackend, filename="t.tex")
    return LatexDocumentBackend(in_doc=d, path_or_stream=BytesIO(src)).convert()

after_text = rb"""\documentclass{article}\begin{document}
Results below.
\begin{tabular}{ccc}
\multirow{2}{*}{Model} & \multicolumn{2}{c}{BLEU} \\
 & EN-DE & EN-FR \\
Transformer & 28.4 & 41.8 \\
\end{tabular}\end{document}"""
t = conv(after_text).tables[0].data
print([(c.text, c.start_row_offset_idx, c.end_row_offset_idx, c.start_col_offset_idx, c.end_col_offset_idx)
       for c in t.table_cells[:3]])

at_start = rb"""\begin{tabular}{ccc}
\multicolumn{2}{c}{AB} & C \\
x & y & z \\
\end{tabular}"""
t = conv(at_start).tables[0].data
print(t.num_cols, [[c.text for c in r] for r in t.grid])

Actual

[('2*Model', 0, 1, 0, 1), ('2cBLEU', 0, 1, 1, 2), ('', 0, 1, 2, 3)]
4 [['AB', 'AB', '2cAB', 'C'], ['x', 'y', 'z', '']]

Expected

[('Model', 0, 2, 0, 1), ('BLEU', 0, 1, 1, 3), ...]
3 [['AB', 'AB', 'C'], ['x', 'y', 'z']]

Cause

In docling/backend/latex/utils/table.py, _process_table_macro_node reads the arguments with node.latex_verbatim()[n.pos:]. n.pos is a position in the whole document, but the verbatim string starts at the environment. The argument groups also stay in the node list, so they are read again as cell content.

Proposed fix

Register MacroSpec("multicolumn", "{{{") and MacroSpec("multirow", "[{[{[{") in the LaTeX context, so pylatexenc consumes the arguments. Then read the span and the content from nodeargd and apply the span to the cell that the next & or \\ ends. The hand-written brace parser is no longer needed. Outside a parsed table (for example in a deluxetable or a paragraph), only the text is kept.

I checked nested spans (\multicolumn around \multirow and the reverse), \multicolumn{1}, \multirow with optional arguments, unbraced \multicolumn2c{AB}, tabularx and longtable, \& in cell text, bad or negative spans (fall back to 1) and very large spans (clamped).

This changes the groundtruth of 4 papers in tests/data/latex/groundtruth (1706.03762, 2305.03393, 2412.19437, 2501.00089). Every change is a span that is now applied or a phantom column that is now removed; the JSON diff is large only because the 43-column table shrinks. \cline / \cmidrule arguments leaking into cells is a separate problem (#5387) and is not changed here.

There is a regression test in tests/test_latex/test_tables.py. It fails on main and passes 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, so I checked the groundtruth by regenerating it.)

Prior work: #4404 tried to fix the same bug by correcting the offset. Its author closed it without a review. #3176 (closed) looks like the same symptom.

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-multicolumn-args. 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 one. I'll fix the offset used when the span arguments are re-read, and add tests for both cases in the report.

VanshajPoonia avatar Nov 26 '26 13:11 VanshajPoonia

Thanks for the interest! I already have a fix with tests ready for this one (branch fix/latex-multicolumn-args) and will open the PR as soon as a maintainer gives the go-ahead, so please hold off to avoid duplicate work.

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