4

I've already got an answer for Add parentheses if double superscript. I want to go a step further as proposed below.

However lualatex complains of “missing $” when using the e{_}-type argument…

\documentclass[12pt, a4paper, twoside]{report}

\RequirePackage{xparse}

\ExplSyntaxOn
\cs_generate_variant:Nn \tl_if_in:nnTF { xnTF }

\bool_new:N \l_tmp_ind_a_bool
\bool_new:N \l_tmp_ind_b_bool
\bool_new:N \l_tmp_exp_a_bool
\bool_new:N \l_tmp_exp_b_bool
\NewDocumentCommand{\ajoutParentheses}{me{_}e{^}}{%
    \tl_if_in:xnTF { #1 } { _ }{ \bool_set_true:N \l_tmp_ind_a_bool }{ \bool_set_false:N \l_tmp_ind_a_bool }
    \IfValueTF{ #2 }{ \bool_set_true:N \l_tmp_ind_b_bool }{ \bool_set_false:N \l_tmp_ind_b_bool }
    \tl_if_in:xnTF { #1 } { ^ }{ \bool_set_true:N \l_tmp_exp_a_bool }{ \bool_set_false:N \l_tmp_exp_a_bool }
    \IfValueTF{ #3 }{ \bool_set_true:N \l_tmp_exp_b_bool }{ \bool_set_false:N \l_tmp_exp_b_bool }
    \ensuremath{%
        \bool_if:nTF { (\l_tmp_ind_a_bool && \l_tmp_ind_b_bool) || (\l_tmp_exp_a_bool && \l_tmp_exp_b_bool) } { \left(#1\right) } { #1 }\IfValueT{ #2 }{ _{#2} }\IfValueT{ #3 }{ ^{#3} }%
    }%
}
\ExplSyntaxOff

\begin{document}
    \section{Works}
    \ajoutParentheses{b}
    \ajoutParentheses{b}^2
    \ajoutParentheses{b_0}
    \ajoutParentheses{b_0}^2
    \ajoutParentheses{b^0}
    \ajoutParentheses{b^0}^2

    \section{Doesn't work}
    \ajoutParentheses{b}_1
    \ajoutParentheses{b}_1^2
    \ajoutParentheses{b_0}_1
    \ajoutParentheses{b_0}_1^2
    \ajoutParentheses{b^0}_1
    \ajoutParentheses{b^0}_1^2
\end{document}

1 Answer 1

4

The subscript character _ changes nature inside \ExplSyntaxOn, thus e{_^} should be used outside it.

\documentclass[12pt, a4paper, twoside]{report}

\usepackage{xparse}

\NewDocumentCommand{\ajoutParentheses}{me{_^}}{%
  \ajoutParenthesesAux{#1}{#2}{#3}%
}

\ExplSyntaxOn

\NewDocumentCommand{\ajoutParenthesesAux}{mmm}
 {
  \bool_lazy_and:nnTF { \tl_if_novalue_p:n { #2 } } { \tl_if_novalue_p:n { #3 } }
   {
    #1
   }
   {
    \ajoutparentheses_add:nnn { #1 } { #2 } { #3 }
   }
 }

\bool_new:N \l__ajoutparentheses_sub_bool
\bool_new:N \l__ajoutparentheses_sup_bool
\prg_generate_conditional_variant:Nnn \str_if_in:nn { e } { T,F,TF }

\cs_new_protected:Nn \ajoutparentheses_add:nnn
 {
  \str_if_in:enTF { #1 } { _ }
   { \bool_set_true:N  \l__ajoutparentheses_sub_bool }
   { \bool_set_false:N \l__ajoutparentheses_sub_bool }
  \str_if_in:enTF { #1 } { ^ }
   { \bool_set_true:N  \l__ajoutparentheses_sup_bool }
   { \bool_set_false:N \l__ajoutparentheses_sup_bool }
  \bool_lazy_or:nnTF { \l__ajoutparentheses_sub_bool } { \l__ajoutparentheses_sup_bool }
   {
    (#1)\tl_if_novalue:nF { #2 } { \sb{#2} } \tl_if_novalue:nF { #3 } { \sp{#3} }
   }
   {
    #1\tl_if_novalue:nF { #2 } { \sb{#2} } \tl_if_novalue:nF { #3 } { \sp{#3} }
   }
 }
\ExplSyntaxOff

\begin{document}

$\ajoutParentheses{b}$
$\ajoutParentheses{b}^2$
$\ajoutParentheses{b_0}$
$\ajoutParentheses{b_0}^2$
$\ajoutParentheses{b^0}$
$\ajoutParentheses{b^0}^2$

$\ajoutParentheses{b}_1$
$\ajoutParentheses{b}_1^2$
$\ajoutParentheses{b_0}_1$
$\ajoutParentheses{b_0}_1^2$
$\ajoutParentheses{b^0}_1$
$\ajoutParentheses{b^0}_1^2$

\newcommand{\foo}{f_1}

$\ajoutParentheses{\foo}$
$\ajoutParentheses{\foo}_1$

\end{document}

enter image description here

2
  • I'm at the very beginning with expl3… ;) I've seen (tex.stackexchange.com/a/33263/151808) you aren't fond of \ensuremath: the only modification I suggest is the use of the pair \left( \right).
    – NBur
    Mar 28, 2019 at 12:56
  • 1
    @NBur Sorry, but you won't find useless \left and \right in my answers.
    – egreg
    Mar 28, 2019 at 13:03

You must log in to answer this question.

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