diff --git a/doc/source/whatsnew/v1.4.2.rst b/doc/source/whatsnew/v1.4.2.rst index 68999b9bba50f..05bc7ff8c96d2 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`) - .. --------------------------------------------------------------------------- diff --git a/pandas/io/formats/templates/latex_longtable.tpl b/pandas/io/formats/templates/latex_longtable.tpl index 580f12aa0df3a..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 %}{{column_format|length}}{% 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 %} 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