astanin / python-tabulate

Pretty-print tabular data in Python, a library and a command-line utility. Repository migrated from bitbucket.org/astanin/python-tabulate.
https://pypi.org/project/tabulate/
MIT License
2.05k stars 159 forks source link

Question/feature request: LaTeX symbols in headers #170

Open s133314 opened 2 years ago

s133314 commented 2 years ago

I'm enjoying tabulate a lot, but I mostly use it just for printing a table in a jupyter notebook via the default plain format:

print(tabulate(table, ...)

I often have headers with symbols/small math formula's. I would very much like it if a \eta for example would automatically be rendered as when printing the table. I'm thinking similar to matplotlib where you can easily include latex math in labels and titles for example. Is this available and if not could it be made?

astanin commented 1 year ago

I suppose that tabulate should stick to producing only Unicode output. So it needs something to recognize and convert inline formulas $E = mc^2$ to Unicode text. Something like https://github.com/phfaist/pylatexenc

As a workaround I would suggest to pre-process your headers and or data with it or a similar converter. Or type your formulas direclty in Unicode.

Let me know what works for you.

I may add an option to tabulate which allows to specify a custom formatter function for all or some values. In your case you would be able to supply a function which takes a string "foo $\eta$" and returns "foo η".

s133314 commented 1 year ago

I did indeed for some things write my formula's directly in unicode but that was quite gruesome. But I guess with the converter you linked to (pylatexenc) that would ease my life. I'm not working with tabulate at the moment but I'll try it out when I use it again. If that library converts my latex to unicode that is good enough for me and I would not need the custom formatter function (could still be a nice to have for the tabulate module off course). Thanks!