Skip to main content

An extension to the texttable library that exports tables directly to Latex.

Project description

latextable

License: MIT Build Status

Texttable is a Python package that can create simple ASCII tables. This package extends its functionality to allow the table to be directly output in Latex, removing the tedious copy and paste chore. The Latex output matches the table design, and there are utilities for adding table captions, labels, and positions.

Features

  • Draw a table object in a Latex format.
  • Matches table decoration (border, header, hlines, vlines).
  • Applies horizontal column alignment.
  • Allows the user to drop certain columns from the output.
  • Provides the ability to add a caption, reference label, and position to the Latex output.
  • The output is correctly indented for directly copying into Latex.
  • Supports booktabs formatting.

Installation

PyPi:

pip install latextable

Requirements:

texttable

Usage

The single function latextable.draw_latex returns a formatted Latex string based on the provided table. Aside from table, all arguments are optional.

draw_latex(table, caption=None, label=None, drop_columns=None, position=None):
    table: Texttable table to be rendered in Latex.
    caption: A string that adds a caption to the Latex formatting.
    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.
    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.

    return: The formatted Latex table returned as a single string.

Examples

A basic example is given below. For more see the examples directory.

Code:

table_1 = Texttable()
table_1.set_cols_align(["l", "r", "c"])
table_1.set_cols_valign(["t", "m", "b"])
table_1.add_rows([["Name", "Age", "Nickname"],
                 ["Mr\nXavier\nHuon", 32, "Xav'"],
                 ["Mr\nBaptiste\nClement", 1, "Baby"],
                 ["Mme\nLouise\nBourgeau", 28, "Lou\n \nLoue"]])
print('-- Example 1: Basic --')
print('Texttable Output:')
print(table_1.draw())
print('\nLatextable Output:')
print(latextable.draw_latex(table_1, caption="An example table.", label="table:example_table"))

Output:

-- Example 1: Basic --
Texttable Output:
+----------+-----+----------+
|   Name   | Age | Nickname |
+==========+=====+==========+
| Mr       |     |          |
| Xavier   |  32 |          |
| Huon     |     |   Xav'   |
+----------+-----+----------+
| Mr       |     |          |
| Baptiste |   1 |          |
| Clement  |     |   Baby   |
+----------+-----+----------+
| Mme      |     |   Lou    |
| Louise   |  28 |          |
| Bourgeau |     |   Loue   |
+----------+-----+----------+

Latextable Output:
\begin{table}
	\begin{center}
		\begin{tabular}{|l|r|c|}
			\hline
			Name & Age & Nickname \\
			\hline
			MrXavierHuon & 32 & Xav' \\
			\hline
			MrBaptisteClement & 1 & Baby \\
			\hline
			MmeLouiseBourgeau & 28 & Lou Loue \\
			\hline
		\end{tabular}
	\end{center}
	\caption{An example table.}
	\label{table:example_table}
\end{table}

Additional Info

For a more in depth article reviewing this library, see this Medium post.
A working example is also given in this Colab Notebook.

Release History

  • 0.2.1
    • Removed row hlines when using booktabs.
  • 0.2.0
    • Added support for booktabs and table positioning.
  • 0.1.1
    • Minor changes to documentation.
  • 0.1.0
    • Initial Release

Meta

Joseph Early
@JosephAEarly
joseph.early.ai@gmail.com

Distributed under the MIT license. See LICENSE for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

latextable-0.2.1.tar.gz (6.6 kB view details)

Uploaded Source

File details

Details for the file latextable-0.2.1.tar.gz.

File metadata

  • Download URL: latextable-0.2.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.6rc1

File hashes

Hashes for latextable-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7f50e144114017a099768da578e444e80dddb1ebbdae21be92de7a95534708a3
MD5 db4eb8b6a703d858f546c0759f734602
BLAKE2b-256 4853e491fd31dc36d87d93ea0894c294954377ea2db500d7ef50f949a3a9d77a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page