From 71f211022f81aa977e0d332c36db447e81b28bba Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 2 Jun 2025 09:20:15 +0200 Subject: [PATCH] auto column detection for minus characters fixed; test modified to catch it --- src/pyladoc/latex.py | 2 +- src/pyladoc/latex_escaping.py | 3 ++- tests/test_rendering_example2_doc.py | 12 ++++++++---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/pyladoc/latex.py b/src/pyladoc/latex.py index bdb4c14..77d6669 100644 --- a/src/pyladoc/latex.py +++ b/src/pyladoc/latex.py @@ -131,7 +131,7 @@ def render_pandas_styler_table(df_style: Styler, caption: str = '', label: str = yield '\\centering\n' # Guess column type - numeric = re.compile('^[<>-\u2212]?\\s*(?:\\d+,?)+(?:\\.\\d+)?(?:\\s\\D.*)?$') + numeric = re.compile('^[<>\\-\u2212]?\\s*(?:\\d+,?)+(?:\\.\\d+)?(?:\\s\\D.*)?$') formats = ['S' if all( (numeric.match(line[ci]['display_value'].strip()) for line in table['body']) ) else 'l' for ci in range(len(table['body'][0])) if table['body'][0][ci]['is_visible']] diff --git a/src/pyladoc/latex_escaping.py b/src/pyladoc/latex_escaping.py index f932561..ce874a3 100644 --- a/src/pyladoc/latex_escaping.py +++ b/src/pyladoc/latex_escaping.py @@ -68,7 +68,8 @@ unicode_to_latex_dict = { '£': r'{\pounds}', '¥': r'{\yen}', '\u00A0': r'~', # Non-breaking space - '\u2007': ' ' # Figure space + '\u2007': ' ', # Figure space + '\u2212': '-' # Unicode minus sign } latex_escape_dict = { diff --git a/tests/test_rendering_example2_doc.py b/tests/test_rendering_example2_doc.py index 8125797..572f01d 100644 --- a/tests/test_rendering_example2_doc.py +++ b/tests/test_rendering_example2_doc.py @@ -71,11 +71,11 @@ def make_document(): mydataset = { 'Row1': ["Line1", "Line2", "Line3", "Line4", "Line5"], 'Row2': [120, '95 km/h', 110, '105 km/h', 130], - 'Row3': ['12 g/km', '> 150 g/km', '110 g/km', '1140 g/km', '13.05 g/km'], + 'Row3': ['12 g/km', '> 150 g/km', '-110 g/km', '1140 g/km', '\u221213.05 g/km'], 'Row4': ['5 stars', '4 stars', '5 stars', '4.5 stars', '5 stars'], 'Row5': [3.5, 7.8, 8.5, 6.9, 4.2], - 'Row6': ['1850 kg', '1500 kg', '1400 kg', '1600 kg', '1700 kg'], - 'Row7': ['600 Nm', '250 Nm', '280 Nm', '320 Nm', '450 Nm'] + 'Row6': ['1850 kg', '150 kg', '140 kg', '1600 kg', '17.55 kg'], + 'Row7': ['600 Nm', '250 Nm', '280,8 Nm', '320 Nm', '450 Nm'] } df = pd.DataFrame(mydataset) @@ -98,9 +98,13 @@ def test_html_render(): def test_latex_render(): doc = make_document() + latex_code = doc.to_latex() + + assert r'\begin{tabular}{lSSSSSS}' in latex_code, "Table format not correct in LaTeX output" + if WRITE_RESULT_FILES: with open('tests/out/test_html_render2.tex', 'w', encoding='utf-8') as f: - f.write(doc.to_latex()) + f.write(latex_code) assert doc.to_pdf('tests/out/test_latex_render2.pdf', font_family='serif') else: