14

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?

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Sorry for the late answer. This results in an error in the tabulate.tabulate() line: AttributeError: 'function' object has no attribute 'tabulate'
@Gabriel, the above answer likely uses import tabulate rather than from tabulate import tabulate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.