pandas to_latex() escapes mathmode

末鹿安然 提交于 2020-02-21 12:57:46

问题


pandas to_latex() methode seems to be a convenient way to convert bigger tabulars into into latex code. However, writing math mode, the $ seems to get escaped.

An example:

import pandas as pd
import numpy as np

table=np.asarray([['$x^2$',3]])
df=pd.DataFrame(table[:,1], columns=['value'], index=table[:,0])
print(df.to_latex(encoding='utf-8'))

output:

\begin{tabular}{ll}
\toprule
{} & value \\
\midrule
\$x\textasciicircum2\$ &     3 \\
\bottomrule
\end{tabular}

Is there a way to prevent this from happening?


回答1:


Yes, the method's escape parameter is set to True by default. You can change it to False:

print(df.to_latex(encoding='utf-8', escape=False))

\begin{tabular}{ll}
\toprule
{} & value \\
\midrule
$x^2$ &     3 \\
\bottomrule
\end{tabular}


来源:https://stackoverflow.com/questions/44260547/pandas-to-latex-escapes-mathmode

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