phfaist / pylatexenc

Simple LaTeX parser providing latex-to-unicode and unicode-to-latex conversion
https://pylatexenc.readthedocs.io
MIT License
301 stars 37 forks source link

Powers #36

Open sashaalesin opened 4 years ago

sashaalesin commented 4 years ago
>>> latex = 'e^{2x}'
>>> LatexNodes2Text().latex_to_text(latex)
'e^2x'

How i can get e^(2x)?

Thanks.

phfaist commented 4 years ago

Hi, thanks for the issue. I've been thinking a bit about how to handle powers and subscripts but there are some edge cases I want to make sure that I can polish before providing support for these out of the box. Here is a simple solution you can use in the meantime:

from pylatexenc import macrospec, latexwalker, latex2text

# define ^/_ for the parser as accepting a mandatory argument
lwc = latexwalker.get_default_latex_context_db()
lwc.add_context_category('powers', specials=[
    macrospec.SpecialsSpec('^', args_parser=macrospec.MacroStandardArgsParser('{')),
    macrospec.SpecialsSpec('_', args_parser=macrospec.MacroStandardArgsParser('{')),
])

# define the replacement string for ^
l2tc = latex2text.get_default_latex_context_db()
l2tc.add_context_category('powers', specials=[
    latex2text.SpecialsTextSpec('^', simplify_repl='^(%s)'),
    latex2text.SpecialsTextSpec('_', simplify_repl='_(%s)'),
])

latex_text = r'e^{x_1}'

lw = latexwalker.LatexWalker(latex_text, latex_context=lwc)
l2t = latex2text.LatexNodes2Text(latex_context=l2tc)

print(l2t.nodelist_to_text(lw.get_latex_nodes()[0]))
# e^(x_(1))

The above code has the following caveats:

I'm currently thinking about some ideas for ways to avoid these caveats, in order to support ^ and _ by default in pylatexenc, but I haven't found the time to work these out fully yet.

sashaalesin commented 4 years ago

Thank you! I need to use pylatexenc only for parsing math formulas, so it's good for me :)

phfaist commented 4 years ago

Hi @nemeer, I think your issue is an inherent limitation of Unicode—as far as I know, there is no way to represent M_\odot in unicode. (Only a very specific subset of characters can be set in subscript/superscript in unicode as I can tell from "Unicode subscripts and superscripts" on wikipedia.) In my snippet above, I used the text replacement "M_(⊙)" which is the closest I could think of. You can change the replacement text with whatever you like in the arguments simplify_repl='^(%s)' of the SpecialsTextSpec(...) calls. You can specify a function, too, to implement more complicated logic, see the docs. Hope this helps.

nemeer commented 4 years ago

Thanks a lot for the update.

Konfekt commented 3 years ago

Could this special subset then be replaced?

hckiang commented 3 years ago

+1! Having θᵢ would be nice

phfaist commented 3 years ago

Thanks for the pointer to the list of unicode sub/superscripts. For the reasons that were mentioned above, it isn't straightforward to implement this. I'm thinking about some upgrades to how LaTeX gets converted to unicode text, and I'll try to integrate unicode super/subscripts as much as possible. (Plus, there would be additional design decisions, e.g., what should happen to subscripts where not all characters have a unicode subscript variant, such as $\theta_{i,j*k}$?)