1

I'm trying to create a summation command that formats it better than just using \sum, and without having to use \sum\limits_a^b every time. I'm using

\makeatletter
\newcommand{\summ}[3][\@nil]{%
\def\tmp{#1}%
\ifx\tmp\@nnil
   \sum\limits_{#2}^{#3}
\else
     \sum\limits_{#1 = #2}^{#3}
\fi}
\makeatother

The else part doesn't work. If I use \summ{1}{2}, I get

enter image description here

which is as expected. but if I use \summ{1}{2}{3}, I get

enter image description here

which is not right. What am I doing wrong?

4
  • Welcome to TeX.SE.
    – Mico
    Jan 19, 2021 at 19:31
  • 1
    If I understand the definition of \summ correctly, it takes 3 arguments, the first of which is optional (and defaults to \@nil). If this understanding is correct, you may want to change \summ{1}{2}{3} to \summ[i]{1}{2}, say, as the first, optional parameter has to be encased in square brackets, not curly braces.
    – Mico
    Jan 19, 2021 at 19:38
  • Also, you're comparing \tmp to \@nnil yet you're setting \tmp to \@nil. While they may be similar, other packages could define \@nnil at which point your macro would fail. So, make sure you're comparing \tmp to \@nil (what you're supplying in your default for the optional argument) and not something else.
    – Werner
    Jan 19, 2021 at 19:56
  • why do you ever need \sum\limits ? if you want, for some reason \sum to take limits even in inline math just add \limits to its definition. But you basically then can not use \sum in inline math at all. Jan 19, 2021 at 20:23

1 Answer 1

1

The way you've set up the macro \summ, taking 1 optional and 2 required arguments, you probably shouldn't write \summ{1}{2}{3}; instead, do give \summ[i]{1}{2} a try. You'll find out that it expands to \sum\limits_{i=1}^{2}.

I think that defining \summ to take an optional argument is unnecessarily complicated. How about dropping the conditional and simply setting

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

That way, it's no big deal to switch from \summ{1}{N} to \summ{i=1}{N}, is it?

A full MWE:

\documentclass{article}
\newcommand\summ[2]{\sum\limits_{#1}^{#2}}
\begin{document}
$\summ{1}{N} \quad \summ{i=1}{N}$
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .