# Add parentheses if double superscript

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{_||^}%
\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.

• Do you plan to use also subscripts in the optional argument to \dans? – egreg Oct 19 '18 at 20:40
• This may occur… At least within the G argument. – NBur Oct 19 '18 at 20:41
• And what should happen with \dans{(x,y)}{\BbbR_+}^2? Do you want parentheses or not? – egreg Oct 19 '18 at 20:42
• Actually I won't have this problem since I choose to denote \BbbR^+ and \BbbR^{+\star}! But \BbbR_+^2 is readable without parenthesis, isn't it? – NBur Oct 19 '18 at 20:47

Using expl3 to check for an ^ inside of the second argument with \tl_if_in:nnTF, one could add the braces based on this test:

\documentclass[11pt]{article}

\usepackage{unicode-math}
\usepackage{xparse}
\ExplSyntaxOn
\cs_new:Npn \NBur_superscript:n #1
{
\tl_if_in:nnTF { #1 } { ^ }
{ \left( #1 \right) }
{ #1 }
}
\NewDocumentCommand{\dans}{mG{\BbbC}e{^}}
{%
\ensuremath
{%
#1\in\IfValueTF{#3}{\NBur_superscript:n{#2}^{#3}}{#2}%
}%
}
\ExplSyntaxOff

\begin{document}
\dans{x}^2,
\dans{x}{A}^2,
\dans{x}{A^*}^2
\end{document}

If you hide the superscript inside a macro above test won't find it. A possible solution would be to fully expand the argument, in order to look for superscript chars then:

\documentclass[11pt]{article}

\usepackage{unicode-math}
\usepackage{xparse}
\newcommand\Test{A^*}
\ExplSyntaxOn
\cs_generate_variant:Nn \tl_if_in:nnTF { xnTF }
\cs_new:Npn \NBur_superscript:n #1
{
\tl_if_in:xnTF { #1 } { ^ }
{ \left( #1 \right) }
{ #1 }
}
\NewDocumentCommand{\dans}{mG{\BbbC}e{^}}
{%
\ensuremath
{%
#1\in\IfValueTF{#3}{\NBur_superscript:n{#2}^{#3}}{#2}%
}%
}
\ExplSyntaxOff

\begin{document}
\dans{x}^2,
\dans{x}{A}^2,
\dans{x}{A^*}^2
\dans{x}{\Test}^2
\end{document}

The above still might fail, e.g. if the superscript is hidden inside something unexpandable or if something shouldn't be expanded there (the former leading to wrong behaviour, the latter possibly to an error or really bad behaviour). The latter should be rare with normal use cases (I hope).

• Nice! However, let say I define \NewDocumentCommand{\BbbNe}{}{\ensuremath{\BbbN^{\star}}}. I've got the double superscript error when calling \dans{(i,j)}{\BbbNe}^2. – NBur Oct 19 '18 at 19:39
• @NBur see my edit. – Skillmon Oct 19 '18 at 19:49
• I really need to dig into expl3: seems very powerful. Can you suggest a good starting doc? – NBur Oct 19 '18 at 19:54
• @NBur texdoc interface3 is a good starting point (and the only thing I use). – Skillmon Oct 19 '18 at 20:09