Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent pandas.Dataframe.to_latex from adding escape \ characters before latex formula characters (i.e dollar signs/brackets)?

I'm trying to produce a data table for a paper that I can insert into the LaTeX document, using pandas. However, the entries of both the table headers and the elements need to be mathematical formulas in latex ($...$). In my case, this is because some of them because they have subscripts, and others because all the table values need to have upper and lower error bars, i.e like 0.21^{+0.03}_{-0.05} etc...

So I made a pandas table with string elements such as '$0.21^{+0.03}_{-0.05}$'. The dollar signs are there because LaTeX needs to interpret this as a formula as far as I can tell. Unfortunately, when I then try to write the table into latex format with df.to_latex(...), the parser puts escape characters \ in front of every dollar sign. The same seems to happen with the underscores, brackets, and all the latex formula characters ('$0.21^{+0.03}_{-0.05}$' becomes '\$0.21\^\{+0.03\}\_\{-0.05\}$'

How would I be able to block this from happening?

like image 701
Marses Avatar asked Oct 15 '25 13:10

Marses


1 Answers

Based on the DataFrame.to_latex help, you can supply escape = False (default is True).

DataFrame.to_latex(escape = False, ...)
like image 154
Werner Avatar answered Oct 19 '25 06:10

Werner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!