I'd like to set up a macro able to add parenthesis if the argument has already one superscript. It similar to this question, unfortunately the compilation of the following MWE doesn't end…
\documentclass[11pt]{article}
\usepackage{xparse}
\RequirePackage{unicode-math}
\usepackage{listofitems}
\NewDocumentCommand{\dans}{mG{\BbbR}e{^}}{%
\ensuremath{#1\in\IfValueTF{#3}{{\left(#2\right)}^{#3}}{#2}}%
}
\NewDocumentCommand{\Dans}{mG{\BbbR}e{^}}{%
\setsepchar{_||^}%
\readlist\mymat{#2}%
\ensuremath{#1\in\IfValueTF{#3}{\ifnum\mymatlen>1\left(#2\right)^{#3}\else#2^{#3}\fi}{#2}}%
}
\begin{document}
\dans{x}, \dans{(x, y)}{\BbbC^2}, \dans{(x, y)}{\BbbC}^2, \dans{(x, y)}{\BbbC^\star}^2
%\Dans{x}, \Dans{(x, y)}{\BbbC^2}, \Dans{(x, y)}{\BbbC}^2, \Dans{(x, y)}{\BbbC^\star}^2
\end{document}
Problem arises when uncommenting the line.



\dans? – egreg Oct 19 '18 at 20:40Gargument. – NBur Oct 19 '18 at 20:41\dans{(x,y)}{\BbbR_+}^2? Do you want parentheses or not? – egreg Oct 19 '18 at 20:42\BbbR^+and\BbbR^{+\star}! But\BbbR_+^2is readable without parenthesis, isn't it? – NBur Oct 19 '18 at 20:47