I'm trying to write a macro which automatically adds parentheses to an expression if the argument is composite.
Specifically, I would like to define a command for the inner product of two vectors using the
$v\cdot w$ notation. So I would define
However, if one argument is a composite expression like
the result should be
$v\cdot(w_1+w_2)$. I would like the parentheses to be added automatically. The reason is that at some point later I might decide to switch to angle notation
<v,w_1+w_2>, which would make the parentheses unnecessary.
I looked into packages like
xstring to test for string length. However, I didn't get it to work with subscript expressions like
$x_i$ (where no parentheses should be added).
Is something like this possible?