从...开始这里,我想要一个根据作为参数传递的元素数量添加上标的函数。如下所示。
如您所见,当集合中已经有一个上标时,会添加不需要的括号。我猜我的代码存在扩展问题。任何帮助都非常感谢!
% !TeX program = lualatex
% !BIB TS-program = biber
% !TeX encoding = utf8
% !TeX spellcheck = fr_FR
\documentclass{memoir}
\usepackage{unicode-math}
\usepackage{booktabs}
% tiré de https://tex.stackexchange.com/a/481888/151808
\NewDocumentCommand{\ajoutParentheses}{me{_^}}{\ensuremath{\ajoutParenthesesAux{#1}{#2}{#3}}} % adds parentheses around #1 if needed
\NewDocumentCommand{\liste}{d()}{\ensuremath{\left(\formatListe { #1 }[ , ]\right)}} % (⋅,⋅,⋅) typesets list
% typesets #2 \in [or \notin if #1] #3_{#4}^{#5} adding parentheses around #2 if list and around #3 if needed
\NewDocumentCommand{\dans}{smD(){\BbbR}e{_^}}{\ensuremath{\ajoutParenthesesListe{#2}\IfBooleanTF{#1}{\notin}{\in}\IfValueTF{#5}{\ajoutParenthesesAux{#3}{#4}{#5}}{\ajoutParenthesesAux{#3}{#4}{\nbElmts{#2}}}}}
\def\pe{+\star}
\def\BbbRpe{\ensuremath{\BbbR^{\pe}}} % R plus étoile
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\IfNoValueOrEmptyTF}{mmm}
{
\IfNoValueTF{#1}{#2}
{
\tl_if_empty:nTF {#1} {#2} {#3}
}
}
\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
\bool_new:N \l__sub_bool
\bool_new:N \l__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 }
% \cs_log:N {1: ~ #1}
% \cs_log:N {2: ~ #2}
% \cs_log:N {3: ~ #3}
% \cs_log:N \l__ajoutparentheses_sub_bool
% \cs_log:N \l__ajoutparentheses_sup_bool
% \fbox{#3} \tl_if_blank:oTF { #3 } {=blanc} {≠blanc}\tl_if_empty:oTF { #3 } {=vide} {≠vide}\IfNoValueOrEmptyTF{ #3 }{NoV ~ ou ~ vide}{pas ~ NoV ~ ou ~ vide}
\IfNoValueOrEmptyTF{ #2 }{ \bool_set_false:N \l__sub_bool }{ \bool_set_true:N \l__sub_bool }
\IfNoValueOrEmptyTF{ #3 }{ \bool_set_false:N \l__sup_bool }{ \bool_set_true:N \l__sup_bool }
% \cs_log:N \l__sub_bool
% \cs_log:N \l__sup_bool
\bool_lazy_and:nnTF { \bool_lazy_or_p:nn { \l__ajoutparentheses_sub_bool }{ \l__ajoutparentheses_sup_bool } } { \bool_lazy_or_p:nn { \l__sub_bool }{ \l__sup_bool } }
{
\left(#1\right)\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} }
}
}
\NewDocumentCommand{\nbElmts}{m}{
\clist_clear_new:N \l__estia_clist
\clist_set:Nn \l__estia_clist { #1 }
\int_compare:nNnTF { \clist_count:N \l__estia_clist } > 1 { \clist_count:N \l__estia_clist } { \c_empty_tl }
}
\NewDocumentCommand{\ajoutParenthesesListe}{m}{
\clist_clear_new:N \l__estia_clist
\clist_set:Nn \l__estia_clist { #1 }
\int_compare:nNnTF { \clist_count:N \l__estia_clist } > 1 { \liste(#1) } { #1 }
}
%\NewDocumentCommand{\dans}{smD(){\BbbR}e{_^}}{ % doesn't work since “e” type argument is changed within \ExplSyntax
% \tl_clear:N \l_tmpa_tl\l_tmpa_tl
% \IfValueTF{#5}{\tl_set:Nv \l_tmpa_tl { #5 }}{
% \clist_clear_new:N \l__estia_clist
% \clist_set:Nn \l__estia_clist { #2 }\tl_set:Nn \l_tmpa_tl {
% \int_compare:nNnTF { \clist_count:N \l__estia_clist } > 1 { \clist_count:N \l__estia_clist } {}
% }}
% % \tl_set:Nn \l_tmpa_tl { 5 }
% % \cs_log:N {#5 ~ \nbElmts{#2} ~ \l_tmpa_tl}
% \ensuremath{\ajoutParenthesesListe{#2}
% % \IfBooleanTF{#1}{\notin}{\in}\IfValueTF{#5}{\ajoutParenthesesAux{#3}{#4}{#5}}{\ajoutParenthesesAux{#3}{#4}{\nbElmts{#2}}}}}
% \IfBooleanTF{#1}{\notin}{\in}\ajoutParenthesesAux{#3}{#4}{\l_tmpa_tl}}}
\cs_new_protected:Npn \l_estia_formatListe:nnnnn #1#2#3#4#5
{
\clist_clear_new:N \l__estia_clist
\clist_map_inline:nn { #1 }
{
\str_if_eq:noTF { … } { \tl_to_str:n { ##1 } }
{ \clist_put_right:Nn \l__estia_clist { ##1 } }
{ \clist_put_right:Nn \l__estia_clist { #5 } }
}
\clist_use:Nnnn \l__estia_clist { #2 } { #3 } { #4 }
}
\NewDocumentCommand{\formatListe}{mO{\,;}ooD(){##1}}{
\l_estia_formatListe:nnnnn { #1 }{ \IfNoValueOrEmptyTF{#2}{\,;}{#2} } { \IfNoValueOrEmptyTF{#3}{#2}{#3} } { \IfNoValueOrEmptyTF{#4}{#2}{#4} } { #5 }
}
\ExplSyntaxOff
\begin{document}
\begin{tabular}{@{}lccr@{}}
\toprule
{\bfseries Cmd} & {\bfseries Output} & {\bfseries Should be}&\\
\midrule
\verb*|\ajoutParentheses{aze}_1^2| & \ajoutParentheses{aze}_1^2 & $aze_1^2$ & right\\
\verb*|\ajoutParentheses{aze_1}_1^2| & \ajoutParentheses{aze_1}_1^2 & $\left(aze_1\right)_1^2$ & right\\
\verb*|\dans{x}| & \dans{x} & $x\in\BbbR$ & right\\
\verb*|\dans{x}(\BbbRpe)| & \dans{x}(\BbbRpe) & $x\in\BbbRpe$ & {\bfseries wrong}\\
\verb*|\dans{x_1}| & \dans{x_1} & $x_1\in\BbbR$ & right\\
\verb*|\dans{x_1}(\BbbRpe)| & \dans{x_1}(\BbbRpe) & $x_1\in\BbbRpe$ & {\bfseries wrong}\\
\verb*|\dans{x, y}| & \dans{x, y} & $\left(x, y\right)\in\BbbR^2$ & right\\
\verb*|\dans{x, y}(\BbbRpe)| & \dans{x, y}(\BbbRpe) & $\left(x, y\right)\in\left(\BbbRpe\right)^2$ & right\\
\verb*|\dans{U}^3| & \dans{U}^3 & $U\in\BbbR^3$ & right\\
\verb*|\dans{U, V}(\BbbR^3)| & \dans{U, V}(\BbbR^3) & $\left(U, V\right)\in\left(\BbbR^3\right)^2$ & right\\
\bottomrule
\end{tabular}
\end{document}