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:


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:


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.

  • I think it is better to accept to always use B_{\dH}, as promoting B_\dH to ends up being weird as it depends on the definition of \dH. I might even consider using \BdH as a shortcut for that ball
    – daleif
    Commented Aug 8, 2014 at 9:42
  • Agreed (I more or less stated it in the question anyway), but my curiosity is still unsatisfied...
    – mbork
    Commented Aug 8, 2014 at 9:43
  • I'm guessing you will need some of the hardcore expl3 people to explain how this can be done with xparse, using {d_H} in an \NewDocumentCommand, then B_\dH does not work at all.
    – daleif
    Commented Aug 8, 2014 at 9:47
  • Might even be better to make a more intelligent ball macro instead with a syntax like \Ball[type=H](0,1)
    – daleif
    Commented Aug 8, 2014 at 9:48
  • Same issue happens with optional argument, because that one expands to \@ifnextchar. Or \NewDocumentCommand.
    – user202729
    Commented Dec 17, 2021 at 5:55

1 Answer 1


Delay the closing of the group: after _, one can use \bgroup.

\newcommand\dH{\bgroup d_H\@ifstar{^*\egroup}{\egroup}}

$B_\dH$ and $B_\dH*$

enter image description here

But avoid it.

  • +1 even though unbalanced groups always hurt my brain. Does one need to use \bgroup...\egroup here or would \begingroup...\endgroup also be possible? Commented Aug 8, 2014 at 10:05
  • 1
    This. Is. Pure. Awesome. (Though an extremely dirty trick.) The only reason I didn't accept this (yet?) is that I want to wait for more answers (though it seems unlikely that they'll come...) May I submit this to the "TeX beauties and oddities" session for the next BachoTeX conference (with due credit, of course?)
    – mbork
    Commented Aug 8, 2014 at 10:10
  • @mbork Of course you can. ;-) Note that \bgroup...\egroup don't hurt when \dH is used at the normal level, because the symbol is ordinary anyway.
    – egreg
    Commented Aug 8, 2014 at 10:15
  • One question: why define \s@dH?
    – mbork
    Commented Aug 8, 2014 at 10:27
  • 1
    @mbork Possibly not needed, actually. Removed.
    – egreg
    Commented Aug 8, 2014 at 10:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .