diff --git a/doc/source/_static/style/latex_1.png b/doc/source/_static/style/latex_1.png new file mode 100644 index 0000000000000..8b901878a0ec9 Binary files /dev/null and b/doc/source/_static/style/latex_1.png differ diff --git a/doc/source/_static/style/latex_2.png b/doc/source/_static/style/latex_2.png new file mode 100644 index 0000000000000..7d6baa681575e Binary files /dev/null and b/doc/source/_static/style/latex_2.png differ diff --git a/doc/source/reference/style.rst b/doc/source/reference/style.rst index 8c443f3ae9bb6..6a075ad702bde 100644 --- a/doc/source/reference/style.rst +++ b/doc/source/reference/style.rst @@ -24,6 +24,7 @@ Styler properties Styler.env Styler.template_html + Styler.template_latex Styler.loader Style application @@ -66,3 +67,4 @@ Style export and import Styler.export Styler.use Styler.to_excel + Styler.to_latex diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 4abc190bae70f..a48074789f727 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -141,6 +141,9 @@ properly format HTML and eliminate some inconsistencies (:issue:`39942` :issue:` :class:`.Styler` has also been compatible with non-unique index or columns, at least for as many features as are fully compatible, others made only partially compatible (:issue:`41269`). One also has greater control of the display through separate sparsification of the index or columns, using the new 'styler' options context (:issue:`41142`). +We have added an extension to allow LaTeX styling as an alternative to CSS styling and a method :meth:`.Styler.to_latex` +which renders the necessary LaTeX format including built-up styles. + Documentation has also seen major revisions in light of new features (:issue:`39720` :issue:`39317` :issue:`40493`) .. _whatsnew_130.dataframe_honors_copy_with_dict: diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 56e34d9500f31..977a3a24f0844 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -21,6 +21,7 @@ from pandas._typing import ( Axis, + FilePathOrBuffer, FrameOrSeries, FrameOrSeriesUnion, IndexLabel, @@ -30,6 +31,7 @@ from pandas.util._decorators import doc import pandas as pd +from pandas import RangeIndex from pandas.api.types import is_list_like from pandas.core import generic import pandas.core.common as com @@ -39,6 +41,8 @@ ) from pandas.core.generic import NDFrame +from pandas.io.formats.format import save_to_buffer + jinja2 = import_optional_dependency("jinja2", extra="DataFrame.style requires jinja2.") from pandas.io.formats.style_render import ( @@ -403,6 +407,338 @@ def to_excel( engine=engine, ) + def to_latex( + self, + buf: FilePathOrBuffer[str] | None = None, + *, + column_format: str | None = None, + position: str | None = None, + position_float: str | None = None, + hrules: bool = False, + label: str | None = None, + caption: str | None = None, + sparse_index: bool | None = None, + sparse_columns: bool | None = None, + multirow_align: str = "c", + multicol_align: str = "r", + siunitx: bool = False, + encoding: str | None = None, + ): + r""" + Write Styler to a file, buffer or string in LaTeX format. + + .. versionadded:: 1.3.0 + + Parameters + ---------- + buf : str, Path, or StringIO-like, optional, default None + Buffer to write to. If ``None``, the output is returned as a string. + column_format : str, optional + The LaTeX column specification placed in location: + + \\begin{tabular}{} + + Defaults to 'l' for index and + non-numeric data columns, and, for numeric data columns, + to 'r' by default, or 'S' if ``siunitx`` is ``True``. + position : str, optional + The LaTeX positional argument (e.g. 'h!') for tables, placed in location: + + \\begin{table}[] + position_float : {"centering", "raggedleft", "raggedright"}, optional + The LaTeX float command placed in location: + + \\begin{table}[] + + \\ + hrules : bool, default False + Set to `True` to add \\toprule, \\midrule and \\bottomrule from the + {booktabs} LaTeX package. + label : str, optional + The LaTeX label included as: \\label{