latextable documentation

Drawing functions for outputting a Texttable table in a Latex format.

latextable.draw_latex(table, caption=None, caption_short=None, caption_above=False, label=None, drop_columns=None, drop_rows=None, position=None, use_booktabs=False, multicolumn_header=None, alias=None)

Draw a Texttable table in Latex format. Aside from table, all arguments are optional.

Parameters:
  • table – Texttable table to be rendered in Latex, or a list of rows that represents a table.

  • caption – A string that adds a caption to the Latex formatting.

  • caption_short – A string that adds a short caption (used in the list of tables). Ignored if caption is None.

  • caption_above – If True, the caption will be added above the table rather than below it (default).

  • label – A string that adds a referencing label to the Latex formatting.

  • drop_columns – A list of column names that won’t be in the Latex output. Each column name must be in the table header.

  • drop_rows – A list of row indices that won’t be in the Latex output. Each row index must be in [0, number of rows - 1], where number of rows does not include the header.

  • position – A string that represents LaTex’s float position of the table. For example ‘ht’ results in the float position [ht].

  • use_booktabs – Whether to override the table formatting with booktabs (https://ctan.org/pkg/booktabs?lang=en). If true, the texttable formatting is ignored, and instead the default booktabs style is used. This overrides the border, vertical lines, and horizontal lines. Note the booktabs package will need to be included in your Latex document (usepackage{booktabs}). Defaults to false.

  • multicolumn_header – A list of 2-tuples that defines multicolumn header names and widths. An additional header row will be added above the normal header row. The first entry in each 2-tuple is the header name, and the second entry is the number of columns it spans. The sum of column widths should equal the number of columns (after dropping any requested columns).

  • alias – A str -> str dictionary denoting strings in the table data that should be aliased in the Latex output. Useful for escaping special Latex characters (e.g. &) or inserting custom Latex. For example, to replace ‘+-’ with ‘$pm$’, the dict would be {‘+-’: ‘$pm$’}.

Returns:

The formatted Latex table returned as a single string.

exception latextable.DropColumnError(column, header)

Bases: Exception

Error thrown when a dropped column does not exist in the table header.

exception latextable.MulticolumnHeaderError(n_expected_columns, sum_multicolumn)

Bases: Exception

Error thrown when there is a mismatch between multicolumns and actual columns (after dropping columns).

exception latextable.DropRowError(n_rows, row_idx)

Bases: Exception

Error thrown when a dropped row is outside the range of valid rows.

latextable._draw_latex_preamble(table, position, caption, caption_short, use_booktabs)

Draw the Latex table preamble.

Applies column horizontal alignment, columns vlines, and table vertical border if appropriate.

Example Output:

\begin{table}
    \begin{center}
        \begin{tabular}{|l|r|c|}
Parameters:

table – Texttable table to be rendered in Latex.

Returns:

The Latex table preamble as a single string.

latextable._draw_latex_header(table, drop_columns, use_booktabs, multicolumn_header)

Draw the Latex header.

Applies header border if appropriate.

Example Output:

\hline
Name & Age & Nickname \\
\hline
Parameters:
  • table – Texttable table to be rendered in Latex.

  • drop_columns – A list of columns that should not be in the final Latex output.

  • multicolumn_header – A list of 2-tuples describing multicolumn header names and their widths.

Returns:

The Latex table header as a single string.

latextable._draw_latex_content(table, drop_columns, drop_rows, use_booktabs)

Draw the Latex table content.

Example Output:

MrXavierHuon & 32 & Xav' \\
\hline
MrBaptisteClement & 1 & Baby \\
\hline
MmeLouiseBourgeau & 28 & Lou Loue \\
Parameters:
  • table – Texttable table to be rendered in Latex.

  • drop_columns – A list of columns that should not be in the final Latex output.

  • drop_columns – A list of row indices that should not be in the final Latex output.

Returns:

The Latex table content as a single string.

latextable._draw_latex_postamble(table, caption, caption_short, label, use_booktabs)

Draw the Latex table postamble.

Adds caption and label if given. Applies table bottom border if appropriate.

Example Output:

    \hline
    \end{tabular}
\end{center}
\caption{An example table.}
\label{table:example_table}

end{table}

Parameters:
  • table – Texttable table to be rendered in Latex.

  • caption – A caption to add to the table.

  • caption_short – Short caption used in the list of tables. Ignored if caption is None.

  • label – A label to add to the table.

Returns:

The Latex table postamble as one string.

latextable._draw_table_caption(caption, caption_short)

Add a caption to the table, with an optional short version (for table of contents etc.).

Parameters:
  • caption – The main caption for the table.

  • caption_short – The short version of the caption.

Returns:

The Latex table caption as one string.

latextable._clean_row(row)

Clean a row prior to drawing. Currently just removes newlines.

Parameters:

row – Row to clean.

Returns:

Cleaned row.

latextable._sanitise_drop_columns(header, drop_columns, multicolumn_header)

Check the columns to be dropped - each column must be in the table header. If also using a multicolumn header, check the sum of column widths matches the number of columns (after dropping).

Parameters:
  • header – Table header array.

  • drop_columns – List of columns to be dropped.

Returns:

None

latextable._drop_columns(target, header, drop_columns)

Drop columns from a target array.

Parameters:
  • target – Array from which the columns should be dropped.

  • header – Table header array.

  • drop_columns – The columns that should be dropped. Each column should be in the header.

Returns:

The target array with the relevant columns dropped.

latextable._sanitise_drop_rows(n_rows, drop_rows)

Check the rows to be dropped - 0 <= row_idx < n_rows for each row_idx to be dropped.

Parameters:
  • drop_rows – List of rows to be dropped.

  • n_rows – Number of rows in the table (excluding the header).

Returns:

None

latextable._drop_rows(rows, drop_rows)

Drop rows by their indices.

Parameters:
  • rows – Table from which the rows should be dropped.

  • drop_rows – List of rows to be dropped.

Returns:

The target array with the relevant rows dropped.

latextable._indent_text(text, indent)

Indent a string by a certain number of tabs.

Parameters:
  • text – String to indent.

  • indent – Number of tabs.

Returns:

The indented string.