使用制表Python包生成正确的LaTeX表 [英] Generate proper LaTeX table using tabulate Python package

查看:564
本文介绍了使用制表Python包生成正确的LaTeX表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用制表 Python包来生成正确的LaTeX格式的表. /p>

这是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")

通过此示例,我得到的是:

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

正确的格式应为:

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

即:程序包在$符号之前插入反冲,并将\pm中的反冲替换为\textbackslash{}.

是否可以生成正确格式的表?

解决方案

更改tablefmt,使其等于latex_raw.从文档中:

latex_raw的行为类似于latex,但不会转义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?

解决方案

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.

这篇关于使用制表Python包生成正确的LaTeX表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆