Latex

class astropy.io.ascii.latex.Latex(ignore_latex_commands=['hline', 'vspace', 'tableline'], latexdict={}, caption='', col_align=None) [edit on github][source]

Bases: astropy.io.ascii.core.BaseReader

Write and read LaTeX tables.

This class implements some LaTeX specific commands. Its main purpose is to write out a table in a form that LaTeX can compile. It is beyond the scope of this class to implement every possible LaTeX command, instead the focus is to generate a syntactically valid LaTeX tables.

This class can also read simple LaTeX tables (one line per table row, no \multicolumn or similar constructs), specifically, it can read the tables that it writes.

Reading a LaTeX table, the following keywords are accepted:

ignore_latex_commands :
Lines starting with these LaTeX commands will be treated as comments (i.e. ignored).

When writing a LaTeX table, the some keywords can customize the format. Care has to be taken here, because python interprets \\ in a string as an escape character. In order to pass this to the output either format your strings as raw strings with the r specifier or use a double \\\\.

Examples:

caption = r'My table \label{mytable}'
caption = 'My table \\\\label{mytable}'

latexdict : Dictionary of extra parameters for the LaTeX output

  • tabletype : used for first and last line of table.

    The default is \\begin{table}. The following would generate a table, which spans the whole page in a two-column document:

    ascii.write(data, sys.stdout, Writer = ascii.Latex,
                latexdict = {'tabletype': 'table*'})
    
  • col_align : Alignment of columns

    If not present all columns will be centered.

  • caption : Table caption (string or list of strings)

    This will appear above the table as it is the standard in many scientific publications. If you prefer a caption below the table, just write the full LaTeX command as latexdict['tablefoot'] = r'\caption{My table}'

  • preamble, header_start, header_end, data_start, data_end, tablefoot: Pure LaTeX

    Each one can be a string or a list of strings. These strings will be inserted into the table without any further processing. See the examples below.

  • units : dictionary of strings

    Keys in this dictionary should be names of columns. If present, a line in the LaTeX table directly below the column names is added, which contains the values of the dictionary. Example:

    from astropy.io import ascii
    data = {'name': ['bike', 'car'], 'mass': [75,1200], 'speed': [10, 130]}
    ascii.write(data, Writer=ascii.Latex,
                     latexdict = {'units': {'mass': 'kg', 'speed': 'km/h'}})
    

    If the column has no entry in the units dictionary, it defaults to ' '.

Run the following code to see where each element of the dictionary is inserted in the LaTeX table:

from astropy.io import ascii
data = {'cola': [1,2], 'colb': [3,4]}
ascii.write(data, Writer=ascii.Latex, latexdict=ascii.latex.latexdicts['template'])

Some table styles are predefined in the dictionary ascii.latex.latexdicts. The following generates in table in style preferred by A&A and some other journals:

ascii.write(data, Writer=ascii.Latex, latexdict=ascii.latex.latexdicts['AA'])

As an example, this generates a table, which spans all columns and is centered on the page:

ascii.write(data, Writer=ascii.Latex, col_align='|lr|',
            latexdict={'preamble': r'\begin{center}',
                       'tablefoot': r'\end{center}',
                       'tabletype': 'table*'})
caption : Set table caption

Shorthand for:

latexdict['caption'] = caption
col_align : Set the column alignment.

If not present this will be auto-generated for centered columns. Shorthand for:

latexdict['col_align'] = col_align

Methods Summary

write([table])

Methods Documentation

write(table=None) [edit on github][source]

Page Contents