pylatexenc
pylatexenc copied to clipboard
Powers
>>> latex = 'e^{2x}'
>>> LatexNodes2Text().latex_to_text(latex)
'e^2x'
How i can get e^(2x)?
Thanks.
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 ofX_{\mathrm{min}}. With my above snippet,pylatexencwill not recognize the first syntax because it will parse the argument of_as a macro argument, here capturing only the token\mathrmwithout{min}. -
If you have underscores outside math mode, e.g.,
See \url{example.com/some_page}, then with my above snippetpylatexencwill attempt to parse the_as a math subscript (whereas in LaTeX the\urlcommand 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.
Thank you!
I need to use pylatexenc only for parsing math formulas, so it's good for me :)
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.
Thanks a lot for the update.
Could this special subset then be replaced?
+1! Having θᵢ would be nice
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}$?)