Skip to content

Commit 324ffd2

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 d3058e3 commit 324ffd2

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

‎astropy/table/table.py‎

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

1524+
def _repr_latex_ (self, latexdict={}):
1525+
"""
1526+
Format Table object into the LaTeX format and print to the screen.
1527+
1528+
Parameters
1529+
----------
1530+
latexdict : str or dict
1531+
Dictionary of LaTeX formatting options or either one of these three
1532+
strings corresponding to pre-defined options, ``AA``, ``doublelines``,
1533+
``template``. Defaults to ``{}``.
1534+
1535+
Notes
1536+
-----
1537+
Uses ``latex.Latex.write()`` function from ``astropy.io.ascii`` to change
1538+
the table format to latex. The default LaTeX formatting options defined
1539+
in ``latex.Latex`` are assumed if no ``latexdict`` argument is passed.
1540+
See: https://docs.astropy.org/en/latest/api/astropy.io.ascii.Latex.html#astropy.io.ascii.Latex
1541+
1542+
A new key ``latexdict`` corresponding to ``Latex.latex`` dictionary of
1543+
``astropy.io.ascii.latex`` object is added to ``table.Table.meta``.
1544+
It contains the LaTeX formatting options for the table used.
1545+
"""
1546+
from astropy.io import ascii
1547+
from astropy.io.ascii import latex
1548+
1549+
if latexdict in ['AA', 'doublelines', 'template']:
1550+
latexdict = ascii.latex.latexdicts[latexdict]
1551+
1552+
lTable = latex.Latex(latexdict=latexdict)
1553+
lines = lTable.write(table=self)
1554+
self.meta['latexdict'] = lTable.latex
1555+
for line in lines:
1556+
print(line)
1557+
15241558
def __str__(self):
15251559
return '\n'.join(self.pformat())
15261560

‎astropy/table/tests/test_pprint.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -923,13 +923,19 @@ def test_repr_latex():
923923
"""
924924
Tests the _repr_latex_ method of astropy.table.Table class.
925925
Output should be a latex formatted table printed on the screen.
926-
Test for PR related to issue #3792.
926+
Test for PR related to issue #3972.
927927
"""
928+
from astropy.io import ascii
928929
a = [1, 2, 3, 5]
929930
b = np.array([4, 5, 6, 5])
930931
c = ['hello', 'konichiwa', 'namaskar', 'guten morgen']
931932
d = [2, 3, 5, 10] * u.m/u.s**2
932-
table = Table([a, b, c, d], names=('val', 'val1', 'greeting', 'speed'))
933-
table._repr_latex_()
933+
t = Table([a, b, c, d], names=('val', 'val1', 'greeting', 'speed'))
934+
935+
t._repr_latex_(latexdict='AA')
936+
t._repr_latex_(latexdict='doublelines')
937+
t._repr_latex_(latexdict='template')
938+
t._repr_latex_(latexdict={'caption':'demo table'})
939+
#print(t.meta)
934940

935941

0 commit comments

Comments
 (0)