latextable


Namelatextable JSON
Version 1.0.1 PyPI version JSON
download
home_page
SummaryAn extension to the texttable library that exports tables directly to Latex.
upload_time2023-11-10 13:26:09
maintainer
docs_urlNone
author
requires_python>=3.7
licenseMIT
keywords table texttable latex
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # latextable

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
![Build Status](https://github.com/JAEarly/latextable/workflows/build/badge.svg)
[![Downloads](https://static.pepy.tech/personalized-badge/latextable?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads)](https://pepy.tech/project/latextable)
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://paypal.me/JosephAEarly?country.x=GB&locale.x=en_GB)

<p align="center">
	<img src="/docs/latextable_logo.png" width="300" />
</p>

[Texttable](https://github.com/foutaise/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 columns and rows 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](https://ctan.org/pkg/booktabs?lang=en) formatting.
- Multicolumn headers can be included.
- Table data can be aliased for Latex output (e.g., escaping characters).

## Installation

PyPi:

```
pip install latextable
```

Requirements:

```
texttable
```

## Usage

The single function `latextable.draw_latex` returns a formatted Latex string based on the provided Texttable table or rows.
Aside from table, all arguments are optional.
Full documentation is available on [Read the Docs](https://latextable.readthedocs.io/en/latest/).

```
def 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):
    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$'}.

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

### Examples
A basic example is given below.
For more see the [examples directory](examples).

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](https://towardsdatascience.com/how-to-create-latex-tables-directly-from-python-code-5228c5cea09a).  
A working example is also given in this [Colab Notebook](https://colab.research.google.com/drive/1Iq10lHznMngg1-Uoo-QtpTPii1JDYSQA?usp=sharing).  

## Release History

* 1.0.1
    * Fixed bug with multicolumn headers missing horizontal and vertical lines. Thanks to [electricbrass](https://github.com/electricbrass).   
* 1.0.0
    * Added the ability to a list of rows rather than a Texttable object (Texttable is still a requirement).
      Inspired by [latextable-lite](https://github.com/huisyy/latextable-lite) from [Huisyy](https://github.com/huisyy).
    * Added support for multi-column headers.
    * Added aliasing, where strings in the original data can be replaced before outputting to Latex (e.g., escaping characters).
    * Added docs via Read the Docs.
    * Updated build to pyproject.toml for PEP 621 compliance (thanks to [KOLANICH](https://github.com/KOLANICH)).
    * Fixed bug that occurs when texttable align is not set.
* 0.3.0
    * Added support for [short captions](https://tex.stackexchange.com/questions/11579/short-captions-for-figures-in-listoffigures)
      (thanks to [PhilW92](https://github.com/PhilW92)).
    * Added the ability to drop rows as well as columns.
    * Captions can now be placed above tables instead of below.
* 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

Website: [Joseph Early](https://www.jearly.co.uk/)  
Twitter: [@JosephAEarly](https://twitter.com/JosephAEarly)  
Email: [joseph.early.ai@gmail.com](mailto:joseph.early.ai@gmail.com)

[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/jearly)

Distributed under the MIT license. See [LICENSE](LICENSE) for more information.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "latextable",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "table,texttable,latex",
    "author": "",
    "author_email": "Joseph Early <joseph.early.ai@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/36/1b/c33196862b41b8b69d46d3aa9e2aafefcf378ff05ff67da6014d68d724ac/latextable-1.0.1.tar.gz",
    "platform": null,
    "description": "# latextable\r\n\r\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\r\n![Build Status](https://github.com/JAEarly/latextable/workflows/build/badge.svg)\r\n[![Downloads](https://static.pepy.tech/personalized-badge/latextable?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads)](https://pepy.tech/project/latextable)\r\n[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://paypal.me/JosephAEarly?country.x=GB&locale.x=en_GB)\r\n\r\n<p align=\"center\">\r\n\t<img src=\"/docs/latextable_logo.png\" width=\"300\" />\r\n</p>\r\n\r\n[Texttable](https://github.com/foutaise/texttable) is a Python package that can create simple ASCII tables.\r\nThis package extends its functionality to allow the table to be directly output in Latex, removing the tedious copy and paste chore.\r\nThe Latex output matches the table design, and there are utilities for adding table captions, labels, and positions.\r\n\r\n## Features\r\n- Draw a table object in a Latex format.\r\n- Matches table decoration (border, header, hlines, vlines).\r\n- Applies horizontal column alignment.\r\n- Allows the user to drop columns and rows from the output.\r\n- Provides the ability to add a caption, reference label, and position to the Latex output.\r\n- The output is correctly indented for directly copying into Latex.\r\n- Supports [booktabs](https://ctan.org/pkg/booktabs?lang=en) formatting.\r\n- Multicolumn headers can be included.\r\n- Table data can be aliased for Latex output (e.g., escaping characters).\r\n\r\n## Installation\r\n\r\nPyPi:\r\n\r\n```\r\npip install latextable\r\n```\r\n\r\nRequirements:\r\n\r\n```\r\ntexttable\r\n```\r\n\r\n## Usage\r\n\r\nThe single function `latextable.draw_latex` returns a formatted Latex string based on the provided Texttable table or rows.\r\nAside from table, all arguments are optional.\r\nFull documentation is available on [Read the Docs](https://latextable.readthedocs.io/en/latest/).\r\n\r\n```\r\ndef draw_latex(table, caption=None, caption_short=None, caption_above=False, label=None, drop_columns=None,\r\n               drop_rows=None, position=None, use_booktabs=False, multicolumn_header=None, alias=None):\r\n    table: Texttable table to be rendered in Latex, or a list of rows that represents a table.\r\n    caption: A string that adds a caption to the Latex formatting.\r\n    caption_short: A string that adds a short caption (used in the list of tables). Ignored if caption is None.\r\n    caption_above: If True, the caption will be added above the table rather than below it (default).\r\n    label: A string that adds a referencing label to the Latex formatting.\r\n    drop_columns: A list of column names that won't be in the Latex output.\r\n     Each column name must be in the table header.\r\n    drop_rows: A list of row indices that won't be in the Latex output.\r\n     Each row index must be in [0, number of rows - 1], where number of rows does not include the header.\r\n    position: A string that represents LaTex's float position of the table.\r\n     For example 'ht' results in the float position [ht].\r\n    use_booktabs: Whether to override the table formatting with booktabs (https://ctan.org/pkg/booktabs?lang=en).\r\n     If true, the texttable formatting is ignored, and instead the default booktabs style is used.\r\n     This overrides the border, vertical lines, and horizontal lines.\r\n     Note the booktabs package will need to be included in your Latex document (\\\\usepackage{booktabs}).\r\n     Defaults to false.\r\n    multicolumn_header: A list of 2-tuples that defines multicolumn header names and widths.\r\n     An additional header row will be added above the normal header row.\r\n     The first entry in each 2-tuple is the header name, and the second entry is the number of columns it spans.\r\n     The sum of column widths should equal the number of columns (after dropping any requested columns).\r\n    alias: A str -> str dictionary denoting strings in the table data that should be aliased in the Latex output.\r\n     Useful for escaping special Latex characters (e.g. &) or inserting custom Latex.\r\n     For example, to replace '+-' with '$\\\\pm$', the dict would be {'+-': '$\\\\pm$'}.\r\n\r\n    return: The formatted Latex table returned as a single string.\r\n```\r\n\r\n### Examples\r\nA basic example is given below.\r\nFor more see the [examples directory](examples).\r\n\r\nCode:\r\n\r\n```\r\ntable_1 = Texttable()\r\ntable_1.set_cols_align([\"l\", \"r\", \"c\"])\r\ntable_1.set_cols_valign([\"t\", \"m\", \"b\"])\r\ntable_1.add_rows([[\"Name\", \"Age\", \"Nickname\"],\r\n                 [\"Mr\\nXavier\\nHuon\", 32, \"Xav'\"],\r\n                 [\"Mr\\nBaptiste\\nClement\", 1, \"Baby\"],\r\n                 [\"Mme\\nLouise\\nBourgeau\", 28, \"Lou\\n \\nLoue\"]])\r\nprint('-- Example 1: Basic --')\r\nprint('Texttable Output:')\r\nprint(table_1.draw())\r\nprint('\\nLatextable Output:')\r\nprint(latextable.draw_latex(table_1, caption=\"An example table.\", label=\"table:example_table\"))\r\n```\r\n\r\nOutput:\r\n\r\n```\r\n-- Example 1: Basic --\r\nTexttable Output:\r\n+----------+-----+----------+\r\n|   Name   | Age | Nickname |\r\n+==========+=====+==========+\r\n| Mr       |     |          |\r\n| Xavier   |  32 |          |\r\n| Huon     |     |   Xav'   |\r\n+----------+-----+----------+\r\n| Mr       |     |          |\r\n| Baptiste |   1 |          |\r\n| Clement  |     |   Baby   |\r\n+----------+-----+----------+\r\n| Mme      |     |   Lou    |\r\n| Louise   |  28 |          |\r\n| Bourgeau |     |   Loue   |\r\n+----------+-----+----------+\r\n\r\nLatextable Output:\r\n\\begin{table}\r\n\t\\begin{center}\r\n\t\t\\begin{tabular}{|l|r|c|}\r\n\t\t\t\\hline\r\n\t\t\tName & Age & Nickname \\\\\r\n\t\t\t\\hline\r\n\t\t\tMrXavierHuon & 32 & Xav' \\\\\r\n\t\t\t\\hline\r\n\t\t\tMrBaptisteClement & 1 & Baby \\\\\r\n\t\t\t\\hline\r\n\t\t\tMmeLouiseBourgeau & 28 & Lou Loue \\\\\r\n\t\t\t\\hline\r\n\t\t\\end{tabular}\r\n\t\\end{center}\r\n\t\\caption{An example table.}\r\n\t\\label{table:example_table}\r\n\\end{table}\r\n```\r\n\r\n## Additional Info\r\n\r\nFor a more in depth article reviewing this library, see this [Medium post](https://towardsdatascience.com/how-to-create-latex-tables-directly-from-python-code-5228c5cea09a).  \r\nA working example is also given in this [Colab Notebook](https://colab.research.google.com/drive/1Iq10lHznMngg1-Uoo-QtpTPii1JDYSQA?usp=sharing).  \r\n\r\n## Release History\r\n\r\n* 1.0.1\r\n    * Fixed bug with multicolumn headers missing horizontal and vertical lines. Thanks to [electricbrass](https://github.com/electricbrass).   \r\n* 1.0.0\r\n    * Added the ability to a list of rows rather than a Texttable object (Texttable is still a requirement).\r\n      Inspired by [latextable-lite](https://github.com/huisyy/latextable-lite) from [Huisyy](https://github.com/huisyy).\r\n    * Added support for multi-column headers.\r\n    * Added aliasing, where strings in the original data can be replaced before outputting to Latex (e.g., escaping characters).\r\n    * Added docs via Read the Docs.\r\n    * Updated build to pyproject.toml for PEP 621 compliance (thanks to [KOLANICH](https://github.com/KOLANICH)).\r\n    * Fixed bug that occurs when texttable align is not set.\r\n* 0.3.0\r\n    * Added support for [short captions](https://tex.stackexchange.com/questions/11579/short-captions-for-figures-in-listoffigures)\r\n      (thanks to [PhilW92](https://github.com/PhilW92)).\r\n    * Added the ability to drop rows as well as columns.\r\n    * Captions can now be placed above tables instead of below.\r\n* 0.2.1\r\n    * Removed row hlines when using booktabs.\r\n* 0.2.0\r\n    * Added support for booktabs and table positioning.\r\n* 0.1.1\r\n    * Minor changes to documentation.\r\n* 0.1.0\r\n    * Initial Release.\r\n\r\n## Meta\r\n\r\nWebsite: [Joseph Early](https://www.jearly.co.uk/)  \r\nTwitter: [@JosephAEarly](https://twitter.com/JosephAEarly)  \r\nEmail: [joseph.early.ai@gmail.com](mailto:joseph.early.ai@gmail.com)\r\n\r\n[![\"Buy Me A Coffee\"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/jearly)\r\n\r\nDistributed under the MIT license. See [LICENSE](LICENSE) for more information.\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "An extension to the texttable library that exports tables directly to Latex.",
    "version": "1.0.1",
    "project_urls": {
        "Homepage": "https://github.com/JAEarly/latextable"
    },
    "split_keywords": [
        "table",
        "texttable",
        "latex"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "941834acc346d10326ab4d615cd1919bcbf2b22f13c365a7c88ba8afc60b5270",
                "md5": "e9efe2cc328019e61055320969834cc0",
                "sha256": "3c2db035574f36ef95ed8e2ccf32d37c2f534b6f2701022c5e00f159244b8306"
            },
            "downloads": -1,
            "filename": "latextable-1.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e9efe2cc328019e61055320969834cc0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 9130,
            "upload_time": "2023-11-10T13:26:07",
            "upload_time_iso_8601": "2023-11-10T13:26:07.434252Z",
            "url": "https://files.pythonhosted.org/packages/94/18/34acc346d10326ab4d615cd1919bcbf2b22f13c365a7c88ba8afc60b5270/latextable-1.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "361bc33196862b41b8b69d46d3aa9e2aafefcf378ff05ff67da6014d68d724ac",
                "md5": "6e7799305de59715064f424b64c90962",
                "sha256": "e0d5e426f65e06c39d4b16441f027e435393bf11b40151a3aab3c706576d9ce9"
            },
            "downloads": -1,
            "filename": "latextable-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "6e7799305de59715064f424b64c90962",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 9107,
            "upload_time": "2023-11-10T13:26:09",
            "upload_time_iso_8601": "2023-11-10T13:26:09.236035Z",
            "url": "https://files.pythonhosted.org/packages/36/1b/c33196862b41b8b69d46d3aa9e2aafefcf378ff05ff67da6014d68d724ac/latextable-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-10 13:26:09",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "JAEarly",
    "github_project": "latextable",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "latextable"
}
        
Elapsed time: 0.15033s