2
2
Module for formatting output data in Latex.
3
3
"""
4
4
from abc import ABC , abstractmethod
5
- from typing import IO , Iterator , List , Optional , Type
5
+ from typing import IO , Iterator , List , Optional , Tuple , Type , Union
6
6
7
7
import numpy as np
8
8
11
11
from pandas .io .formats .format import DataFrameFormatter , TableFormatter
12
12
13
13
14
+ def _split_into_full_short_caption (
15
+ caption : Optional [Union [str , Tuple [str , str ]]]
16
+ ) -> Tuple [str , str ]:
17
+ """Extract full and short captions from caption string/tuple.
18
+
19
+ Parameters
20
+ ----------
21
+ caption : str or tuple, optional
22
+ Either table caption string or tuple (full_caption, short_caption).
23
+ If string is provided, then it is treated as table full caption,
24
+ while short_caption is considered an empty string.
25
+
26
+ Returns
27
+ -------
28
+ full_caption, short_caption : tuple
29
+ Tuple of full_caption, short_caption strings.
30
+ """
31
+ if caption :
32
+ if isinstance (caption , str ):
33
+ full_caption = caption
34
+ short_caption = ""
35
+ else :
36
+ try :
37
+ full_caption , short_caption = caption
38
+ except ValueError as err :
39
+ msg = "caption must be either a string or a tuple of two strings"
40
+ raise ValueError (msg ) from err
41
+ else :
42
+ full_caption = ""
43
+ short_caption = ""
44
+ return full_caption , short_caption
45
+
46
+
14
47
class RowStringConverter (ABC ):
15
48
r"""Converter for dataframe rows into LaTeX strings.
16
49
@@ -275,6 +308,8 @@ class TableBuilderAbstract(ABC):
275
308
Use multirow to enhance MultiIndex rows.
276
309
caption: str, optional
277
310
Table caption.
311
+ short_caption: str, optional
312
+ Table short caption.
278
313
label: str, optional
279
314
LaTeX label.
280
315
position: str, optional
@@ -289,6 +324,7 @@ def __init__(
289
324
multicolumn_format : Optional [str ] = None ,
290
325
multirow : bool = False ,
291
326
caption : Optional [str ] = None ,
327
+ short_caption : Optional [str ] = None ,
292
328
label : Optional [str ] = None ,
293
329
position : Optional [str ] = None ,
294
330
):
@@ -298,6 +334,7 @@ def __init__(
298
334
self .multicolumn_format = multicolumn_format
299
335
self .multirow = multirow
300
336
self .caption = caption
337
+ self .short_caption = short_caption
301
338
self .label = label
302
339
self .position = position
303
340
@@ -384,8 +421,23 @@ def _position_macro(self) -> str:
384
421
385
422
@property
386
423
def _caption_macro (self ) -> str :
387
- r"""Caption macro, extracted from self.caption, like \caption{cap}."""
388
- return f"\\ caption{{{ self .caption } }}" if self .caption else ""
424
+ r"""Caption macro, extracted from self.caption.
425
+
426
+ With short caption:
427
+ \caption[short_caption]{caption_string}.
428
+
429
+ Without short caption:
430
+ \caption{caption_string}.
431
+ """
432
+ if self .caption :
433
+ return "" .join (
434
+ [
435
+ r"\caption" ,
436
+ f"[{ self .short_caption } ]" if self .short_caption else "" ,
437
+ f"{{{ self .caption } }}" ,
438
+ ]
439
+ )
440
+ return ""
389
441
390
442
@property
391
443
def _label_macro (self ) -> str :
@@ -596,15 +648,32 @@ def env_end(self) -> str:
596
648
597
649
598
650
class LatexFormatter (TableFormatter ):
599
- """
651
+ r """
600
652
Used to render a DataFrame to a LaTeX tabular/longtable environment output.
601
653
602
654
Parameters
603
655
----------
604
656
formatter : `DataFrameFormatter`
657
+ longtable : bool, default False
658
+ Use longtable environment.
605
659
column_format : str, default None
606
660
The columns format as specified in `LaTeX table format
607
661
<https://en.wikibooks.org/wiki/LaTeX/Tables>`__ e.g 'rcl' for 3 columns
662
+ multicolumn : bool, default False
663
+ Use \multicolumn to enhance MultiIndex columns.
664
+ multicolumn_format : str, default 'l'
665
+ The alignment for multicolumns, similar to `column_format`
666
+ multirow : bool, default False
667
+ Use \multirow to enhance MultiIndex rows.
668
+ caption : str or tuple, optional
669
+ Tuple (full_caption, short_caption),
670
+ which results in \caption[short_caption]{full_caption};
671
+ if a single string is passed, no short caption will be set.
672
+ label : str, optional
673
+ The LaTeX label to be placed inside ``\label{}`` in the output.
674
+ position : str, optional
675
+ The LaTeX positional argument for tables, to be placed after
676
+ ``\begin{}`` in the output.
608
677
609
678
See Also
610
679
--------
@@ -619,18 +688,18 @@ def __init__(
619
688
multicolumn : bool = False ,
620
689
multicolumn_format : Optional [str ] = None ,
621
690
multirow : bool = False ,
622
- caption : Optional [str ] = None ,
691
+ caption : Optional [Union [ str , Tuple [ str , str ]] ] = None ,
623
692
label : Optional [str ] = None ,
624
693
position : Optional [str ] = None ,
625
694
):
626
695
self .fmt = formatter
627
696
self .frame = self .fmt .frame
628
697
self .longtable = longtable
629
- self .column_format = column_format # type: ignore[assignment]
698
+ self .column_format = column_format
630
699
self .multicolumn = multicolumn
631
700
self .multicolumn_format = multicolumn_format
632
701
self .multirow = multirow
633
- self .caption = caption
702
+ self .caption , self . short_caption = _split_into_full_short_caption ( caption )
634
703
self .label = label
635
704
self .position = position
636
705
@@ -658,6 +727,7 @@ def builder(self) -> TableBuilderAbstract:
658
727
multicolumn_format = self .multicolumn_format ,
659
728
multirow = self .multirow ,
660
729
caption = self .caption ,
730
+ short_caption = self .short_caption ,
661
731
label = self .label ,
662
732
position = self .position ,
663
733
)
@@ -671,7 +741,7 @@ def _select_builder(self) -> Type[TableBuilderAbstract]:
671
741
return TabularBuilder
672
742
673
743
@property
674
- def column_format (self ) -> str :
744
+ def column_format (self ) -> Optional [ str ] :
675
745
"""Column format."""
676
746
return self ._column_format
677
747
0 commit comments