Having a \dsum like we can have a \dfrac
In almost all cases the third setting is preferable to the first two
\documentclass[12pt]{article}
\usepackage{xparse}
\NewDocumentCommand\dsumx{e{_^}}{{\displaystyle\sum_{#1}^{#2}}}
\newcommand\dsuml{\sum\limits}
\begin{document}
xparse\\
$\dsumx_{k=1}^{n} 2^k = \sum_{k=1}^{n} 2^k = 2^{n+1} - 1$
limits\\
$\dsuml_{k=1}^{n} 2^k = \sum_{k=1}^{n} 2^k = 2^{n+1} - 1$
standard
\[\sum_{k=1}^{n} 2^k = \sum_{k=1}^{n} 2^k = 2^{n+1} - 1\]
\end{document}
The first uses the display summation which is inappropriate for inline math.
The second uses the correct summation but the limits setting will still make the expression too tall to fit in an inline setting.
The third sets the expression as a display with appropriate summation layouts just using the standard \sum
.
You can use xparse
's e
type parameter to parse it
This allows you to parse both \dsum_{k=1}^{n}
and \dsum^{n}_{k=1}
.
Notes:
- The extra brace group to ensure that
\displaystyle
stays local. - The
e{^_}
provides two arguments,#
1 is the one given to^
and#2
is the one given to_
(independent of the order they are provided in). If no parameters of either type are given\IfValueT{}
will not execute its parameter.\IfValueT{}
is a more efficient form of\IfValueTF{}{}
and is useful when you only really need to do something under one condition.
Code:
\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\dsum}{%
e{^_}
}{%
{%
\displaystyle\sum
\IfValueT{#1}{^{#1}}
\IfValueT{#2}{_{#2}}
}
}%
\begin{document}
$\sum_{k=1}^{n} 2^k = \dsum_{k=1}^{n} 2^k = \dsum^{n}_{k=1} 2^k = 2^{n+1} - 1$ .
\end{document}
I provide \dsum
and \Dsum
, depending on how you want the look of the \textstyle
limits.
\documentclass{article}
\usepackage{mathtools}
\DeclareMathSymbol{\Xdsum}{\mathop}{largesymbols}{88}
\DeclareMathSymbol{\Xtsum}{\mathop}{largesymbols}{80}
\DeclareMathOperator*{\dsum}{\mathchoice{\Xdsum}{\Xdsum}{\Xtsum}{\Xtsum}}
\newcommand\Dsum{\dsum\limits}
\begin{document}
\centering
Sum and defined dsum and Dsum:\par
$\sum_{i=1}^2 x_i\quad \dsum_{i=1}^2 x_i \quad \Dsum_{i=1}^2 x_i $
\[\sum_{i=1}^2 x_i\quad \dsum_{i=1}^2 x_i \quad \Dsum_{i=1}^2 x_i\]
\end{document}
GuM asks that I comment on why I use the star version of \DeclareMathOperator
. It is to designate that, in \displaystyle
, that limits are placed above and below the operator, rather than placed as super/sub-scripts. If you remove the *
you will see all limits in \dsum
and \Dsum
revert to super/sub-script limits.