When I write notes by hand, if I want to box an equation, I usually do it in the following way. I find the lowest (highest) point of the equation, I go a little bit higher (lower) and take a line, cutting it a little before and after the aforementioned point. If there are more than one extrema of equal height I do it for all of them (pic related).
Is there any way to do this in LaTeX? (for example with TikZ I guess)