7

In https://tex.stackexchange.com/a/89448/37686, the last option given (with E = ...) produces a sum-like expression which I am looking to reproduce.

How can I define a command (\sumn, say) which behaves like \sum in display mode but which has the extra n symbol next to the \sum symbol, so that

E = \sumn_{-\infty}^\infty f(x)

produces the output given in the linked answer?

  • 1
    Welcome to TeX.SX! I don't understand the question: The LaTeX code has already been provided by Werner in his answer: E &= \sum_{-\infty}^{\infty}\mathop{}_{\mkern-5mu n} f(x). Or you're asking about a general command for this? –  Aug 05 '15 at 22:00

3 Answers3

6

The amsmath package already provides for this with \sideset:

\documentclass{article}
\usepackage{amsmath}

\newcommand{\vsum}[1]{%
  \sideset{}{_{#1}}\sum
}

\newcommand{\sumn}{\vsum{n}}

\begin{document}

\begin{align*}
  E &= \sumn_{0}^{\infty} f(n) \\
  F &= \vsum{m}_{-\infty}^{\infty} g(m) \\
  G &= \vsum{\text{Ridiculously long text}}_{i=0}^{n} h(i)
\end{align*}

\end{document}

enter image description here

egreg
  • 1,121,712
3

A suggestion for \sumn, using the sum code from Werner's answer here

I generously gifted an optional argument defaulting to n if omitted.

\documentclass[12pt]{article}

\newcommand{\sumn}[1][n]{%
  \sum_{-\infty}^{\infty}\mathop{}_{\mkern-5mu {#1}}%
}

\usepackage{mathtools}

\begin{document}
\begin{align*}
  E &= \sumn f(x) \\
  F &= \sumn[m] g(x) \\
  G &= \sumn[\text{Ridiculously long text}] h(x)   
\end{align*}
\end{document}

enter image description here

Edit -- Extended version:

\documentclass[12pt]{article}

\usepackage{xparse}
\NewDocumentCommand{\sumnimproved}{omm}{%
  \IfValueTF{#1}{%
    \sum\limits_{#2}^{#3}\mathop{}_{\mkern-5mu #1}%
  }{%
    \sum\limits_{#2}^{#3}%
  }%
}

\NewDocumentCommand{\sumn}{oD<>{-\infty}O{\infty}}{%
  \IfValueTF{#1}{%
    \sum\limits_{#2}^{#3}\mathop{}_{\mkern-5mu #1}%
  }{%
    \sum\limits_{#2}^{#3}%
  }%
}


\newcommand{\sumnbasic}[1][n]{%
  \sum_{-\infty}^{\infty}\mathop{}_{\mkern-5mu #1}
}

\usepackage{mathtools}

\begin{document}
\begin{align*}
  E &= \sumnbasic f(x) \\
  F &= \sumnbasic[m] g(x) \\
  G &= \sumnbasic[\text{Ridiculously long text}] h(x) 
  \\
  D &= \sumnimproved[n]{-\infty}{\infty} e(x) \\
  E &= \sumnimproved[n]{-\infty}{\infty} f(x) \\
  F &= \sumnimproved[m]{-1}{1} g(x) \\
  G &= \sumnimproved[\text{Ridiculously long text}]{k=0}{\infty} h(x) \\
  \\
  E &= \sumn[n] f(x) \\
  F &= \sumn[m]<a>[b] g(x) \\
  G &= \sumn[\text{Ridiculously long text}] h(x) 
\end{align*}
\end{document}

enter image description here

  • Hi Christian, thanks for the answer. However, what I'd like to be able to do is to set the lower and upper limits on the \sum using sub- and superscripts on the \sumn command. – Liam Baker Aug 05 '15 at 22:22
1

A basic definition would be

\newcommand*\sumn{\mathrlap{\mathord{\phantom{{}\sum}}_{m}}\sum\limits}

altough it may collide with the following math.

A partial solution would be to use normal arguments

\newcommand*\sumn[3][m]{\sum\limits_{#2}^{#3}\mathord{\vphantom{\sum}}_{#1}}

With the power of xparse it's easy, altough it might look long because we have to define new arguments. The code is taken from this answer.

\documentclass{scrartcl}
\usepackage{mathtools,amssymb,xparse}

\ExplSyntaxOn
\cs_new_protected:Npn \__xparse_count_type_k:w #1
 {
  \__xparse_single_token_check:n { #1 }
  \quark_if_recursion_tail_stop_do:Nn #1 { \__xparse_bad_arg_spec:wn }
  \__xparse_count_mandatory:N
 }
\cs_new_protected:Npn \__xparse_count_type_K:w #1 #2
 {
  \__xparse_single_token_check:n { #1 }
  \quark_if_recursion_tail_stop_do:nn { #2 } { \__xparse_bad_arg_spec:wn }
  \__xparse_count_mandatory:N
 }
\cs_new_protected:Npn \__xparse_add_type_k:w #1
 { \exp_args:NNo \__xparse_add_type_K:w #1 { \c__xparse_no_value_tl } }
\cs_new_protected:Npn \__xparse_add_type_K:w #1 #2
 {
  \__xparse_flush_m_args:
  \__xparse_add_grabber_optional:N K
  \tl_put_right:Nn \l__xparse_signature_tl { #1 { #2 } }
  \__xparse_prepare_signature:N
 }
\cs_new_protected:Npn \__xparse_add_expandable_type_k:w #1
 {
  \exp_args:NNo \__xparse_add_expandable_type_K:w #1 { \c__xparse_no_value_tl }
 }
\cs_new_protected_nopar:Npn \__xparse_add_expandable_type_K:w #1 #2
 {
  \__msg_kernel_error:nnx { xparse } { invalid-expandable-argument-type } { K }
  \__xparse_add_expandable_type_m:w % May be create this?
 }
\cs_new_protected:Npn \__xparse_grab_K:w #1 #2 #3 \l__xparse_args_tl
 {
  \__xparse_grab_K_aux:NnnNn #1 { #2 } { #3 } \cs_set_protected_nopar:Npn
   { _ignore_spaces }
 }
\cs_new_protected:Npn \__xparse_grab_K_long:w #1 #2 #3 \l__xparse_args_tl
 {
  \__xparse_grab_K_aux:NnnNn #1 { #2 } { #3 } \cs_set_protected:Npn
   { _ignore_spaces }
 }
\cs_new_protected:Npn \__xparse_grab_K_trailing:w #1 #2 #3 \l__xparse_args_tl
 {
  \__xparse_grab_K_aux:NnnNn #1 { #2 } { #3 } \cs_set_protected_nopar:Npn
   { _ignore_spaces }
 }
\cs_new_protected:Npn \__xparse_grab_K_long_trailing:w #1 #2 #3 \l__xparse_args_tl
 {
  \__xparse_grab_K_aux:NnnNn #1 { #2 } { #3 } \cs_set_protected:Npn
   { _ignore_spaces }
 }
\cs_new_protected:Npn \__xparse_grab_K_aux:NnnNn #1 #2 #3 #4 #5
 {
  \exp_after:wN #4 \l__xparse_fn_tl ##1
   {
    \__xparse_add_arg:n { ##1 }
    #3 \l__xparse_args_tl
   }    
  \use:c { peek_meaning_remove #5 :NTF } #1
   { \l__xparse_fn_tl }
   {
    \__xparse_add_arg:n { #2 }
    #3 \l__xparse_args_tl
   }
 }

\prop_put:Nnn \c__xparse_shorthands_prop { a } { k \sb }
\prop_put:Nnn \c__xparse_shorthands_prop { b } { k \sp }
\prop_put:Nnn \c__xparse_shorthands_prop { A } { K \sb }
\prop_put:Nnn \c__xparse_shorthands_prop { B } { K \sp }
\ExplSyntaxOff

\NewDocumentCommand\sumn{ab}
  {\sum\limits\IfValueT{#1}{_{#1}}\IfValueT{#2}{^{#2}}%
   \mkern-2mu\mathord{\vphantom{\sum}}_{m}}

\begin{document}
\[
  E = \sumn_{-\infty}^\infty f(x)
\]
\end{document}

If the sub- or super-scripts can be long, that would move the m far away to the right, the solution in that case is to check for lengths of both scripts and act depending of it, but I leave that for now, unless it's really necessary.

Manuel
  • 27,118