Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate proper LaTeX table using tabulate Python package

Tags:

python

latex

I'm using the tabulate Python package to generate a properly LaTeX formatted table.

Here's a MWE:

from tabulate import tabulate

table = [[r"${:.1f}\pm{:.1f}$".format(2.3564, 0.5487)],
         [r"${:.1f}\pm{:.1f}$".format(45.1236, 8.00021)]
         ]

print tabulate(table, tablefmt="latex")

What I get with this example is:

\begin{tabular}{l}
\hline
 \$2.4\textbackslash{}pm0.5\$  \\
 \$45.1\textbackslash{}pm8.0\$ \\
\hline
\end{tabular}

when the proper formatting would be:

\begin{tabular}{l}
\hline
 $2.4\pm0.5$  \\
 $45.1\pm8.0$ \\
\hline
\end{tabular}

I.e.: the package is inserting backlashes before the $ symbols, and replacing the backlash in \pm with \textbackslash{}.

Is it possible to generate the correct formatted table?

like image 399
Gabriel Avatar asked Feb 15 '16 20:02

Gabriel


People also ask

What package use to for table in LaTeX?

The tabular environment is the default LaTeX method to create tables. You must specify a parameter to this environment; here we use {c c c} which tells LaTeX that there are three columns and the text inside each one of them must be centred.

How do I import a tabulate module in Python?

Click the Python Interpreter tab within your project tab. Click the small + symbol to add a new library to the project. Now type in the library to be installed, in your example "tabulate" without quotes, and click Install Package . Wait for the installation to terminate and close all pop-ups.


2 Answers

Change the tablefmt so that it is equal to latex_raw. From the documentation:

latex_raw behaves like latex but does not escape LaTeX commands and special characters.

like image 92
Roby Avatar answered Sep 21 '22 16:09

Roby


This

tabulate.LATEX_ESCAPE_RULES = {}

worked for me

>>> tabulate.LATEX_ESCAPE_RULES = {}
>>> print(tabulate.tabulate([[r'\alpha']], tablefmt='latex'))

\begin{tabular}{l}
\hline
 \alpha \\
\hline
\end{tabular}

worked for me.

like image 20
sega_sai Avatar answered Sep 22 '22 16:09

sega_sai