I want to define a macro \ind which let's me set in math mode a subscript in upright letters and when starred (\ind*) in italic letters. Additionally, an optional argument allows me to adjust the kerning between letter and index, i.e. W_{\ind[-2mu]{x}}. This works quite well. But surprisingly, I have to put braces around the \ind-command in order to make it run.
Can someone explain me why I have to put braces around the command and how I can modify the macro to avoid this? It's a super big deal but I would like to understand the mechanism behind it.
The error is always that a { and a } is missing.
MWE:
\documentclass{article}
\makeatletter
\newcommand\ind{@ifstar{\ind@star}{\ind@nostar}}
\newcommand\ind@star[2][]{\mkern \muexpr 0mu #1 #2}
\newcommand\ind@nostar[2][]{\mathrm{\mkern \muexpr 0mu #1 #2}}
\makeatother
\begin{document}
$\sigma_{\ind{xy}}$ % works
$\sigma_\ind{xy}$ % does not work
\end{document}

\textare really an exception to the rule. The proper LaTeX syntax requires braces: if something works without them it's by accident. What makes it not work in your case is the\@ifstartest. – Phelype Oleinik Aug 01 '20 at 00:07\def\ind#1{ { do something with #1 } }works. So perhaps\newcommand\ind[1]{ { \@ifstar \ind@star \ind@nostar #1 } }works. – Symbol 1 Aug 01 '20 at 00:20#1would be a*or a[and the command wouldn't work properly. – Phelype Oleinik Aug 01 '20 at 01:02\sigma_\text{xy}works? That's really unexpected. How come? – Hagen von Eitzen Aug 01 '20 at 16:18\text{xy}eventually expands to (roughly){\hbox{xy}}, so the braces are inserted by the expansion of the command. – Phelype Oleinik Aug 01 '20 at 18:24