如果是双上标或双下标,则添加括号

如果是双上标或双下标,则添加括号

我已经知道答案了如果双上标,请添加括号。我想按照下面的建议更进一步。

然而,lualatex使用 -type 参数时会抱怨“缺少 $” e{_}......

\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

下标字符_在 内部发生性质变化\ExplSyntaxOn,因此e{_^}应在其外部使用。

\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}

在此处输入图片描述

相关内容