For convenience, I'll often create custom commands for variables I'm using often in a document, for example
\newcommand{\cartPos}{\ensuremath{x}}
This works fine, and I can now do all the usual things like $\dot{\cartPos}$. But if I have something with an underscore, such as
\newcommand{\vectorPos}{\ensuremath{r_x}}
if I now do \dot{\vectorPos}, the dot is applied to the entire object, instead of being aligned above the r only, and on in between the variable and the underscore.
Is there a way to deal with this elegantly?



\dot{r}_x, not\dot{r_x}. – Mico Jun 23 '23 at 16:26\ensuremath. – egreg Jun 23 '23 at 16:27