A macro that detects the subscript of the argument

macrossubscriptssuperscripts

I'm not good at TeX programming. I've looked for the method that detects the subscript or superscript of the argument in macro. Existing relevant answers using the xifthen package (Detecting subscript in command argument) are somewhat verbose for me to understand. Specifically, I want to make a macro such that if the argument has a subscript, \dot{} is applied only to the base character. It would have the form

\newcommand{\rdot}[1]{'code body'}

For example, \rdot{a_{bcd}} will give
enter image description here
instead of enter image description here that \dot{a_{bcd}} gives. \rdot should apply to superscripts in the same way.

Edit: I noticed that some macros in the answers yield a different result for boldface letters. For example, consider \rdot{\bf{v}_{abc}}. I didn't know it would make such a difference.

Best Answer

Testing for _ is fragile and likely to fail if the subscript is hidden in a macro or made with \sb rather than _ or perhaps if it follows ^ so the base isn't "everything before _" .

I don't think it is needed as the accent will naturally apply to the first token or brace group.

enter image description here

\documentclass{article}

\newcommand\rdot[1]{\dot #1}

\begin{document}


$\rdot{a_{bcd}}$ and $\rdot{a}$ or even $\rdot{a^{2}_b}$

\end{document}
Related Question