From 067404829591314b1d6743e9a8e73548e7931c5d Mon Sep 17 00:00:00 2001 From: Philippe Faist Date: Thu, 10 Sep 2020 00:16:19 +0200 Subject: [PATCH] bugfix for latexencode braces-after-macro mode, fixes #44 --- pylatexenc/latexencode/__init__.py | 2 +- test/test_latexencode.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pylatexenc/latexencode/__init__.py b/pylatexenc/latexencode/__init__.py index 94e2ee83..3ec3a96a 100644 --- a/pylatexenc/latexencode/__init__.py +++ b/pylatexenc/latexencode/__init__.py @@ -639,7 +639,7 @@ def _apply_protection_braces_all(self, repl): return '{' + repl + '}' def _apply_protection_braces_after_macro(self, repl): k = repl.rfind('\\') - if k > 0 and repl[k+1:].isalpha(): + if k >= 0 and repl[k+1:].isalpha(): # has dangling named macro, apply protection. return repl + '{}' return repl diff --git a/test/test_latexencode.py b/test/test_latexencode.py index 22d07927..c999eb53 100644 --- a/test/test_latexencode.py +++ b/test/test_latexencode.py @@ -49,6 +49,11 @@ def test_basic_2(self): input = "\"\N{LATIN CAPITAL LETTER A WITH GRAVE} votre sant\N{LATIN SMALL LETTER E WITH ACUTE}!\" s'exclama le ma\N{LATIN SMALL LETTER I WITH CIRCUMFLEX}tre de maison \N{LATIN SMALL LETTER A WITH GRAVE} 100%." self.assertEqual(u.unicode_to_latex(input), "''\\`A votre sant\\'e!'' s'exclama le ma\\^\\i{}tre de maison \\`a 100\\%.") + def test_basic_2a(self): + # Issue #44 + u = UnicodeToLatexEncoder(replacement_latex_protection='braces-after-macro') + input = "Jabłoński, François, ⟨.⟩, ~" + self.assertEqual(u.unicode_to_latex(input), "Jab\\l{}o\\'nski, Fran\\c{c}ois, \\ensuremath{\\langle}.\\ensuremath{\\rangle}, \\textasciitilde{}") def test_basic_2b(self): u = UnicodeToLatexEncoder(replacement_latex_protection='none')