Skip to content

ENH: latex styler bool pandas options #43670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 18, 2021
1 change: 1 addition & 0 deletions doc/source/user_guide/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ 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.
======================================= ============ ==================================


Expand Down
7 changes: 7 additions & 0 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,11 @@ def register_converter_cb(key):
rules, e.g. "\|r" will draw a rule on the left side of right aligned merged cells.
"""

styler_hrules = """
: bool
Whether to add horizontal rules on top and bottom and below the headers.
"""

styler_environment = """
: str
The environment to replace ``\\begin{table}``. If "longtable" is used results
Expand Down Expand Up @@ -929,6 +934,8 @@ def register_converter_cb(key):
validator=is_one_of_factory(val_mca),
)

cf.register_option("latex.hrules", False, styler_hrules, validator=is_bool)

cf.register_option(
"latex.environment",
None,
Expand Down
23 changes: 14 additions & 9 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ 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,
Expand All @@ -488,7 +488,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:

Expand All @@ -509,9 +509,12 @@ def to_latex(
\\<position_float>

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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a versionchanged 1.4 here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


.. versionchanged:: 1.4.0
label : str, optional
The LaTeX label included as: \\label{<label>}.
This is used with \\ref{<label>} in the main .tex file.
Expand All @@ -522,16 +525,17 @@ def to_latex(
sparse_index : bool, optional
Whether to sparsify the display of a hierarchical index. Setting to False
will display each explicit level element in a hierarchical key for each row.
Defaults to ``pandas.options.styler.sparse.index`` value.
Defaults to ``pandas.options.styler.sparse.index``, which is `True`.
sparse_columns : bool, optional
Whether to sparsify the display of a hierarchical index. Setting to False
will display each explicit level element in a hierarchical key for each
column. Defaults to ``pandas.options.styler.sparse.columns`` value.
column. Defaults to ``pandas.options.styler.sparse.columns``, which
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one hasn't changed, just clarified docs.

is `True`.
multirow_align : {"c", "t", "b", "naive"}, optional
If sparsifying hierarchical MultiIndexes whether to align text centrally,
at the top or bottom using the multirow package. If not given defaults to
``pandas.options.styler.latex.multirow_align``. If "naive" is given renders
without multirow.
``pandas.options.styler.latex.multirow_align``, which is `"c"`.
If "naive" is given renders without multirow.

.. versionchanged:: 1.4.0
multicol_align : {"r", "c", "l", "naive-l", "naive-r"}, optional
Expand All @@ -550,12 +554,12 @@ def to_latex(
If given, the environment that will replace 'table' in ``\\begin{table}``.
If 'longtable' is specified then a more suitable template is
rendered. If not given defaults to
``pandas.options.styler.latex.environment``.
``pandas.options.styler.latex.environment``, which is `None`.

.. versionadded:: 1.4.0
encoding : str, optional
Character encoding setting. Defaults
to ``pandas.options.styler.render.encoding`` value of "utf-8".
to ``pandas.options.styler.render.encoding``, which is "utf-8".
convert_css : bool, default False
Convert simple cell-styles from CSS to LaTeX format. Any CSS not found in
conversion table is dropped. A style can be forced by adding option
Expand Down Expand Up @@ -844,6 +848,7 @@ def to_latex(
overwrite=False,
)

hrules = get_option("styler.latex.hrules") if hrules is None else hrules
if hrules:
obj.set_table_styles(
[
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/io/formats/style/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,15 @@ def test_repr_option(styler):
assert styler._repr_html_() is None


@pytest.mark.parametrize("option", ["hrules"])
def test_bool_options(styler, option):
with option_context(f"styler.latex.{option}", False):
latex_false = styler.to_latex()
with option_context(f"styler.latex.{option}", True):
latex_true = styler.to_latex()
assert latex_false != latex_true # options are reactive under to_latex(*no_args)


def test_siunitx_basic_headers(styler):
assert "{} & {A} & {B} & {C} \\\\" in styler.to_latex(siunitx=True)
assert " & A & B & C \\\\" in styler.to_latex() # default siunitx=False
Expand Down