From c882900f22e4c4bca77e31abac42bfed406d0b77 Mon Sep 17 00:00:00 2001 From: fbirds5230 Date: Tue, 24 Nov 2026 14:47:25 +0100 Subject: [PATCH] Preserve leading LaTeX escapes when splitting coauthors --- bibtexparser/middlewares/names.py | 4 ++++ tests/middleware_tests/test_names.py | 30 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/bibtexparser/middlewares/names.py b/bibtexparser/middlewares/names.py index 7169bd4..dbe7973 100644 --- a/bibtexparser/middlewares/names.py +++ b/bibtexparser/middlewares/names.py @@ -573,6 +573,10 @@ def split_multiple_persons_names(names: str) -> list[str]: # Escaped character. if char == "\\": + if step == NEXT_WORD: + spans[-1].append(possible_end) + spans.append([pos - 1]) + step = START_WHITESPACE try: next(namesiter) # If we're at the end of the string, then the \ is just a \. diff --git a/tests/middleware_tests/test_names.py b/tests/middleware_tests/test_names.py index 7d809f9..df79cb7 100644 --- a/tests/middleware_tests/test_names.py +++ b/tests/middleware_tests/test_names.py @@ -90,6 +90,36 @@ def test_split_coauthors_consistent_with_bibtex(field_value: str, expected: list assert split_multiple_persons_names(field_value) == expected +@pytest.mark.parametrize( + "names, expected", + [ + (r"John Smith and \'{E}lodie Dupont", ["John Smith", r"\'{E}lodie Dupont"]), + (r"John Smith and \AA berg", ["John Smith", r"\AA berg"]), + ( + r"John Smith and \'{E}lodie Dupont and \AA berg", + ["John Smith", r"\'{E}lodie Dupont", r"\AA berg"], + ), + (r"John Smith a\'nd Jane Doe", [r"John Smith a\'nd Jane Doe"]), + ], +) +def test_split_coauthors_preserves_escapes(names: str, expected: list[str]): + """Escapes belong to names and cannot form part of an `and` delimiter.""" + assert split_multiple_persons_names(names) == expected + + +def test_escaped_coauthors_survive_split_and_merge(): + """Splitting and merging coauthors must preserve their LaTeX accent commands.""" + authors = r"John Smith and \'{E}lodie Dupont and \AA berg" + library = bibtexparser.parse_string( + "@article{example, author={" + authors + "}}", + append_middleware=[SeparateCoAuthors()], + ) + written = bibtexparser.write_string(library, prepend_middleware=[MergeCoAuthors()]) + reparsed = bibtexparser.parse_string(written) + + assert reparsed.entries[0]["author"] == authors + + @pytest.mark.parametrize( "name", [