Skip to content

Commit df9e74c

Browse files
Suyog7130nstarman
authored andcommitted
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 a262c5d commit df9e74c

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
@@ -1523,6 +1523,31 @@ def _repr_html_(self):
15231523
def __repr__(self):
15241524
return self._base_repr_(html=False, max_width=None)
15251525

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

‎astropy/table/tests/test_pprint.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,3 +912,42 @@ def test_output_globs(self):
912912
with t.pprint_exclude_names.set('a??'):
913913
out = t.pformat_all()
914914
assert out == exp
915+
916+
917+
def test_repr_latex():
918+
"""
919+
Tests the _repr_latex_ method of astropy.table.Table class.
920+
Output should be a latex formatted table returned as a string.
921+
Test for PR related to issue #3972.
922+
"""
923+
a = [1, 2, 3, 5]
924+
b = np.array([4, 5, 6, 5])
925+
c = ['hello', 'konichiwa', 'namaskar', 'guten morgen']
926+
d = [2, 3, 5, 10] * u.m/u.s**2
927+
t = Table([a, b, c, d], names=('val', 'val1', 'greeting', 'speed'))
928+
929+
lTable1 = [r'\begin{table}',
930+
r'\begin{tabular}{cccc}',
931+
r'val & val1 & greeting & speed \\',
932+
r' & & & $\mathrm{m\,s^{-2}}$ \\',
933+
r'1 & 4 & hello & 2.0 \\',
934+
r'2 & 5 & konichiwa & 3.0 \\',
935+
r'3 & 6 & namaskar & 5.0 \\',
936+
r'5 & 5 & guten morgen & 10.0 \\',
937+
r'\end{tabular}',
938+
r'\end{table}']
939+
assert t._repr_latex_().splitlines() == lTable1
940+
941+
lTable2 = [r'\begin{table}',
942+
r'\caption{this is a table}',
943+
r'\begin{tabular}{cccc}',
944+
r'val & val1 & greeting & speed \\',
945+
r' & & & $\mathrm{m\,s^{-2}}$ \\',
946+
r'1 & 4 & hello & 2.0 \\',
947+
r'2 & 5 & konichiwa & 3.0 \\',
948+
r'3 & 6 & namaskar & 5.0 \\',
949+
r'5 & 5 & guten morgen & 10.0 \\',
950+
r'\end{tabular}',
951+
r'\end{table}']
952+
t.meta['latexdict'] = {'caption':'this is a table'}
953+
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)