question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

How i can get e^(2x)?

Thanks.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
phfaistcommented, Aug 29, 2021

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

2reactions
phfaistcommented, May 9, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Powers (TV Series 2015–2016) - IMDb
Set in a world where humans and superheroes co-exist, a homicide detective whose powers were taken from him investigates crimes involving superhumans along ......
Read more >
Powers (American TV series) - Wikipedia
Powers is an American streaming television series developed by Brian Michael Bendis and Charlie Huston for PlayStation Network. It is based on the...
Read more >
List of Supernatural Powers and Abilities - Superpower Wiki
Superpowers are superhuman and supernatural abilities, characteristics, and/or attributes employed and explored in many works of fiction and has become a ...
Read more >
POWERS - Facebook
Love the song "Stay Gold" on The Knocks new album "History!" I hope we get to hear more music from you two soon!...
Read more >
Powers Anchors
DEWALT is the leader in contractor power tools including cordless drills, woodworking tools and professional power tools.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found