I have a number in my python script that I want to use as part of the title of a plot in matplotlib. Is there a function that converts a float to a formatted TeX string?
Basically,
str(myFloat)
returns
3.5e+20
but I want
$3.5 \times 10^{20}$
or at least for matplotlib to format the float like the second string would be formatted. I'm also stuck using python 2.4, so code that runs in old versions is especially appreciated.
With old stype formatting:
with new format:
You can do something like:
in the old style:
Install the num2tex package:
and format your title as:
or use the
_repr_latex_()
method:which will give you the same thing.
num2tex
inherits fromstr
so theformat
function can be used as you would use it for a string:Disclaimer: I (very recently) created
num2tex
. It works well for my workflow and I am now trying to get feedback from others who might be interested in using it.