Skip to content

Commit 01fedda

Browse files
committed
add _repr_latex_() method to table.Table
and a corresponding test for it in test_pprint.py resolves issue astropy#3972 closes astropy#3972
1 parent 21f367a commit 01fedda

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎astropy/table/table.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,31 @@ def _repr_html_(self):
15211521
def __repr__(self):
15221522
return self._base_repr_(html=False, max_width=None)
15231523

1524+
def _repr_latex_(self):
1525+
"""
1526+
Format a Table object into the LaTeX format, and return it as a string.
1527+
1528+
Notes
1529+
-----
1530+
Uses `astropy.io.ascii.latex.Latex.write()` function to change
1531+
the Table format to LaTeX. The default LaTeX formatting options defined
1532+
in ``latex.Latex`` are assumed if the ``table.Table.meta`` dictionary
1533+
doesn't already contain a ``latexdict`` keyword.
1534+
See: `astropy.io.ascii.Latex`
1535+
1536+
A new key ``latexdict`` corresponding to ``Latex.latex`` dictionary of
1537+
``astropy.io.ascii.latex`` object is added to ``table.Table.meta``.
1538+
It contains the LaTeX formatting options for the table used.
1539+
"""
1540+
from astropy.io import ascii
1541+
1542+
latexdict = self.meta.get('latexdict', {})
1543+
lTable = ascii.latex.Latex(latexdict=latexdict)
1544+
lines = lTable.write(table=self)
1545+
self.meta['latexdict'] = lTable.latex
1546+
1547+
return '\n'.join(lines)
1548+
15241549
def __str__(self):
15251550
return '\n'.join(self.pformat())
15261551

‎astropy/table/tests/test_pprint.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,3 +917,42 @@ def test_output_globs(self):
917917
with t.pprint_exclude_names.set('a??'):
918918
out = t.pformat_all()
919919
assert out == exp
920+
921+
922+
def test_repr_latex():
923+
"""
924+
Tests the _repr_latex_ method of astropy.table.Table class.
925+
Output should be a latex formatted table returned as a string.
926+
Test for PR related to issue #3972.
927+
"""
928+
a = [1, 2, 3, 5]
929+
b = np.array([4, 5, 6, 5])
930+
c = ['hello', 'konichiwa', 'namaskar', 'guten morgen']
931+
d = [2, 3, 5, 10] * u.m/u.s**2
932+
t = Table([a, b, c, d], names=('val', 'val1', 'greeting', 'speed'))
933+
934+
lTable1 = [r'\begin{table}',
935+
r'\begin{tabular}{cccc}',
936+
r'val & val1 & greeting & speed \\',
937+
r' & & & $\mathrm{m\,s^{-2}}$ \\',
938+
r'1 & 4 & hello & 2.0 \\',
939+
r'2 & 5 & konichiwa & 3.0 \\',
940+
r'3 & 6 & namaskar & 5.0 \\',
941+
r'5 & 5 & guten morgen & 10.0 \\',
942+
r'\end{tabular}',
943+
r'\end{table}']
944+
assert t._repr_latex_().splitlines() == lTable1
945+
946+
lTable2 = [r'\begin{table}',
947+
r'\caption{this is a table}',
948+
r'\begin{tabular}{cccc}',
949+
r'val & val1 & greeting & speed \\',
950+
r' & & & $\mathrm{m\,s^{-2}}$ \\',
951+
r'1 & 4 & hello & 2.0 \\',
952+
r'2 & 5 & konichiwa & 3.0 \\',
953+
r'3 & 6 & namaskar & 5.0 \\',
954+
r'5 & 5 & guten morgen & 10.0 \\',
955+
r'\end{tabular}',
956+
r'\end{table}']
957+
t.meta['latexdict'] = {'caption':'this is a table'}
958+
assert t._repr_latex_().splitlines() == lTable2
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A new feature, the ``_repr_latex_`` method, is now available for ``table.Table``.
2+
This displays a LaTeX formatted table on the screen and can be called using ``Table._repr_latex_()``.

0 commit comments

Comments
 (0)