From 782e236be3fda62a8772c0af590a0c269f3dcae4 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 18 Feb 2022 18:41:23 +0100 Subject: [PATCH 1/3] test fixes --- pandas/io/formats/style_render.py | 2 ++ pandas/io/formats/templates/latex_longtable.tpl | 2 +- pandas/tests/io/formats/style/test_to_latex.py | 7 +++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 2e90074a6bbd3..022e987e4da51 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -838,6 +838,8 @@ def _translate_latex(self, d: dict, clines: str | None) -> None: body.append(row_body_headers + row_body_cells) d["body"] = body + d["n_columns"] = len(d["body"][0]) # used by longtable template for count + # clines are determined from info on index_lengths and hidden_rows and input # to a dict defining which row clines should be added in the template. if clines not in [ diff --git a/pandas/io/formats/templates/latex_longtable.tpl b/pandas/io/formats/templates/latex_longtable.tpl index 580f12aa0df3a..65ff971c66205 100644 --- a/pandas/io/formats/templates/latex_longtable.tpl +++ b/pandas/io/formats/templates/latex_longtable.tpl @@ -59,7 +59,7 @@ {% if midrule is not none %} \{{midrule}} {% endif %} -\multicolumn{% raw %}{{% endraw %}{{column_format|length}}{% raw %}}{% endraw %}{r}{Continued on next page} \\ +\multicolumn{% raw %}{{% endraw %}{{n_columns}}{% raw %}}{% endraw %}{r}{Continued on next page} \\ {% if midrule is not none %} \{{midrule}} {% endif %} diff --git a/pandas/tests/io/formats/style/test_to_latex.py b/pandas/tests/io/formats/style/test_to_latex.py index 145cd832ab270..2cffcb1843fcf 100644 --- a/pandas/tests/io/formats/style/test_to_latex.py +++ b/pandas/tests/io/formats/style/test_to_latex.py @@ -990,3 +990,10 @@ def test_clines_multiindex(clines, expected, env): styler.hide(level=1) result = styler.to_latex(clines=clines, environment=env) assert expected in result + + +def test_col_format_len(styler): + # gh 46037 + result = styler.to_latex(environment="longtable", column_format="lrr{10cm}") + expected = r"\multicolumn{4}{r}{Continued on next page} \\" + assert expected in result From aa633d3782752e05a85e927fcc8418dc23ac2a85 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 18 Feb 2022 18:46:27 +0100 Subject: [PATCH 2/3] whats new --- doc/source/whatsnew/v1.4.2.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.2.rst b/doc/source/whatsnew/v1.4.2.rst index fdac1c385499a..49909c02e2f0b 100644 --- a/doc/source/whatsnew/v1.4.2.rst +++ b/doc/source/whatsnew/v1.4.2.rst @@ -23,7 +23,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ -- +- Fixed "longtable" formatting in :meth:`.Styler.to_latex` when ``column_format`` is given in extended format (:issue:`46037`) - .. --------------------------------------------------------------------------- From 22400038b64c553a8a73eb72ad2e537dd5dcf9ee Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 18 Feb 2022 18:48:27 +0100 Subject: [PATCH 3/3] whats new --- pandas/io/formats/style_render.py | 2 -- pandas/io/formats/templates/latex_longtable.tpl | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 022e987e4da51..2e90074a6bbd3 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -838,8 +838,6 @@ def _translate_latex(self, d: dict, clines: str | None) -> None: body.append(row_body_headers + row_body_cells) d["body"] = body - d["n_columns"] = len(d["body"][0]) # used by longtable template for count - # clines are determined from info on index_lengths and hidden_rows and input # to a dict defining which row clines should be added in the template. if clines not in [ diff --git a/pandas/io/formats/templates/latex_longtable.tpl b/pandas/io/formats/templates/latex_longtable.tpl index 65ff971c66205..b97843eeb918d 100644 --- a/pandas/io/formats/templates/latex_longtable.tpl +++ b/pandas/io/formats/templates/latex_longtable.tpl @@ -59,7 +59,7 @@ {% if midrule is not none %} \{{midrule}} {% endif %} -\multicolumn{% raw %}{{% endraw %}{{n_columns}}{% raw %}}{% endraw %}{r}{Continued on next page} \\ +\multicolumn{% raw %}{{% endraw %}{{body[0]|length}}{% raw %}}{% endraw %}{r}{Continued on next page} \\ {% if midrule is not none %} \{{midrule}} {% endif %}