I'd like to define a command which expands to { <output> } instead of <output>. I've tried wrapping the command contents with additional
{/}\bgroup/\egroup\begingroup/\endgroup\group_begin:/\group_end:\c_group_begin_token/\c_group_end_token\str_use:N \c_left_brace_str/\str_use:N \c_right_brace_str
but to no avail.
My use case: I'd like to be able to write \R[1][2] instead of \mathbb{R}^{1 \times 2}, but also be able to use this in subscripts and superscripts, like 0_\R[1][2], without needing to surround it with braces. I know, this is rather specific and maybe lazy, but I'm honestly also just curious about this from a TeXnical point of view.
Simplified MWE:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\DeclareDocumentCommand { \R } { o }
{
\mathbf { R }
\IfValueT { #1 } { \sp { #1 } }
}
\DeclareDocumentCommand { \Q } { m }
{
\mathbf { Q } \sp { #1 }
}
\ExplSyntaxOff
\begin{document}
$0_\R$ % throws error "Missing } inserted"
$0_\R[2]$ % throws error "Missing } inserted"
$0_\Q{2}$ % expands to $0_\mathbf{Q}^{2}$, not $0_{\mathbf{Q}^{2}}$
\end{document}


x_\text{aaa}sort of half work and don't give an error – David Carlisle Jan 26 '24 at 17:57o_\R...work\Rneeds to expand to a brace group, so it's quite hard to do what you want but would be trivial but bad practice if\Rhad a mandatory{}argument rather than[]– David Carlisle Jan 26 '24 at 18:04x_\text{aaa}sort of "half" works, are you just referring to the fact that it's not good practice (https://tex.stackexchange.com/a/82338/170958), or is there actually any "functional" difference betweenx_\text{aaa}andx_{\text{aaa}}, specifically? My understanding was that those code snippets would be equivalent tox\sb\text{aaa}andx\sb{\text{aaa}}, respectively, and that those two are essentially equivalent to one another (https://tex.stackexchange.com/a/1929/170958). – steve Jan 27 '24 at 01:04\fbox\text{aaa}it has to be\fbox{\text{aaa}}so if anything tries to redefine ^ as an active macro with a normal#1style argument, it'll fail. and it's a bad habit to get in to, if you have\def\foo{abc}and do\fbox\foothat is\fbox{abc}butx^\fooisx^{a}bcas^expands tokens looking for{(which is why the answers work) but is almost never what you want andx^{abc}was intended but you get the wrong thing with no warning – David Carlisle Jan 27 '24 at 02:29