Skip to content

Commit 0bad85d

Browse files
authored
ENH: latex styler bool pandas options (#43670)
1 parent 8613007 commit 0bad85d

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

doc/source/user_guide/options.rst

+1
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ styler.latex.multicol_align r Alignment of headers in a m
512512
styler.latex.multirow_align c Alignment of index labels in a merged row due to sparsification. Can be in {"c", "t", "b"}.
513513
styler.latex.environment None If given will replace the default ``\\begin{table}`` environment. If "longtable" is specified
514514
this will render with a specific "longtable" template with longtable features.
515+
styler.latex.hrules False If set to True will render ``\\toprule``, ``\\midrule``, and ``\bottomrule`` by default.
515516
======================================= ============ ==================================
516517

517518

pandas/core/config_init.py

+7
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,11 @@ def register_converter_cb(key):
823823
rules, e.g. "\|r" will draw a rule on the left side of right aligned merged cells.
824824
"""
825825

826+
styler_hrules = """
827+
: bool
828+
Whether to add horizontal rules on top and bottom and below the headers.
829+
"""
830+
826831
styler_environment = """
827832
: str
828833
The environment to replace ``\\begin{table}``. If "longtable" is used results
@@ -929,6 +934,8 @@ def register_converter_cb(key):
929934
validator=is_one_of_factory(val_mca),
930935
)
931936

937+
cf.register_option("latex.hrules", False, styler_hrules, validator=is_bool)
938+
932939
cf.register_option(
933940
"latex.environment",
934941
None,

pandas/io/formats/style.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def to_latex(
468468
column_format: str | None = None,
469469
position: str | None = None,
470470
position_float: str | None = None,
471-
hrules: bool = False,
471+
hrules: bool | None = None,
472472
label: str | None = None,
473473
caption: str | tuple | None = None,
474474
sparse_index: bool | None = None,
@@ -488,7 +488,7 @@ def to_latex(
488488
Parameters
489489
----------
490490
buf : str, Path, or StringIO-like, optional, default None
491-
Buffer to write to. If ``None``, the output is returned as a string.
491+
Buffer to write to. If `None`, the output is returned as a string.
492492
column_format : str, optional
493493
The LaTeX column specification placed in location:
494494
@@ -509,9 +509,12 @@ def to_latex(
509509
\\<position_float>
510510
511511
Cannot be used if ``environment`` is "longtable".
512-
hrules : bool, default False
512+
hrules : bool
513513
Set to `True` to add \\toprule, \\midrule and \\bottomrule from the
514514
{booktabs} LaTeX package.
515+
Defaults to ``pandas.options.styler.latex.hrules``, which is `False`.
516+
517+
.. versionchanged:: 1.4.0
515518
label : str, optional
516519
The LaTeX label included as: \\label{<label>}.
517520
This is used with \\ref{<label>} in the main .tex file.
@@ -522,16 +525,17 @@ def to_latex(
522525
sparse_index : bool, optional
523526
Whether to sparsify the display of a hierarchical index. Setting to False
524527
will display each explicit level element in a hierarchical key for each row.
525-
Defaults to ``pandas.options.styler.sparse.index`` value.
528+
Defaults to ``pandas.options.styler.sparse.index``, which is `True`.
526529
sparse_columns : bool, optional
527530
Whether to sparsify the display of a hierarchical index. Setting to False
528531
will display each explicit level element in a hierarchical key for each
529-
column. Defaults to ``pandas.options.styler.sparse.columns`` value.
532+
column. Defaults to ``pandas.options.styler.sparse.columns``, which
533+
is `True`.
530534
multirow_align : {"c", "t", "b", "naive"}, optional
531535
If sparsifying hierarchical MultiIndexes whether to align text centrally,
532536
at the top or bottom using the multirow package. If not given defaults to
533-
``pandas.options.styler.latex.multirow_align``. If "naive" is given renders
534-
without multirow.
537+
``pandas.options.styler.latex.multirow_align``, which is `"c"`.
538+
If "naive" is given renders without multirow.
535539
536540
.. versionchanged:: 1.4.0
537541
multicol_align : {"r", "c", "l", "naive-l", "naive-r"}, optional
@@ -550,12 +554,12 @@ def to_latex(
550554
If given, the environment that will replace 'table' in ``\\begin{table}``.
551555
If 'longtable' is specified then a more suitable template is
552556
rendered. If not given defaults to
553-
``pandas.options.styler.latex.environment``.
557+
``pandas.options.styler.latex.environment``, which is `None`.
554558
555559
.. versionadded:: 1.4.0
556560
encoding : str, optional
557561
Character encoding setting. Defaults
558-
to ``pandas.options.styler.render.encoding`` value of "utf-8".
562+
to ``pandas.options.styler.render.encoding``, which is "utf-8".
559563
convert_css : bool, default False
560564
Convert simple cell-styles from CSS to LaTeX format. Any CSS not found in
561565
conversion table is dropped. A style can be forced by adding option
@@ -844,6 +848,7 @@ def to_latex(
844848
overwrite=False,
845849
)
846850

851+
hrules = get_option("styler.latex.hrules") if hrules is None else hrules
847852
if hrules:
848853
obj.set_table_styles(
849854
[

pandas/tests/io/formats/style/test_to_latex.py

+9
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,15 @@ def test_repr_option(styler):
779779
assert styler._repr_html_() is None
780780

781781

782+
@pytest.mark.parametrize("option", ["hrules"])
783+
def test_bool_options(styler, option):
784+
with option_context(f"styler.latex.{option}", False):
785+
latex_false = styler.to_latex()
786+
with option_context(f"styler.latex.{option}", True):
787+
latex_true = styler.to_latex()
788+
assert latex_false != latex_true # options are reactive under to_latex(*no_args)
789+
790+
782791
def test_siunitx_basic_headers(styler):
783792
assert "{} & {A} & {B} & {C} \\\\" in styler.to_latex(siunitx=True)
784793
assert " & A & B & C \\\\" in styler.to_latex() # default siunitx=False

0 commit comments

Comments
 (0)