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

Question about equality #64

Closed npapapietro closed 3 years ago

npapapietro commented 3 years ago

Question equality of two latex expressions. Perhaps this is naïve but if I have two expressions that display the same way, should there be a way to do this?

a = r"\bar{x}_{y}^{z}"
b = r"\bar{x}^{z}_{y}"

def tex_eq(a,b):
    # equalitycheck

assert tex_eq(a,b) is True
phfaist commented 3 years ago

Hi, that's a tricky question. When do you consider two expressions equal? Is "\overline{x}" the same as "\bar{x}"? is "a-b" the same as "$a-b$"? If you can narrow down the scope of the task you can use pylatexenc to parse the latex expression into logical nodes, and then do whatever comparison or standardization you'd like on the level of the nodes. For instance, in your example, you could declare ^ and _ as "latex specials" in pylatexenc (see here) and parse their argument suitably; then you can scan through your parsed nodes and standardize the order of any subscripts and superscripts.

To summarize, I think pylatexenc can help you accomplish what you're after, by parsing the latex expression into logical nodes, but only once you've really specified more precisely what constitutes "two equal expressions". Good luck!

npapapietro commented 3 years ago

Thanks for the quick answer!