Generate proper LaTeX table using tabulate Python package

徘徊边缘 提交于 2019-12-13 13:29:56

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/35418787/generate-proper-latex-table-using-tabulate-python-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!