From 7bb2d171c493ef7b523821c25ec4feb1f3e433be Mon Sep 17 00:00:00 2001 From: JHM Darbyshire <24256554+attack68@users.noreply.github.com> Date: Thu, 6 Jan 2022 00:30:32 +0100 Subject: [PATCH] Backport PR #45138: ENH: add `cline` to `Styler.to_latex` --- doc/source/whatsnew/v1.4.0.rst | 1 + pandas/io/formats/style.py | 18 +++ pandas/io/formats/style_render.py | 60 +++++++-- .../io/formats/templates/latex_longtable.tpl | 4 + pandas/io/formats/templates/latex_table.tpl | 4 + .../tests/io/formats/style/test_to_latex.py | 114 ++++++++++++++++++ 6 files changed, 189 insertions(+), 12 deletions(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 5e74cf57e8718..e23b521d47840 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -105,6 +105,7 @@ There are also some LaTeX specific enhancements: - :meth:`.Styler.to_latex` introduces keyword argument ``environment``, which also allows a specific "longtable" entry through a separate jinja2 template (:issue:`41866`). - Naive sparsification is now possible for LaTeX without the necessity of including the multirow package (:issue:`43369`) + - *cline* support has been added for MultiIndex row sparsification through a keyword argument (:issue:`45138`) .. _whatsnew_140.enhancements.pyarrow_csv_engine: diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 2e74ac93b1ced..29c1e35dbb546 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -494,6 +494,7 @@ def to_latex( position: str | None = None, position_float: str | None = None, hrules: bool | None = None, + clines: str | None = None, label: str | None = None, caption: str | tuple | None = None, sparse_index: bool | None = None, @@ -542,6 +543,22 @@ def to_latex( Defaults to ``pandas.options.styler.latex.hrules``, which is `False`. .. versionchanged:: 1.4.0 + clines : str, optional + Use to control adding \\cline commands for the index labels separation. + Possible values are: + + - `None`: no cline commands are added (default). + - `"all;data"`: a cline is added for every index value extending the + width of the table, including data entries. + - `"all;index"`: as above with lines extending only the width of the + index entries. + - `"skip-last;data"`: a cline is added for each index value except the + last level (which is never sparsified), extending the widtn of the + table. + - `"skip-last;index"`: as above with lines extending only the width of the + index entries. + + .. versionadded:: 1.4.0 label : str, optional The LaTeX label included as: \\label{