@@ -33,7 +33,7 @@ def test_minimal_latex_tabular(styler):
33
33
expected = dedent (
34
34
"""\
35
35
\\ begin{tabular}{lrrl}
36
- {} & {A} & {B} & {C} \\ \\
36
+ & A & B & C \\ \\
37
37
0 & 0 & -0.61 & ab \\ \\
38
38
1 & 1 & -1.22 & cd \\ \\
39
39
\\ end{tabular}
@@ -47,7 +47,7 @@ def test_tabular_hrules(styler):
47
47
"""\
48
48
\\ begin{tabular}{lrrl}
49
49
\\ toprule
50
- {} & {A} & {B} & {C} \\ \\
50
+ & A & B & C \\ \\
51
51
\\ midrule
52
52
0 & 0 & -0.61 & ab \\ \\
53
53
1 & 1 & -1.22 & cd \\ \\
@@ -69,7 +69,7 @@ def test_tabular_custom_hrules(styler):
69
69
"""\
70
70
\\ begin{tabular}{lrrl}
71
71
\\ hline
72
- {} & {A} & {B} & {C} \\ \\
72
+ & A & B & C \\ \\
73
73
0 & 0 & -0.61 & ab \\ \\
74
74
1 & 1 & -1.22 & cd \\ \\
75
75
\\ otherline
@@ -170,7 +170,7 @@ def test_cell_styling(styler):
170
170
expected = dedent (
171
171
"""\
172
172
\\ begin{tabular}{lrrl}
173
- {} & {A} & {B} & {C} \\ \\
173
+ & A & B & C \\ \\
174
174
0 & 0 & \\ itshape {\\ Huge -0.61} & ab \\ \\
175
175
1 & \\ itshape {\\ Huge 1} & -1.22 & \\ itshape {\\ Huge cd} \\ \\
176
176
\\ end{tabular}
@@ -185,8 +185,8 @@ def test_multiindex_columns(df):
185
185
expected = dedent (
186
186
"""\
187
187
\\ begin{tabular}{lrrl}
188
- {} & \\ multicolumn{2}{r}{A} & {B} \\ \\
189
- {} & {a} & {b} & {c} \\ \\
188
+ & \\ multicolumn{2}{r}{A} & B \\ \\
189
+ & a & b & c \\ \\
190
190
0 & 0 & -0.61 & ab \\ \\
191
191
1 & 1 & -1.22 & cd \\ \\
192
192
\\ end{tabular}
@@ -199,8 +199,8 @@ def test_multiindex_columns(df):
199
199
expected = dedent (
200
200
"""\
201
201
\\ begin{tabular}{lrrl}
202
- {} & {A} & {A} & {B} \\ \\
203
- {} & {a} & {b} & {c} \\ \\
202
+ & A & A & B \\ \\
203
+ & a & b & c \\ \\
204
204
0 & 0 & -0.61 & ab \\ \\
205
205
1 & 1 & -1.22 & cd \\ \\
206
206
\\ end{tabular}
@@ -218,7 +218,7 @@ def test_multiindex_row(df):
218
218
expected = dedent (
219
219
"""\
220
220
\\ begin{tabular}{llrrl}
221
- {} & {} & {A} & {B} & {C} \\ \\
221
+ & & A & B & C \\ \\
222
222
\\ multirow[c]{2}{*}{A} & a & 0 & -0.61 & ab \\ \\
223
223
& b & 1 & -1.22 & cd \\ \\
224
224
B & c & 2 & -2.22 & de \\ \\
@@ -232,7 +232,7 @@ def test_multiindex_row(df):
232
232
expected = dedent (
233
233
"""\
234
234
\\ begin{tabular}{llrrl}
235
- {} & {} & {A} & {B} & {C} \\ \\
235
+ & & A & B & C \\ \\
236
236
A & a & 0 & -0.61 & ab \\ \\
237
237
A & b & 1 & -1.22 & cd \\ \\
238
238
B & c & 2 & -2.22 & de \\ \\
@@ -251,8 +251,8 @@ def test_multiindex_row_and_col(df):
251
251
expected = dedent (
252
252
"""\
253
253
\\ begin{tabular}{llrrl}
254
- {} & {} & \\ multicolumn{2}{l}{Z} & {Y} \\ \\
255
- {} & {} & {a} & {b} & {c} \\ \\
254
+ & & \\ multicolumn{2}{l}{Z} & Y \\ \\
255
+ & & a & b & c \\ \\
256
256
\\ multirow[b]{2}{*}{A} & a & 0 & -0.61 & ab \\ \\
257
257
& b & 1 & -1.22 & cd \\ \\
258
258
B & c & 2 & -2.22 & de \\ \\
@@ -266,8 +266,8 @@ def test_multiindex_row_and_col(df):
266
266
expected = dedent (
267
267
"""\
268
268
\\ begin{tabular}{llrrl}
269
- {} & {} & {Z} & {Z} & {Y} \\ \\
270
- {} & {} & {a} & {b} & {c} \\ \\
269
+ & & Z & Z & Y \\ \\
270
+ & & a & b & c \\ \\
271
271
A & a & 0 & -0.61 & ab \\ \\
272
272
A & b & 1 & -1.22 & cd \\ \\
273
273
B & c & 2 & -2.22 & de \\ \\
@@ -287,15 +287,15 @@ def test_multi_options(df):
287
287
288
288
expected = dedent (
289
289
"""\
290
- {} & {} & \\ multicolumn{2}{r}{Z} & {Y} \\ \\
291
- {} & {} & {a} & {b} & {c} \\ \\
290
+ & & \\ multicolumn{2}{r}{Z} & Y \\ \\
291
+ & & a & b & c \\ \\
292
292
\\ multirow[c]{2}{*}{A} & a & 0 & -0.61 & ab \\ \\
293
293
"""
294
294
)
295
295
assert expected in styler .to_latex ()
296
296
297
297
with option_context ("styler.latex.multicol_align" , "l" ):
298
- assert "{} & {} & \\ multicolumn{2}{l}{Z} & {Y} \\ \\ " in styler .to_latex ()
298
+ assert " & & \\ multicolumn{2}{l}{Z} & Y \\ \\ " in styler .to_latex ()
299
299
300
300
with option_context ("styler.latex.multirow_align" , "b" ):
301
301
assert "\\ multirow[b]{2}{*}{A} & a & 0 & -0.61 & ab \\ \\ " in styler .to_latex ()
@@ -342,7 +342,7 @@ def test_hidden_index(styler):
342
342
expected = dedent (
343
343
"""\
344
344
\\ begin{tabular}{rrl}
345
- {A} & {B} & {C} \\ \\
345
+ A & B & C \\ \\
346
346
0 & -0.61 & ab \\ \\
347
347
1 & -1.22 & cd \\ \\
348
348
\\ end{tabular}
@@ -385,8 +385,8 @@ def test_comprehensive(df, environment):
385
385
\\ rowcolors{3}{pink}{}
386
386
\\ begin{tabular}{rlrlr}
387
387
\\ toprule
388
- {} & {} & \\ multicolumn{2}{r}{Z} & {Y} \\ \\
389
- {} & {} & {a} & {b} & {c} \\ \\
388
+ & & \\ multicolumn{2}{r}{Z} & Y \\ \\
389
+ & & a & b & c \\ \\
390
390
\\ midrule
391
391
\\ multirow[c]{2}{*}{A} & a & 0 & \\ textbf{\\ cellcolor[rgb]{1,1,0.6}{-0.61}} & ab \\ \\
392
392
& b & 1 & -1.22 & cd \\ \\
@@ -580,12 +580,12 @@ def test_longtable_comprehensive(styler):
580
580
\\ begin{longtable}{lrrl}
581
581
\\ caption[short]{full} \\ label{fig:A} \\ \\
582
582
\\ toprule
583
- {} & {A} & {B} & {C} \\ \\
583
+ & A & B & C \\ \\
584
584
\\ midrule
585
585
\\ endfirsthead
586
586
\\ caption[]{full} \\ \\
587
587
\\ toprule
588
- {} & {A} & {B} & {C} \\ \\
588
+ & A & B & C \\ \\
589
589
\\ midrule
590
590
\\ endhead
591
591
\\ midrule
@@ -607,9 +607,9 @@ def test_longtable_minimal(styler):
607
607
expected = dedent (
608
608
"""\
609
609
\\ begin{longtable}{lrrl}
610
- {} & {A} & {B} & {C} \\ \\
610
+ & A & B & C \\ \\
611
611
\\ endfirsthead
612
- {} & {A} & {B} & {C} \\ \\
612
+ & A & B & C \\ \\
613
613
\\ endhead
614
614
\\ multicolumn{4}{r}{Continued on next page} \\ \\
615
615
\\ endfoot
@@ -623,27 +623,34 @@ def test_longtable_minimal(styler):
623
623
624
624
625
625
@pytest .mark .parametrize (
626
- "sparse, exp" ,
626
+ "sparse, exp, siunitx " ,
627
627
[
628
- (True , "{} & \\ multicolumn{2}{r}{A} & {B}" ),
629
- (False , "{} & {A} & {A} & {B}" ),
628
+ (True , "{} & \\ multicolumn{2}{r}{A} & {B}" , True ),
629
+ (False , "{} & {A} & {A} & {B}" , True ),
630
+ (True , " & \\ multicolumn{2}{r}{A} & B" , False ),
631
+ (False , " & A & A & B" , False ),
630
632
],
631
633
)
632
- def test_longtable_multiindex_columns (df , sparse , exp ):
634
+ def test_longtable_multiindex_columns (df , sparse , exp , siunitx ):
633
635
cidx = MultiIndex .from_tuples ([("A" , "a" ), ("A" , "b" ), ("B" , "c" )])
634
636
df .columns = cidx
637
+ with_si = "{} & {a} & {b} & {c} \\ \\ "
638
+ without_si = " & a & b & c \\ \\ "
635
639
expected = dedent (
636
640
f"""\
637
- \\ begin{{longtable}}{{lrrl }}
641
+ \\ begin{{longtable}}{{l { "SS" if siunitx else "rr" } l }}
638
642
{ exp } \\ \\
639
- {{}} & {{a}} & {{b}} & {{c}} \\ \\
643
+ { with_si if siunitx else without_si }
640
644
\\ endfirsthead
641
645
{ exp } \\ \\
642
- {{}} & {{a}} & {{b}} & {{c}} \\ \\
646
+ { with_si if siunitx else without_si }
643
647
\\ endhead
644
648
"""
645
649
)
646
- assert expected in df .style .to_latex (environment = "longtable" , sparse_columns = sparse )
650
+ result = df .style .to_latex (
651
+ environment = "longtable" , sparse_columns = sparse , siunitx = siunitx
652
+ )
653
+ assert expected in result
647
654
648
655
649
656
@pytest .mark .parametrize (
@@ -661,7 +668,7 @@ def test_longtable_caption_label(styler, caption, cap_exp, label, lab_exp):
661
668
expected = dedent (
662
669
f"""\
663
670
{ cap_exp1 } { lab_exp } \\ \\
664
- {{}} & {{A}} & {{B}} & {{C}} \\ \\
671
+ & A & B & C \\ \\
665
672
\\ endfirsthead
666
673
{ cap_exp2 } \\ \\
667
674
"""
@@ -672,8 +679,15 @@ def test_longtable_caption_label(styler, caption, cap_exp, label, lab_exp):
672
679
673
680
674
681
@pytest .mark .parametrize ("index" , [True , False ])
675
- @pytest .mark .parametrize ("columns" , [True , False ])
676
- def test_apply_map_header_render_mi (df , index , columns ):
682
+ @pytest .mark .parametrize (
683
+ "columns, siunitx" ,
684
+ [
685
+ (True , True ),
686
+ (True , False ),
687
+ (False , False ),
688
+ ],
689
+ )
690
+ def test_apply_map_header_render_mi (df , index , columns , siunitx ):
677
691
cidx = MultiIndex .from_tuples ([("Z" , "a" ), ("Z" , "b" ), ("Y" , "c" )])
678
692
ridx = MultiIndex .from_tuples ([("A" , "a" ), ("A" , "b" ), ("B" , "c" )])
679
693
df .loc [2 , :] = [2 , - 2.22 , "de" ]
@@ -688,7 +702,7 @@ def test_apply_map_header_render_mi(df, index, columns):
688
702
if columns :
689
703
styler .applymap_index (func , axis = "columns" )
690
704
691
- result = styler .to_latex ()
705
+ result = styler .to_latex (siunitx = siunitx )
692
706
693
707
expected_index = dedent (
694
708
"""\
@@ -699,13 +713,17 @@ def test_apply_map_header_render_mi(df, index, columns):
699
713
)
700
714
assert (expected_index in result ) is index
701
715
702
- expected_columns = dedent (
716
+ exp_cols_si = dedent (
703
717
"""\
704
718
{} & {} & \\ multicolumn{2}{r}{\\ bfseries{Z}} & {Y} \\ \\
705
719
{} & {} & {a} & {b} & {\\ bfseries{c}} \\ \\
706
720
"""
707
721
)
708
- assert (expected_columns in result ) is columns
722
+ exp_cols_no_si = """\
723
+ & & \\ multicolumn{2}{r}{\\ bfseries{Z}} & Y \\ \\
724
+ & & a & b & \\ bfseries{c} \\ \\
725
+ """
726
+ assert ((exp_cols_si if siunitx else exp_cols_no_si ) in result ) is columns
709
727
710
728
711
729
def test_repr_option (styler ):
@@ -714,3 +732,8 @@ def test_repr_option(styler):
714
732
with option_context ("styler.render.repr" , "latex" ):
715
733
assert "\\ begin{tabular}" in styler ._repr_latex_ ()[:15 ]
716
734
assert styler ._repr_html_ () is None
735
+
736
+
737
+ def test_siunitx_basic_headers (styler ):
738
+ assert "{} & {A} & {B} & {C} \\ \\ " in styler .to_latex (siunitx = True )
739
+ assert " & A & B & C \\ \\ " in styler .to_latex () # default siunitx=False
0 commit comments