1

Is there any way to define a command like:

\newcommand{\mysum}[2]{\sum\limits_{#1}^{#2}}

where the two arguments are separated by a comma instead of brackets?

So for example, I'd like to have:

\documentclass[12pt, letterpaper]{article}
\usepackage{amsmath}
%yet to be defined command%

\begin{document} The sum given by $a=\mysum{n=1, \infty}$ \end{document}

Return the same thing as:

\documentclass[12pt, letterpaper]{article}
\usepackage{amsmath}

\begin{document} The sum given by $a=\sum\limits_{n=1}^{\infty}$ \end{document}

EDS
  • 159

2 Answers2

3

expl3's \clist_item:nn might be your friend:

\documentclass[12pt, letterpaper]{article}
\usepackage{amsmath}
\ExplSyntaxOn
\cs_new:Npn \mysum #1 {\sum\limits\sb{\clist_item:nn{#1}{1}}\sp{\clist_item:nn{#1}{2}}} 
\ExplSyntaxOff

\begin{document} The sum given by $a=\mysum{n=1, \infty}$

The sum given by $a=\sum\limits_{n=1}^{\infty}$ \end{document}

enter image description here

Ulrich Diez
  • 28,770
2

I'd not add \limits unconditionally and use a *-variant if you need it.

\documentclass{article}
\usepackage{amsmath}

\NewDocumentCommand{\mysum}{s >{\SplitArgument{1}{,}}m}{% \sum \IfBooleanT{#1}{\limits}% \mysumaux#2% } \NewDocumentCommand{\mysumaux}{mm}{_{#1}\IfValueT{#2}{^{#2}}}

\begin{document}

$\mysum{n=1,\infty}$ is good.

$\mysum*{n=1,\infty}$ is bad. [ \mysum{n=1,\infty}\qquad \mysum{n\ge0} ]

\end{document}

You see that a single item in the argument is correctly treated as a subscript.

enter image description here

egreg
  • 1,121,712