# How to define a new math operator with \mathclap in its arguments?

I would like to define a math operator with \mathop and I would like its arguments to be automatically put in a zero width box with \mathclap. Defining an operator, e.g. \sum, with \newcommand

\newcommand{\Sum}[2]{\sum_{\mathclap{#1}}^{\mathclap{#2}}}


would do, but it would force me to provide every time two arguments. If I define the new command with an optional argument, that wouldn't solve the matter, since the mandatory argument is still to be provided (and I may need only the optional in some occurrences). Besides \mathop doesn't require a specific order of the arguments, while \newcommand does.

Any ideas?

-

You can look ahead for _ and ^ by hand.I'm not sure it's a good idea but something like

\documentclass{article}
\usepackage{mathtools}

\makeatletter
\def\Sum{%
\let\Sum@subarg\relax
\let\Sum@suparg\relax
\Sum@testsub}

\def\Sum@sub_#1{%
\def\Sum@subarg{#1}%
\ifx\Sum@suparg\relax
\expandafter\Sum@testsup
\else
\expandafter\Sum@doit
\fi}

\def\Sum@sup^#1{%
\def\Sum@suparg{#1}%
\Sum@testsub@b
}

\def\Sum@testsup{%
\let\Sum@suparg\@empty
\@ifnextchar^\Sum@sup\Sum@testsub}

\def\Sum@testsup@b{%
\let\Sum@suparg\@empty
\@ifnextchar^\Sum@sup\Sum@doit}

\def\Sum@testsub{%
\@ifnextchar_\Sum@sub\Sum@testsup@b}

\def\Sum@testsub@b{%
\@ifnextchar_\Sum@sub\Sum@doit}

\def\Sum@doit{%
\sum_{\mathclap{\Sum@subarg}}^{\mathclap{\Sum@suparg}}}

\makeatletter

\begin{document}

$\sum_{aaaaa+aaaaa}^{bbbbbb}x_i$
$\Sum_{aaaaa+aaaaa}^{bbbbbb}x_i$
$\Sum^{bbbbbb}_{aaaaa+aaaaa}x_i$
$\Sum_{aaaaa+aaaaa}x_i$
$\Sum^{bbbbbb}x_i$
$\Sum x_i$

\end{document}

-
Thanks! I don't understand how you did it but that is exactly what I needed! – AndreasT Oct 17 '12 at 12:14