How to create Latex tables directly from Python code | by Joseph Early | Towards Data Science

Creating tables of results plays a major part in communicating the outcomes of experiments in data science. Various solutions exist for producing tables in Python and drawing them in the console output, but how do you transfer them to a written report? Usually, this requires some time-consuming and mind-numbing copying and pasting, but there are some better solutions.
A few libraries exist for Python that allow you to draw tables. In this article I’m going to focus on two in particular: tabulate and texttable. Both of them fulfil the same goal — the production of nicely formatted tables with as little hassle as possible. Tabulate is the more popular library, with over 10 million downloads last month. In comparison, texttable comes in at just under 2 million (according to PyPi Stats at the end of June 2020):
Both libraries allow you to format the tables in a particular style. Texttable gives complete autonomy to the user in terms of choosing their table design; it’s possible to customise column alignment, header alignment, table decoration, column types and even the characters used to draw the lines between the rows and columns. Tabulate chooses to focus instead on pre-designed formats, but the user is still able to override these if they choose to do so.
My personal preference is to use texttable. In all likelihood, I use it because it was the first library that I came across for creating tables, but it hasn’t let me down yet.
The tabulate formats are mostly geared towards use in other platforms or to emulate their designs, e.g. github, html, psql. One of these formats allows the creation of latex code, providing you with text that you can directly copy and paste into your Latex document. Job done — that was easy.
Texttable, being more lightweight than tabulate, offers no such solution. However, as a personal side project, I put together my first Python library that acts as a wrapper around texttable to create Latex tables. I creatively called it latextable, and it’s currently available on PyPi. It provides a similar output to the tabulate Latex format, as well as providing extra functionality such as matching the formatting of the texttable console output and allowing the user to add table captions and labels.
Using either library is incredibly straightforward; they both do exactly what they’re designed for and have pretty similar usages. For a list of rows containing your table data, the tables are created as such:
print('Tabulate Table:')print(tabulate(rows, headers='firstrow'))table = Texttable()table.set_cols_align(["c"] * 4)table.set_deco(Texttable.HEADER | Texttable.VLINES)print('\nTexttable Table:')print(table.draw())
Outputting the Latex code is equally easy:
print(‘\nTabulate Latex:’)print(tabulate(rows, headers=’firstrow’, tablefmt=’latex’))print(‘\nTexttable Latex:’)print(latextable.draw_latex(table, caption=”A comparison of rocket features.”))
And it gives the following Latex code:
Both Latex outputs can be copied directly into a Latex document, and the latextable output has the advantage of being automatically indented. A comparison of the console output with the rendered Latex is given below:
I’ve provided the complete source code for this exampled in a Colab Notebook. It’s likely that you’ll still have to make some alterations to the Latex code to get it exactly how you want it, but by using one of these tools at least you don’t have to waste time copying and pasting stuff across. It also gives the benefit that you won’t make any mistakes when copying across!