Back
other Jul 16, 2024

Trick to Check Equality of Expression Containing Subscripts Using a Basic LaTeX Expression Evaluator

A silly bug turned genius hack.

by Justin Skycak (@justinskycak) justinmath.com 283 words
View original

A silly bug turned genius hack.

Want to get notified about new posts? Join the mailing list and follow on X/Twitter.


Today I found a bug in a LaTeX expression evaluator that, by pure dumb luck, implemented a genius hack for comparing expressions involving subscripts.

The bug is that evaluator treats subscripts as multiplications. If you give it "a_{n}" it parses a variable "a_" times a variable "n".

(This is less a bug and more just that the evaluator was not built to handle subscripts, but I’ll call it a bug for simplicity since it could easily behave unexpextedly yet without errors if you don’t know about this limitation.)

Anyway, this bug initially seems silly – but as it turns out:

Even better, the fact that the subscript is parsed into the preceding variable symbol means you’re able to disambiguate "a_{n}" and "n_{a}" despite multiplication being associative.

So, what all this means is that

  1. you can reliably compare expressions containing subscripts,
  2. WITHOUT actually having to do any reasoning about subscripts in the evaluator.

What an elegant way to sidestep all the complexity in what otherwise would have been a real headache to deal with!


Want to get notified about new posts? Join the mailing list and follow on X/Twitter.