From 70650f4bc92b5e6c67665a35ca0c455d63dedb68 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Mon, 20 Sep 2021 20:23:27 +0200 Subject: [PATCH 1/3] latex styler bool options --- doc/source/user_guide/options.rst | 3 ++ pandas/core/config_init.py | 21 +++++++++++ pandas/io/formats/style.py | 37 ++++++++++++------- .../tests/io/formats/style/test_to_latex.py | 10 +++++ 4 files changed, 57 insertions(+), 14 deletions(-) diff --git a/doc/source/user_guide/options.rst b/doc/source/user_guide/options.rst index 59319fda8045c..91f87d7257ee6 100644 --- a/doc/source/user_guide/options.rst +++ b/doc/source/user_guide/options.rst @@ -512,6 +512,9 @@ styler.latex.multicol_align r Alignment of headers in a m styler.latex.multirow_align c Alignment of index labels in a merged row due to sparsification. Can be in {"c", "t", "b"}. styler.latex.environment None If given will replace the default ``\\begin{table}`` environment. If "longtable" is specified this will render with a specific "longtable" template with longtable features. +styler.latex.hrules False If set to True will render ``\\toprule``, ``\\midrule``, and ``\bottomrule`` by default. +styler.latex.siunitx False If set to True will render column format and header format compatible with the siunitx package. +styler.latex.convert_css False If set to True will render styles in LaTeX converted from a Styler built with HTML-CSS styles. ======================================= ============ ================================== diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index cf41bcff3d0c8..f4dac18ca56a3 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -821,6 +821,21 @@ def register_converter_cb(key): The specifier for horizontal alignment of sparsified LaTeX multicolumns. """ +styler_hrules = """ +: bool + Whether to add horizontal rules on top and bottom and below the headers. +""" + +styler_siunitx = """ +: bool + Whether to render LaTeX suitable for the siunitx package. +""" + +styler_conv_css = """ +: bool + Whether to render LaTeX styles converted from an HTML-CSS Styler. +""" + styler_environment = """ : str The environment to replace ``\\begin{table}``. If "longtable" is used results @@ -925,6 +940,12 @@ def register_converter_cb(key): validator=is_one_of_factory(["r", "c", "l", "naive-l", "naive-r"]), ) + cf.register_option("latex.siunitx", False, styler_siunitx, validator=is_bool) + + cf.register_option("latex.hrules", False, styler_hrules, validator=is_bool) + + cf.register_option("latex.convert_css", False, styler_conv_css, validator=is_bool) + cf.register_option( "latex.environment", None, diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 2fbd9506e391a..9df2f49ffc6df 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -461,17 +461,17 @@ def to_latex( column_format: str | None = None, position: str | None = None, position_float: str | None = None, - hrules: bool = False, + hrules: bool | None = None, label: str | None = None, caption: str | tuple | None = None, sparse_index: bool | None = None, sparse_columns: bool | None = None, multirow_align: str | None = None, multicol_align: str | None = None, - siunitx: bool = False, + siunitx: bool | None = None, environment: str | None = None, encoding: str | None = None, - convert_css: bool = False, + convert_css: bool | None = None, ): r""" Write Styler to a file, buffer or string in LaTeX format. @@ -481,7 +481,7 @@ def to_latex( Parameters ---------- buf : str, Path, or StringIO-like, optional, default None - Buffer to write to. If ``None``, the output is returned as a string. + Buffer to write to. If `None`, the output is returned as a string. column_format : str, optional The LaTeX column specification placed in location: @@ -502,9 +502,10 @@ def to_latex( \\ Cannot be used if ``environment`` is "longtable". - hrules : bool, default False + hrules : bool Set to `True` to add \\toprule, \\midrule and \\bottomrule from the {booktabs} LaTeX package. + Defaults to ``pandas.options.styler.latex.hrules``, which is `False`. label : str, optional The LaTeX label included as: \\label{