Latex#

class astropy.io.ascii.Latex(ignore_latex_commands=['hline', 'vspace', 'tableline', 'toprule', 'midrule', 'bottomrule'], latexdict={}, caption='', col_align=None)[source]#

Bases: BaseReader

LaTeX format table.

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

  • tabletypeused 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, format="latex",
                latexdict={'tabletype': 'table*'})
    

    If None, the table environment will be dropped, keeping only the tabular environment.

  • tablealignpositioning of table in text.

    The default is not to specify a position preference in the text. If, e.g. the alignment is ht, then the LaTeX will be \\begin{table}[ht].

  • col_alignAlignment of columns

    If not present all columns will be centered.

  • captionTable 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.

  • unitsdictionary 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, format="latex",
                latexdict={'units': {'mass': 'kg', 'speed': 'km/h'}})
    

    If the column has no entry in the units dictionary, it defaults to the unit attribute of the column. If this attribute is not specified (i.e. it is None), the unit will be written as ' '.

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, format="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, format="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, format="latex", col_align='|lr|',
            latexdict={'preamble': r'\begin{center}',
                       'tablefoot': r'\end{center}',
                       'tabletype': 'table*'})
captionSet table caption

Shorthand for:

latexdict['caption'] = caption
col_alignSet the column alignment.

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

latexdict['col_align'] = col_align

Attributes Summary

max_ndim

Methods Summary

write([table])

Write table as list of strings.

Attributes Documentation

max_ndim = None#

Methods Documentation

write(table=None)[source]#

Write table as list of strings.

Parameters:
tableTable

Input table data.

Returns:
lineslist

List of strings corresponding to ASCII table