I'd like to create a command which only targets the first "level" of the indexes. I'll illustrate with some examples, using \qq as the command in the first line, and the "real" LaTeX code on the line below:
d\qq{SA}
d_{\mathrm{SA}}
d\qq{SA_j}
d_{\mathrm{SA_{\mathit{j}}}}
d\qq{S_{j,k} A_j}
d_{\mathrm{S_{\mathit{j,k}} A_{\mathit{j}}}}
d\qq{S_{j,k} A_{j,k}}
d_{\mathrm{S_{\mathit{j,k}} A_{\mathit{j,k}}}}
The compiled code should look like this:

I have absolutely no idea on how to de this, or if it can be done just using \newcommand.
Please help, doing this manually drives me nuts :)


$d_{\mathrm{SA}_{j}}$: mark as\mathrmonly the bit that should go in upright type. – egreg Apr 23 '13 at 14:56