Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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

enter image description here

\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}
share|improve this answer
    
Thanks! I don't understand how you did it but that is exactly what I needed! –  AndreasT Oct 17 '12 at 12:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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