Note: this is more or less a cross-post from my blog. I hope this is acceptable, especially that this is a genuine question!
I’m currently writing a (longish) document on mathematics. Among other things, I write about the Hausdorff metric, which I denote by $d_H$. Since this notation appears quite often, I decided to write a command for it:
\newcommand{\dH}{{d_H}}
The additional braces are there, because sometimes I want to talk about a ball with respect to the Hausdorff metric, denoted by $B_{d_H}$ – for that, I wanted to write B_\dH. Without the extra braces this won’t work (for obvious reasons).
So far, so good. But there’s another catch: I want a similar notion to the Hausdorff metric (call it a Haudorff quasimetric), which I denote by $d_H^*$. With my definition, \dH^* won’t work (I want the star to be above the subscript). So I wanted to be clever, and use an optional star; this way, I could write \dH or \dH*. I did this:
\newcommand{\dH}{\@ifstar{d_H^*}{d_H}}
But my ability to write B_\dH is lost now! And of course, adding braces around \@ifstar won’t help: it will then never “see” the star. (Note: I know that xparse makes defining commands with optional stars easier, but I expect it won't help with my problem.)
Personally, I can see no way out of this dilemma. It’s not a big deal, I can write B_{\dH} (which is cleaner LaTeX syntax anyway), but I’m curious whether there’s any way to eat this cake and have it, too.

B_{\dH}, as promotingB_\dHto ends up being weird as it depends on the definition of\dH. I might even consider using\BdHas a shortcut for that ball – daleif Aug 08 '14 at 09:42expl3people to explain how this can be done withxparse, using{d_H}in an\NewDocumentCommand, thenB_\dHdoes not work at all. – daleif Aug 08 '14 at 09:47\Ball[type=H](0,1)– daleif Aug 08 '14 at 09:48\@ifnextchar. Or\NewDocumentCommand. – user202729 Dec 17 '21 at 05:55mathordthough. – user202729 Jun 13 '22 at 16:10