From 3dfaed5041b56fd55d2500e415d91fe2db2212b4 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Tue, 27 Oct 2026 20:05:37 +0000 Subject: [PATCH] Handle missing verbatim arguments and empty LaTeX matrices --- .../external/pylatexenc/latex2text/__init__.py | 2 +- .../external/pylatexenc/macrospec/_argparsers.py | 10 +++++----- tests/utilities/test_latex_regressions.py | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/great_ai/utilities/external/pylatexenc/latex2text/__init__.py b/great_ai/utilities/external/pylatexenc/latex2text/__init__.py index 54360d1..94117d3 100644 --- a/great_ai/utilities/external/pylatexenc/latex2text/__init__.py +++ b/great_ai/utilities/external/pylatexenc/latex2text/__init__.py @@ -386,7 +386,7 @@ def fmt_matrix_environment_node(node, l2tobj): state.new_row() # finish the last row # now format the contents as array -- - max_char_width = max((len(x) for row in state.matrix_rows for x in row)) + max_char_width = max((len(x) for row in state.matrix_rows for x in row), default=0) matrix_contents = "; ".join( ( " ".join((x.rjust(max_char_width, " ") for x in row)) diff --git a/great_ai/utilities/external/pylatexenc/macrospec/_argparsers.py b/great_ai/utilities/external/pylatexenc/macrospec/_argparsers.py index abadc40..96bcd59 100644 --- a/great_ai/utilities/external/pylatexenc/macrospec/_argparsers.py +++ b/great_ai/utilities/external/pylatexenc/macrospec/_argparsers.py @@ -460,12 +460,12 @@ class VerbatimArgsParser(MacroStandardArgsParser): if self.verbatim_arg_type == "verb-macro": # read the next nonwhitespace char. This is the delimiter of the # argument - while w.s[pos].isspace(): + while pos < len(w.s) and w.s[pos].isspace(): pos += 1 - if pos >= len(w.s): - raise latexwalker.LatexWalkerParseError( - s=w.s, pos=pos, msg=r"Missing argument to \verb command" - ) + if pos >= len(w.s): + raise latexwalker.LatexWalkerParseError( + s=w.s, pos=pos, msg=r"Missing argument to \verb command" + ) verbdelimchar = w.s[pos] beginpos = pos + 1 endpos = w.s.find(verbdelimchar, beginpos) diff --git a/tests/utilities/test_latex_regressions.py b/tests/utilities/test_latex_regressions.py index 3dfe0ca..c3177b3 100644 --- a/tests/utilities/test_latex_regressions.py +++ b/tests/utilities/test_latex_regressions.py @@ -1,6 +1,8 @@ from pathlib import Path +import pytest from great_ai.utilities.external.pylatexenc.latex2text import LatexNodes2Text +from great_ai.utilities.external.pylatexenc.latexwalker import LatexWalkerParseError def test_strict_input_checks_path_ancestry(tmp_path: Path) -> None: @@ -16,3 +18,15 @@ def test_strict_input_checks_path_ancestry(tmp_path: Path) -> None: assert converter.latex_to_text(r"\input{inside}") == "inside" assert converter.latex_to_text(r"\input{../allowed_neighbor/outside}") == "" assert converter.latex_to_text(r"\input{link}") == "" + + +@pytest.mark.parametrize("text", [r"\verb", "\\verb "]) +def test_missing_verbatim_arguments_use_normal_parse_errors(text: str) -> None: + converter = LatexNodes2Text() + with pytest.raises(LatexWalkerParseError): + converter.latex_to_text(text, tolerant_parsing=False) + assert converter.latex_to_text(text, tolerant_parsing=True) == "" + + +def test_empty_matrix_has_a_text_representation() -> None: + assert LatexNodes2Text().latex_to_text(r"\begin{pmatrix}\end{pmatrix}") == "[ ]"