在显示和文本模式下使用素数和运算符

在显示和文本模式下使用素数和运算符

我想定义一个已涂上底漆sum 命令产生\sump如下结果\sump_{x \in X}

  • 在显示模式下,右上角和x \in Xsigma 下方有一个素数,以及
  • 在文本模式下,右上角有一个素数,而x \in Xsigma 的右下角有一个素数。

我尝试过\mathchoice和的组合\sideset来实现这一点,但至今还没有成功。

如果有任何区别,我不仅想用总和来做到这一点,而且还想定义 primed和\prod命令。\min\max

当然,可以定义\sumpx \in X作为参数,然后使用\mathchoice和正确放置它\sideset,但是为了编写更具可读性且包含正常(非素数)总和的代码,我宁愿不这样做。帮忙?

如果人们遵循建议中最被接受的答案如何创建自己的具有限制的数学运算符?,那么在文本模式下使用时,下限\sump_{x \in X}之前会有一个不必要的空格x \in X;撇号会将运算符的右边界推出去,这样下限就不会从 旁边开始Sigma

答案1

您可以借助_以下\sump方法进行测试xparse

\documentclass{article}
\usepackage{amsmath,mathtools}
\usepackage{xparse}

\makeatletter
\NewDocumentCommand{\sump}{e{_}}
 {%
  \DOTSB
  \mathop{\IfNoValueTF{#1}{\sump@{}}{\sump@{#1}}}%
  \nolimits
 }
\newcommand{\sump@}[1]{\mathpalette\sump@@{#1}}
\newcommand{\sump@@}[2]{%
  \ifx#1\displaystyle
    {\sump@display{#2}}%
  \else
    \sum@\nolimits'_{#2}%
  \fi
}
\newcommand{\sump@display}[1]{%
  \sbox\z@{$\m@th\displaystyle\sum@\nolimits'$}%
  \sbox\tw@{$\m@th\displaystyle\sum@\limits_{#1}$}%
  \sbox\@tempboxa{$\m@th\displaystyle'$}
  \mathop{\sum@\nolimits' \kern-\wd\@tempboxa}\limits_{#1}%
  \ifdim\wd\z@>\wd\tw@
    \kern\dimexpr\wd\z@-\wd\tw@\relax
  \fi
}
\makeatother

\begin{document}

\begin{align}
&\sum_{n} a_n &&\textstyle \sum_{n\ge0} a_n \\
&\sump_{n} a_n &&\textstyle \sump_{n\ge0} a_n \\
&\sum_{n\ge0} a_n &&\textstyle \sum_{n\ge0} a_n \\
&\sump_{n\ge0} a_n &&\textstyle \sump_{n\ge0} a_n \\
&\sum_{1\le n\le 32} a_n &&\textstyle \sum_{1\le n\le 32} a_n \\
&\sump_{1\le n\le 32} a_n &&\textstyle \sump_{1\le n\le 32} a_n
\end{align}

\end{document}

在此处输入图片描述

在“非显示”情况下,只需排版\sum'_{#1},在显示情况下,我测量素数,以便调整字距并生成以求和符号为中心的下标。然后,我测量带素数的和和带下标的和,以决定是否需要进行一些字距调整,如果“带素数的和”的大小超过“带下标的和”的大小,则需要进行字距调整。

答案2

正如评论中所说,这个问题是练习 18.44 的主要主题TeXbook. Barbara Beeton 已经建议这是 Knuth 在附录 A 中提出的两个解决方案之一的变体,尽管她的代码应该或多或少地修改为:

\DeclareRobustCommand*{\sump}{%
    \mathop{{\sum}^{\mathrlap{\prime}}}%
}

这是一个完整的、可编译的示例,它还提请注意该解决方案可能存在的缺陷:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{mathtools}

\newcommand*{\sump}{} % check that the name can be used
\DeclareRobustCommand*{\sump}{%
    \mathop{{\sum}^{\mathrlap{\prime}}}%
}



\begin{document}

First in-line:
\( \sump_{i\in I}a_{i} \ne \sum_{i\in I}a_{i} \).
Then in display:
\[
    \sump_{i\in I}a_{i} \ne \sum_{i\in I}a_{i}
    \mbox{.}
\]

You can also say, for instance,
\( \sump\limits_{i\in I}a_{i} \ne \sum\limits_{i\in I}a_{i} \)
and
\[
    \sump\nolimits_{i\in I}a_{i} \ne \sum\nolimits_{i\in I}a_{i}
    \mbox{,}
\]
respectively.

There's a caveat, though: because the prime is not taken into account
when computing the width of the \verb|\sump| symbol, it may bump into
adjacent characters:
\[
    \sump \biggl[\frac{a+b}{c+d}-\frac{x+y}{x-y}\biggr]
        \ne \sum \biggl[\frac{a+b}{c+d}-\frac{x+y}{x-y}\biggr]
\]
The clash is more problematic in in-line math:
\( \sump[a+b] \ne \sum[a+b] \).
However, in practice this is not going to be a problem if you stick to
using the \verb|\sump| symbol only with a (non-empty) subscript.

\end{document}

然而,如果我们必须加载该mathtools包,而这又需要amsmath,我认为我们也应该支持后者的 [ no]sumlimits选项,以及它的\dots功能:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

% Try decommenting the following line:
% \usepackage[nosumlimits]{amsmath}
\usepackage{mathtools}

\makeatletter

% Cannot directly use "\DeclareRobustCommand" with "\DOTSB":
\@ifdefinable\sump@{\DeclareRobustCommand*\sump@{%
    \mathop{{\sum}^{\mathrlap{\prime}}}%
}}
\newcommand*\sump{%
    \DOTSB\sump@\slimits@ % ...or move "\slimits@" into "\sump@"
}

\makeatother



\begin{document}

First in-line:
\( \sump_{i\in I}a_{i} \ne \sum_{i\in I}a_{i} \).
Then in display:
\begin{equation}
    \sump_{i\in I}a_{i} \ne \sum_{i\in I}a_{i}
    \mbox{.}
    \label{eq:displaylimits}
\end{equation}

You can also say, for instance,
\( \sump\limits_{i\in I}a_{i} \ne \sum\limits_{i\in I}a_{i} \)
and
\[
    \sump\nolimits_{i\in I}a_{i} \ne \sum\nolimits_{i\in I}a_{i}
    \mbox{,}
\]
respectively.  And see what happens in the ``default'' case (that is,
in~\eqref{eq:displaylimits}) if you load the \textsf{amsmath} package
with the \texttt{nosumlimits} option.

Finally, note that
\( \sump_{i_{1}}\dots\sump_{i_{p}} x_{i_{1}}\otimes\dots\otimes x_{i_{p}} \)
works exactly in the same way as
\( \sum_{i_{1}}\dots\sum_{i_{p}} x_{i_{1}}\otimes\dots\otimes x_{i_{p}} \)
(as one would expect).  Let's repeat it in display:
\[
    \sump_{i_{1}}\dots\sump_{i_{p}} x_{i_{1}}\otimes\dots\otimes x_{i_{p}}
    \ne
    \sum_{i_{1}}\dots\sum_{i_{p}} x_{i_{1}}\otimes\dots\otimes x_{i_{p}}
\]
(this way the formula is more readable!).

\typeout{A test for robustness: \sump}
\typeout{Compare the above with \sum}

\end{document}

请注意,上述代码(第二个 MWE)在编译期间写入了几条消息,这些消息说明了如何将命令\sump写出到辅助文件中,并表明其在这方面的行为与 的行为相似\sum

相关内容