pylatexenc icon indicating copy to clipboard operation
pylatexenc copied to clipboard

Powers

Open sashaalesin opened this issue 5 years ago • 7 comments

>>> latex = 'e^{2x}'
>>> LatexNodes2Text().latex_to_text(latex)
'e^2x'

How i can get e^(2x)?

Thanks.

sashaalesin avatar May 07 '20 21:05 sashaalesin

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:

  • In LaTeX the syntax X_\mathrm{min} can be used instead of X_{\mathrm{min}}. With my above snippet, pylatexenc will not recognize the first syntax because it will parse the argument of _ as a macro argument, here capturing only the token \mathrm without {min}.

  • If you have underscores outside math mode, e.g., See \url{example.com/some_page}, then with my above snippet pylatexenc will attempt to parse the _ as a math subscript (whereas in LaTeX the \url command does some catcode magic to avoid that).

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.

phfaist avatar May 08 '20 16:05 phfaist

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

sashaalesin avatar May 09 '20 10:05 sashaalesin

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.

phfaist avatar Jun 22 '20 14:06 phfaist

Thanks a lot for the update.

nemeer avatar Jun 23 '20 11:06 nemeer

Could this special subset then be replaced?

Konfekt avatar Aug 18 '21 09:08 Konfekt

+1! Having θᵢ would be nice

hckiang avatar Aug 22 '21 16:08 hckiang

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}$?)

phfaist avatar Aug 29 '21 16:08 phfaist