我尝试定义的命令如下:
\lowint{f}[a,b}{\alpha}
预期:
当第二个参数为空时,预期结果如下:
我注意到使用\underline{\int}
会产生奇怪的结果。无论哪种方式,我的代码是这样的:
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand\lowint{m D[]{} m}%
{
\IfNoValueTF{#2}
{
\underline{\int} #1\,#3
}
{
\clist_set:Nn \l_lims_clist{ #1 }
\tl_set:Nn \l_low_tl {\clist_item:Nn \l_lims_clist{1}}
\tl_set:Nn \l_up_tl {\clist_item:Nn \l_lims_clist{2}}
\underline{\int}
\limits
\c_math_subscript_token { \l_inf_tl }
\c_math_superscript_token { \l_sup_tl }
#1\,#3
}
}
\ExplSyntaxOff
\begin{document}
\[
\lowint{f}[a,b]{\alpha}
\]
\end{document}
我遇到的错误如下:
Limit controls must follow a math operator.
<argument> ...clist {2}}\underline {\int }\limits
Undefined control sequence.
<argument> ... \c_math_subscript_token {\l_inf_tl
Undefined control sequence.
<argument> ...c_math_superscript_token {\l_sup_tl
我不太了解 latex3 代码,不知道可以在其中使用什么“正常”语法。
答案1
使用https://tex.stackexchange.com/a/711998/4427无需检查可选参数是否为空就可以解决这个问题。
以下是代码:
\documentclass{article}
\usepackage{amsmath}
\makeatletter
\NewDocumentCommand{\lowerint}{}{\mathop{}\mathpalette\lowerint@\relax\!\int}
\NewDocumentCommand{\upperint}{}{\mathop{}\mathpalette\upperint@\relax\!\int}
\newcommand{\lowerint@}[2]{%
\begingroup
\sbox\z@{$\m@th#1\int$}%
\lowup@{l}{\underline}{#1}%
\endgroup
}
\newcommand{\upperint@}[2]{%
\begingroup
\sbox\z@{$\m@th#1\int$}%
\lowup@{r}{\overline}{#1}%
\endgroup
}
\newcommand{\lowup@}[3]{%
% #1 = l (lower) or r (upper)
% #2 = \underline (lower) or \overline (upper)
% #3 = math style
\rlap{%
\hspace{0.05\wd\z@}%
\makebox[0.9\wd\z@][#1]{%
$\m@th#2{%
\hspace{0.4\wd\z@}%
\ifx#3\displaystyle\else\hspace{0.1\wd\z@}\fi
\vphantom{\copy\z@}%
}$%
}%
\hspace{0.05\wd\z@}%
}%
}
\makeatother
\ExplSyntaxOn
\NewDocumentCommand\lowint{m D[]{} m}%
{
\clist_set:Nn \l_lims_clist { #2 }
\tl_set:Nn \l_inf_tl {\clist_item:Nn \l_lims_clist {1} }
\tl_set:Nn \l_sup_tl {\clist_item:Nn \l_lims_clist {2} }
\lowerint
^{\l_sup_tl}
\sb{\l_inf_tl}
#1\,\mathrm{d}#3
}
\ExplSyntaxOff
\begin{document}
\[
\lowerint f \,\mathrm{d}\alpha
\]
\[
\lowint{f}[a,b]{\alpha}
\]
\[
\lowint{f}{\alpha}
\]
\end{document}
输出如下: