drakegroup / sigfig

This is the sigfig Python package used for rounding numbers (with expected results)
MIT License
22 stars 6 forks source link

feature request: TeX formatted output #17

Open jslavin opened 1 year ago

jslavin commented 1 year ago

I'm wondering if you'd be open to offering TeX formatted output as an option. My particular use case is for labelling curves in matplotlib, which accepts TeX formatted strings. So, for example round(3.14159E6, sigfigs=3, format='TeX') '$3.14\times10^{6}$' (not sure if you'd want to use 'format' or 'notation' for the keyword). I've coded this myself, though, my code doesn't have all the features of yours.

MikeBusuttil commented 3 weeks ago

that's a good idea @jslavin 💡 I'll add it to the roadmap

In the meantime, the following will give you the same result:

from sigfig import round
def round_tex(*args, **kwargs):
    rounded = str(round(*args, **kwargs))
    inner_tex = rounded.replace('E', '\\times10^{') + '}' if 'E' in rounded else rounded
    return f"${inner_tex}$"

which you can used as follows:

>>> round_tex(3.14159E6, sigfigs=3, format='sci')
'$3.14\times10^{6}$'
>>> round_tex(3.14159E6, sigfigs=3)
'$3140000.0$'