自定义数学符号 - 两个西格玛和

自定义数学符号 - 两个西格玛和

我希望能够设置一个新的数学命令/宏,即一个 Sigma 稍微移动(对角线)在另一个 Sigma 之上,但不确定如何操作。我希望它在方程式中的上下限以及内联时的侧面方面表现得与单个 Sigma 一样。如果更简单,我真的只关心方程式的行为。符号的描述如下: \总和

答案1

最简单的方法是用堆栈。

已编辑以保留 s 的数学样式\sum

\documentclass{article}
\usepackage{stackengine,scalerel}
\newcommand\doublesum{\mathop{\ThisStyle{\ensurestackMath{%
  \stackengine{.3ex}{\SavedStyle\,\sum}{\SavedStyle\sum}{O}{l}{F}{F}{L}}}}}
\begin{document}
\centering
\[
  \doublesum_{ij}^{ND} x_{ij} = 0
\]
\(
  \doublesum_{ij}^{ND} x_{ij} = 0
\)
\end{document}

在此处输入图片描述

当然,也可以选择在相应的索引和上限之间放置逗号或小间隙。或者,可以采用更复杂的操作来垂直交错索引,这些索引现在作为四个强制参数传递:

\documentclass{article}
\usepackage{stackengine,scalerel,amsmath}
\newcommand\doublesum[4]{\ThisStyle{\ensurestackMath{\mathop{%
  {\stackengine{.3ex}{\SavedStyle\,\sum}{\SavedStyle\sum}{O}{l}{F}{F}{L}}}
    _{\stackengine{.3ex}{\SavedStyle_{\phantom{#1}#2}}{%
                         \SavedStyle_{#1}}{O}{l}{F}{F}{L}}
    ^{\stackengine{.3ex}{\SavedStyle_{#3}}{%
                         \SavedStyle_{\phantom{#3}#4}}{U}{l}{F}{F}{L}}}}}
\begin{document}
\centering
\[
  \doublesum{i}{j}{N}{D} x_{ij} = 0
\]
\(
  \doublesum{i}{j}{N}{D} x_{ij} = 0
\)
\end{document}

在此处输入图片描述

答案2

在我看来,输出结果非常糟糕。

\documentclass{article}
\usepackage{amsmath}

\makeatletter
% the user level command, respecting options to amsmath such as 'nosumlimits'
\newcommand\doublesum{\DOTSB\doublesum@\slimits@}
% the lower level command, robust so it won't break in moving arguments
\DeclareRobustCommand{\doublesum@}{%
  \mathop{%
    \vphantom{\sum@}% the same dimensions as \sum
    \smash{\mathpalette\doublesum@@\relax}% delegate to \mathpalette
  }%
}
% the preceding command delegates to \mathpalette
\newcommand{\doublesum@@}[2]{%
  % vertically center the whole
  \vcenter{\hbox{%
    % \ooalign superimposes the rows (separated by \cr)
    \ooalign{%
      % first row with some more space on the right
      $\m@th#1\sum@\doublesum@kern{#1}$\cr
      % a vertical adjustment based on the current math style
      \noalign{%
        \sbox\z@{$\m@th#1\mkern2mu$}%
        \kern\wd\z@
      }
      % second row with some more space on the left
      $\m@th#1\doublesum@kern{#1}\sum@$\cr
    }%
  }}%
}
\newcommand{\doublesum@kern}[1]{%
  % the kern should be greater in \displaystyle
  \mkern\ifx#1\displaystyle 4\else 2\fi mu
}
\makeatother

\begin{document}

$
\displaystyle\doublesum_{i,j}^{N}
\textstyle\doublesum_{i,j}^{N}
\scriptstyle\doublesum_{i,j}^{N}
\scriptscriptstyle\doublesum_{i,j}^{N}
$

$\displaystyle\doublesum_{i,j}^{N}\sum_{i,j}^{N}$
\end{document}

在此处输入图片描述

\mathpalette 的奥秘更多信息\mathpalette

答案3

像往常一样,我更喜欢采用老办法:这是一个经典的解决方案\mathpalette。现在已经更新改善与amsmath包的集成:我很高兴感谢@egreg提醒我这一点,并允许我从中复制一些代码他的回答一如既往地出色

% 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{amsmath} % even if it not strictly necessary...

\makeatletter

\newcommand*\doublesum{\DOTSB\doublesum@x}
\DeclareRobustCommand*\doublesum@x{\mathop{\mathpalette\doublesum@y{}}\slimits@}
\newcommand*\doublesum@y[2]{%
    \vcenter{%
        \setbox\z@ \hbox{$\m@th #1\sum$}%
        \baselineskip .2\ht\z@
        \lineskiplimit -\maxdimen
        \copy\z@
        \moveright .2\wd\z@ \box\z@
    }
}

\makeatother

% Check robustness:
\typeout{\doublesum.} % expected output: "\DOTSB \doublesum@x ."



\begin{document}

Text before.
\[
  \doublesum_{ij}^{ND} x_{ij} \neq \sum_{ij}^{ND} x_{ij}
\]
Text after: \( \doublesum_{ij}^{ND} x_{ij} = 0 \).
Now in \verb|\scriptstyle|: \( \frac{\doublesum_{i\in I} x_{i}}{2} \).

\end{document}

以下是我得到的打印内容:

代码输出

答案4

如何在纯 TeX 中执行此操作:

\input ams-math  % for \scripstyle\sum

\newcount\stylenum 
\def\varstyle#1{\mathchoice{\stylenum=0 #1}{\stylenum=1 #1}{\stylenum=2 #1}{\stylenum=3 #1}}
\def\usestyle{\ifcase\stylenum\displaystyle\or\textstyle\or\scriptstyle\or\scriptscriptstyle\fi}
\def\dbsumR{\ifcase\stylenum .35\or.25\or.21\or.15\fi em}
\def\dbsumU{\ifcase\stylenum .15\or.12\or.09\or.07\fi em}
\def\doublesum{\mathop{\varstyle{%
   \raise\dbsumU\rlap{$\usestyle\sum$}{\kern\dbsumR\sum}}}}

$\displaystyle \doublesum_{ij}^{DN} \textstyle \doublesum_{ij}^{DN}
\scriptstyle \doublesum_{ij}^{DN} \scriptscriptstyle \doublesum_{ij}^{DN}$

\bye

相关内容