Skip to content

Commit b9b8cad

Browse files
committed
Make Latex Styler use proper key:value css entries
1 parent 10855f6 commit b9b8cad

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

pandas/io/formats/style.py

+20-15
Original file line numberDiff line numberDiff line change
@@ -873,10 +873,10 @@ def to_latex(
873873
.. code-block:: python
874874
875875
set_table_styles([
876-
{"selector": "column_format", "props": f":{column_format};"},
877-
{"selector": "position", "props": f":{position};"},
878-
{"selector": "position_float", "props": f":{position_float};"},
879-
{"selector": "label", "props": f":{{{label.replace(':','§')}}};"}
876+
{"selector": "column_format", "props": f"value:{column_format};"},
877+
{"selector": "position", "props": f"value:{position};"},
878+
{"selector": "position_float", "props": f"value:{position_float};"},
879+
{"selector": "label", "props": f"value:{{{label.replace(':','§')}}};"}
880880
], overwrite=False)
881881
882882
Exception is made for the ``hrules`` argument which, in fact, controls all three
@@ -889,8 +889,8 @@ def to_latex(
889889
.. code-block:: python
890890
891891
set_table_styles([
892-
{'selector': 'toprule', 'props': ':toprule;'},
893-
{'selector': 'bottomrule', 'props': ':hline;'},
892+
{'selector': 'toprule', 'props': 'value:toprule;'},
893+
{'selector': 'bottomrule', 'props': 'value:hline;'},
894894
], overwrite=False)
895895
896896
If other ``commands`` are added to table styles they will be detected, and
@@ -901,7 +901,7 @@ def to_latex(
901901
.. code-block:: python
902902
903903
set_table_styles([
904-
{'selector': 'rowcolors', 'props': ':{1}{pink}{red};'}
904+
{'selector': 'rowcolors', 'props': 'value:{1}{pink}{red};'}
905905
], overwrite=False)
906906
907907
A more comprehensive example using these arguments is as follows:
@@ -1122,7 +1122,7 @@ def to_latex(
11221122
if column_format is not None:
11231123
# add more recent setting to table_styles
11241124
obj.set_table_styles(
1125-
[{"selector": "column_format", "props": f":{column_format}"}],
1125+
[{"selector": "column_format", "props": f"value:{column_format}"}],
11261126
overwrite=False,
11271127
)
11281128
elif "column_format" in table_selectors:
@@ -1142,13 +1142,13 @@ def to_latex(
11421142
("r" if not siunitx else "S") if ci in numeric_cols else "l"
11431143
)
11441144
obj.set_table_styles(
1145-
[{"selector": "column_format", "props": f":{column_format}"}],
1145+
[{"selector": "column_format", "props": f"value:{column_format}"}],
11461146
overwrite=False,
11471147
)
11481148

11491149
if position:
11501150
obj.set_table_styles(
1151-
[{"selector": "position", "props": f":{position}"}],
1151+
[{"selector": "position", "props": f"value:{position}"}],
11521152
overwrite=False,
11531153
)
11541154

@@ -1164,24 +1164,29 @@ def to_latex(
11641164
f"got: '{position_float}'"
11651165
)
11661166
obj.set_table_styles(
1167-
[{"selector": "position_float", "props": f":{position_float}"}],
1167+
[{"selector": "position_float", "props": f"value:{position_float}"}],
11681168
overwrite=False,
11691169
)
11701170

11711171
hrules = get_option("styler.latex.hrules") if hrules is None else hrules
11721172
if hrules:
11731173
obj.set_table_styles(
11741174
[
1175-
{"selector": "toprule", "props": ":toprule"},
1176-
{"selector": "midrule", "props": ":midrule"},
1177-
{"selector": "bottomrule", "props": ":bottomrule"},
1175+
{"selector": "toprule", "props": "value:toprule"},
1176+
{"selector": "midrule", "props": "value:midrule"},
1177+
{"selector": "bottomrule", "props": "value:bottomrule"},
11781178
],
11791179
overwrite=False,
11801180
)
11811181

11821182
if label:
11831183
obj.set_table_styles(
1184-
[{"selector": "label", "props": f":{{{label.replace(':', '§')}}}"}],
1184+
[
1185+
{
1186+
"selector": "label",
1187+
"props": f"value:{{{label.replace(':', '§')}}}",
1188+
}
1189+
],
11851190
overwrite=False,
11861191
)
11871192

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

+21-21
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ def test_tabular_hrules(styler):
6969
def test_tabular_custom_hrules(styler):
7070
styler.set_table_styles(
7171
[
72-
{"selector": "toprule", "props": ":hline"},
73-
{"selector": "bottomrule", "props": ":otherline"},
72+
{"selector": "toprule", "props": "value:hline"},
73+
{"selector": "bottomrule", "props": "value:otherline"},
7474
]
7575
) # no midrule
7676
expected = dedent(
@@ -89,10 +89,10 @@ def test_tabular_custom_hrules(styler):
8989

9090
def test_column_format(styler):
9191
# default setting is already tested in `test_latex_minimal_tabular`
92-
styler.set_table_styles([{"selector": "column_format", "props": ":cccc"}])
92+
styler.set_table_styles([{"selector": "column_format", "props": "value:cccc"}])
9393

9494
assert "\\begin{tabular}{rrrr}" in styler.to_latex(column_format="rrrr")
95-
styler.set_table_styles([{"selector": "column_format", "props": ":r|r|cc"}])
95+
styler.set_table_styles([{"selector": "column_format", "props": "value:r|r|cc"}])
9696
assert "\\begin{tabular}{r|r|cc}" in styler.to_latex()
9797

9898

@@ -112,15 +112,15 @@ def test_siunitx_cols(styler):
112112
def test_position(styler):
113113
assert "\\begin{table}[h!]" in styler.to_latex(position="h!")
114114
assert "\\end{table}" in styler.to_latex(position="h!")
115-
styler.set_table_styles([{"selector": "position", "props": ":b!"}])
115+
styler.set_table_styles([{"selector": "position", "props": "value:b!"}])
116116
assert "\\begin{table}[b!]" in styler.to_latex()
117117
assert "\\end{table}" in styler.to_latex()
118118

119119

120120
@pytest.mark.parametrize("env", [None, "longtable"])
121121
def test_label(styler, env):
122122
assert "\n\\label{text}" in styler.to_latex(label="text", environment=env)
123-
styler.set_table_styles([{"selector": "label", "props": ":{more §text}"}])
123+
styler.set_table_styles([{"selector": "label", "props": "value:{more §text}"}])
124124
assert "\n\\label{more :text}" in styler.to_latex(environment=env)
125125

126126

@@ -159,8 +159,8 @@ def test_kwargs_combinations(
159159
def test_custom_table_styles(styler):
160160
styler.set_table_styles(
161161
[
162-
{"selector": "mycommand", "props": ":{myoptions}"},
163-
{"selector": "mycommand2", "props": ":{myoptions2}"},
162+
{"selector": "mycommand", "props": "value:{myoptions}"},
163+
{"selector": "mycommand2", "props": "value:{myoptions2}"},
164164
]
165165
)
166166
expected = dedent(
@@ -410,14 +410,14 @@ def test_comprehensive(df_ext, environment):
410410
stlr.set_caption("mycap")
411411
stlr.set_table_styles(
412412
[
413-
{"selector": "label", "props": ":{fig§item}"},
414-
{"selector": "position", "props": ":h!"},
415-
{"selector": "position_float", "props": ":centering"},
416-
{"selector": "column_format", "props": ":rlrlr"},
417-
{"selector": "toprule", "props": ":toprule"},
418-
{"selector": "midrule", "props": ":midrule"},
419-
{"selector": "bottomrule", "props": ":bottomrule"},
420-
{"selector": "rowcolors", "props": ":{3}{pink}{}"}, # custom command
413+
{"selector": "label", "props": "value:{fig§item}"},
414+
{"selector": "position", "props": "value:h!"},
415+
{"selector": "position_float", "props": "value:centering"},
416+
{"selector": "column_format", "props": "value:rlrlr"},
417+
{"selector": "toprule", "props": "value:toprule"},
418+
{"selector": "midrule", "props": "value:midrule"},
419+
{"selector": "bottomrule", "props": "value:bottomrule"},
420+
{"selector": "rowcolors", "props": "value:{3}{pink}{}"}, # custom command
421421
]
422422
)
423423
stlr.highlight_max(axis=0, props="textbf:--rwrap;cellcolor:[rgb]{1,1,0.6}--rwrap")
@@ -511,17 +511,17 @@ def test_parse_latex_header_span():
511511
def test_parse_latex_table_wrapping(styler):
512512
styler.set_table_styles(
513513
[
514-
{"selector": "toprule", "props": ":value"},
515-
{"selector": "bottomrule", "props": ":value"},
516-
{"selector": "midrule", "props": ":value"},
517-
{"selector": "column_format", "props": ":value"},
514+
{"selector": "toprule", "props": "value:value"},
515+
{"selector": "bottomrule", "props": "value:value"},
516+
{"selector": "midrule", "props": "value:value"},
517+
{"selector": "column_format", "props": "value:value"},
518518
]
519519
)
520520
assert _parse_latex_table_wrapping(styler.table_styles, styler.caption) is False
521521
assert _parse_latex_table_wrapping(styler.table_styles, "some caption") is True
522522
styler.set_table_styles(
523523
[
524-
{"selector": "not-ignored", "props": ":value"},
524+
{"selector": "not-ignored", "props": "value:value"},
525525
],
526526
overwrite=False,
527527
)

0 commit comments

Comments
 (0)