3

I want to write a macro which makes a transformation for the sum symbol.

When i write:

\csum_{i = 1000000}^{n}

it should be converted to this:

\sum_{\mathclap{i = 1000000}}^{n} \;

How can i do that?

  • Why \mathclap? – percusse Jan 22 '15 at 17:15
  • because it looks much better if you have a long subscript – user3613886 Jan 22 '15 at 17:17
  • I've been wondering for a while it there would be an easy & automated system to do this with xparse, some sort of auto implementing t_ g t^ g automatically. With an interface so that actually the sup- and subscript are treated like optional arguments, hence easier to change the behaviour of them. – Manuel Jan 22 '15 at 17:18
  • How about an interface like \csum{_{<sub>}^{sup}}? – Werner Jan 22 '15 at 17:52
4

Something like this???

I am not fond of the _^ syntax of \sum at all, so I dropped this.

The optional argument to \csum is set to the counting index i if omitted.

I kept the \mathclap.

\documentclass{article}

\usepackage{xparse}
\usepackage{mathtools}


\NewDocumentCommand{\csum}{+O{i}+m+m}{%
\ensuremath{\sum\limits_{\mathclap{#1=#2}}^{#3}}%
}%


\begin{document}
\[ \csum{100000}{m} = 17 \]

\[ \csum[k]{100000}{m} = 17 \]
\end{document}

enter image description here

Edit -- Version with optional 3rd argument instead of mandatory

The great benefit from xparse and its macros like \NewDocumentCommand is the better argument handling compared to \newcommand. It's quite easy to define the superscript argument to be optional:

\csum[summation variable]{start value}[upper limit]

Since \sum\limits_{} etc. works without ^{} a default optional empty argument value is possible.

\documentclass{article}

\usepackage{xparse}
\usepackage{mathtools}

\NewDocumentCommand{\csum}{+O{i}+m+O{}}{%
\ensuremath{\sum\limits_{\mathclap{#1=#2}}^{#3}}%
}%


\begin{document}
\[ \csum{100000}[m] = 17 \]

\[ \csum[k]{1}[N] = \dfrac{N \left({N+1}\right)}{2} \]

\[ \csum[k]{1} k = 5050 \]

\end{document}

enter image description here

  • i think this is a more robust approach. How can i change this that \csum{}{} and \csum{} works depending if i want a superscript or not? – user3613886 Jan 22 '15 at 19:39
  • @user3613886: See my update please – user31729 Jan 22 '15 at 21:35
3

Edit

In case you are definitely going to have sub- and superscript after every \csum, always… in that case it's easier, I think:

\def\csum_#1^#2{\sum_{\mathclap{#1}}^{#2}}

But you always have to input it with both of them.

In case you want to be able to say, sometimes \csum x_i, here's a very fragile approach.

Original

Very fragile, but a first approach

\documentclass{scrartcl}
\usepackage{mathtools,xparse}

\NewDocumentCommand\csum{t_ g t^ g}
 {\sum
  \IfBooleanT{#1}
    {_{\mathclap{#2}}}
  \IfBooleanT{#3}
    {^{#4}}
  \IfBooleanTF{#1}{\;}{\IfBooleanT{#3}{\;}}}

\begin{document}
First, what we want,
\[
  \sum_{i = 1000000}^{n} x_i \quad \text{should change to} \quad \sum_{\mathclap{i = 1000000}}^{n} \; x_i
\]
And here we try with our new fragile-defined-easily-breakable command
\[
  \csum_{i = 1000000}^{n} x_i \qquad \text{Hurray!}
\]
\end{document}

enter image description here

Obviously, limitations: you can't have a brace following \sum directly (like \sum {x_n}; the sub-superscripts must be braced in case of been given (e.g., this wouldn't work: \sum_{i=1}^n you have to \sum_{i=1}^{n}.

It would be nice that in the future, an expl3 package that provides math macros would also give a more “robust” way of configuring sub and subscripts. It's easy to define such macros with optional arguments, but it's still nice to have \sum_i^\infty with _ and ^ tokens. So we could, with an interface, prepend code to subscripts, or may be add a predefined kern to such superscript, etc.

  • yes it would be great if _ and ^ would work always, thank you anyway. – user3613886 Jan 22 '15 at 19:42
  • They work always. The think is, in the first option, in case you don't want a superscript, you still need to write ^{}, e.g., if you want \csum_{i} x_i you still need \csum_{i}^{} (but thinking about it, in that case you would be better using \sum directly). – Manuel Jan 22 '15 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.