1

So the command is as follows:

\newcommand{\sumi1ton}{\sum\limits_{i=1}^{n}}

I want to be able to generalize this further. For example, if I type \sumj3toQ then it would give me the summation from j=3 to Q. How do I make this happen without writing a command for every possible scenario?

3
  • 3
    Surely you must be getting an error message when you try to define a macro called \sumi1ton via a \newcommand or \def instruction: One isn't allowed to have numerals (and other non-letter characters) in a TeX macro whose name contains one or more letter characters.
    – Mico
    Aug 7, 2018 at 20:10
  • Possible related here? tex.stackexchange.com/questions/224430/…
    – Sebastiano
    Aug 7, 2018 at 20:14
  • 1
    @Mico you are correct. I made a mistake in this post. The actual command was \sumiton Aug 7, 2018 at 20:34

2 Answers 2

6

With spaces as delimiters to make it clearer:

custom sums

\documentclass{article}
\def\sumx #1 #2 to #3{\sum\limits_{#1=#2}^{#3}}
\begin{document}
    \[\sumx i 1 to n\]
    \[\sumx j 3 to Q\]
\end{document}
7
  • Wonderful answer +1 surely.
    – Sebastiano
    Aug 7, 2018 at 20:06
  • Thank you. So in order to use this, I would need to include it in the preamble for every document, right? Is it possible to include this in every document without having to put it in the preamble everytime? Aug 7, 2018 at 20:12
  • 2
    @MohammedShahid You can compile an own format, but surely that would be more effort than just putting this into every document.
    – TeXnician
    Aug 7, 2018 at 20:14
  • @TeXnician So would having a document with all of the commands I need and then pasting it into the preamble for every other document be the easiest solution? Aug 7, 2018 at 20:21
  • 2
    @MohammedShahid It depends on the amount. Dependent on your technical knowledge it might be easier to write a package in the local texmf-tree and include that (if your definitions accumulate) or even create a new format, but yes, usually copy and paste is the easiest method.
    – TeXnician
    Aug 7, 2018 at 20:29
1

Are that enough weird use cases? The default behaviour for empty arguments could be changed though.

I've added the special case that you use only two arguments with a to in between. In that case it is assumed you did mean \sumx{#1}{0}{#2}, you can change the default start (0 in the above example) at the lines marked with % CHANGE DEFAULT START HERE.

EDIT: Allowing and parsing = contained in #1 as if both #1 and #2 were given.

EDIT: Also allow an optional = in between #1 and #2.

\documentclass[]{article}

\makeatletter
\newcommand\sumx%>>>
  {%
    \@ifnextchar\bgroup
      {\sumx@braces@a}% if \sumx is followed by
      {\sumx@spaces@a}%
  }%<<<
\newcommand\sumx@ifempty[1]%>>>
  {%
    \if\relax\detokenize{#1}\relax
      \expandafter\@firstoftwo
    \else
      \expandafter\@secondoftwo
    \fi
  }%<<<
\newcommand\sumx@if@equal@contained@a[1]%>>>
  {%
    \sumx@if@equal@contained@b#1=\end
  }%<<<
\newcommand\sumx@if@equal@contained@b{}%>>>
\def\sumx@if@equal@contained@b#1=#2\end
  {%
    \sumx@ifempty{#2}{\@secondoftwo}{\@firstoftwo}%
  }%<<<
\newcommand\sumx@braces@a[1]%>>>
  {%
    \sumx@after@a{#1}%
  }%<<<
\newcommand\sumx@spaces@a{}%>>>
\def\sumx@spaces@a #1
  {%
    \sumx@after@a{#1}%
  }%<<<
\newcommand\sumx@after@a[1]%>>>
  {%
    \sumx@if@equal@contained@a{#1}
      {\sumx@after@b{#1}{}}
      {%
        \@ifnextchar{t}
          {\sumx@to@a@after@a{#1}}
          {%
            \@ifnextchar{=}
              {\sumx@equal@after@a{#1}}
              {\sumx@after@after@a{#1}}
          }%
      }%
  }%<<<
\newcommand\sumx@equal@after@a{}%>>>
\def\sumx@equal@after@a #1=%
  {%
    \sumx@after@after@a{#1}%
  }%<<<
\newcommand\sumx@after@after@a[1]%>>>
  {%
    \@ifnextchar\bgroup
      {\sumx@braces@b{#1}}
      {\sumx@spaces@b{#1}}%
  }%<<<
\newcommand\sumx@to@a@after@a{}%>>>
\def\sumx@to@a@after@a #1t%
  {%
    \@ifnextchar{o}
      {\sumx@to@b@after@a{#1}}
      {\sumx@braces@b{#1}{t}}%
  }%<<<
\newcommand\sumx@to@b@after@a{}%>>>
\def\sumx@to@b@after@a #1o%
  {%
    \sumx@to@c@after@a{#1}{0}% CHANGE DEFAULT START HERE
  }%<<<
\newcommand\sumx@to@c@after@a[2]%>>>
  {%
    \@ifnextchar\bgroup
      {\sumx@braces@c{#1}{#2}}
      {\sumx@spaces@c{#1}{#2}}%
  }%<<<
\newcommand\sumx@braces@b[2]%>>>
  {%
    \sumx@after@b{#1}{#2}%
  }%<<<
\newcommand\sumx@spaces@b{}%>>>
\def\sumx@spaces@b #1#2
  {%
    \sumx@after@b{#1}{#2}%
  }%<<<
\newcommand\sumx@after@b[2]%>>>
  {%
    \@ifnextchar\bgroup
      {\sumx@braces@c{#1}{#2}}
      {%
        \@ifnextchar{t}
          {\sumx@to@a{#1}{#2}}
          {\sumx@spaces@c{#1}{#2}}%
      }%
  }%<<<
\newcommand\sumx@braces@c[3]%>>>
  {%
    \sumx@output{#1}{#2}{#3}%
  }%<<<
\newcommand\sumx@spaces@c{}%>>>
\def\sumx@spaces@c #1#2#3
  {%
    \sumx@output{#1}{#2}{#3}%
  }%<<<
\newcommand\sumx@to@a{}%>>>
\def\sumx@to@a #1#2t%
  {%
    \@ifnextchar{o}
      {\sumx@to@b{#1}{#2}}
      {\sumx@output{#1}{#2}{t}}%
  }%<<<
\newcommand\sumx@to@b{}%>>>
\def\sumx@to@b #1#2o%
  {%
    \@ifnextchar\bgroup
      {\sumx@braces@c{#1}{#2}}
      {\sumx@spaces@c{#1}{#2}}%
  }%<<<
\newcommand\sumx@output[3]%>>>
  {%
    \sum
    \sumx@ifempty{#1#2#3}% if all arguments are empty
      {}% do nothing more
      {%
        \limits
        \sumx@ifempty{#1#2}% if both 1 and 2 are empty
          {}% do nothing here
          {%
            _{%
              \sumx@ifempty{#1}
                {#2}
                {%
                  \sumx@ifempty{#2}
                    {#1}
                    {#1=#2}%
                }%
            }%
          }%
        \sumx@ifempty{#3}{}{^{#3}}
      }
  }%<<<
\makeatother


\begin{document}
$
\sumx{ij}{10}{30}
\sumx{ij}{10}to{30}
\sumx  ij   10  to  30
\sumx  ij   10      30
\sumx {ij} {10}    {30}
\sumx {ij} {10} to {30}
\sumx {ij}  10      30 
\sumx  ij  {10}     30
\sumx  ij   10     {30}$\par
$
\sumx i 1 t %no o follows the t so it is the third argument
\sumx i 1 t o fail %spaces between "t" and "o" are parsed as "to" :(
\sumx{}{1}{10}
\sumx{i}{}{}
\sumx{i}{}{10}
\sumx i t a
\sumx i to a
\sumx i t o a
\sumx i t{o} a % if you really need t and o as two arguments group one or both
\sumx i=1 to a
\sumx i = 1 to a
$
\end{document}

the many uses of sumx

The %>>> and %<<< are just the fold marks of my VIM, you can ignore them.

You must log in to answer this question.

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