# Loop code for repeated sums

I tried to read other topics regarding \newcommand* to create a macros in order to have repeated summations with a single command but I was not able to understand the logic behind it. Let me explain better: I would like to create a \newcommand that simply by typing \repsum{9}{F}{u}, where 9 is the repetition number, can create an output like

F_1u_1+F_2u_2+F_3u_3+...+F_8u_8+F_9u_9.

Can someone help me with this matter? Thank you very much :)

EDIT

Thank you for your answers. I am sorry if I did not post a MWE. The answer provided by current user and zarko does the trick but it uses tikzpicture, while I'd like to use it in Math mode. I'll add an example to show you what I'd like to do (in pseudo code the newcommand)

\documentclass{article}
\newcommand*{\repsum}[3]{
for i=1:#1
if i~=#1
#2_i#3_i+
else
#2_i#3_i
end
end
\begin{document}

The CUF Refined theory expands the summation as

$$u=\repsum{9}{F}{u}=F_\tau u_\tau$$

where the last expression exploits the Einstein notation.

\end{document}

• Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Sep 5, 2018 at 9:56

To use it in math mode you could simply use a \foreach outside a tikzpicture. This would require the pgffor package (only if you're not using Tikz already):

\documentclass[]{article}

\usepackage{mathtools}
\usepackage{pgffor}

\newcommand{\repsum}[3]{%
\foreach \i in {1,...,#1}{
\ifnum\i>1
+ #2_{\i} #3_{\i}
\else
#2_{\i} #3_{\i}
\fi
}
}

\begin{document}
\begin{equation*}
\mathbf{F}\bullet\mathbf{u} = \repsum{9}{F}{u}
\end{equation*}
\end{document}

• Thank you, this solved perfectly my issue. Thanks also to the other guy who helped me, probably his answer was not strictly what I wanted because I explained it poorly. For the next time, could you please tell me how to include the output of the code I post? So I can show you directly the expected output I'd like to have. Thank you a lot! Sep 5, 2018 at 10:11
– Max
Sep 5, 2018 at 10:17
• I didn't know it was simply an image, I can do that :) Thank you! Sep 5, 2018 at 10:27

You can use xparse:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\repsum}{O{3}mmm}
{% #1 = optional number of starting summands
% #2 = final number
% #3 = first symbol
% #4 = second symbol
\int_step_inline:nn { #1 } { #3\sb{##1}#4\sb{##1} + }
\dotsb
\int_step_inline:nnn { #2 - 1} { #2 } { + #3\sb{##1}#4\sb{##1} }
}
\ExplSyntaxOff

\begin{document}

First test: $\repsum{9}{F}{u}$

Second test: $\repsum[2]{6}{F}{u}$

The CUF Refined theory expands the summation as
$$u=\repsum{9}{F}{u}=F_\tau u_\tau$$
where the last expression exploits the Einstein notation.

\end{document}


The idea is to make a cycle from 1 to 3 (or the number specified in the optional argument), printing the summands with their subscripts followed by +; then print the dots and + followed by the summands from #2-1 (#2 is the final number of summands) to #2.

With this implementation, you are responsible for ensuring no overlap. So you can do \repsum[1]{4}{F}{u}, but with less than four summands it won't work.

A different version that automatically skips the dots if they're not needed.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\repsum}{O{3}mmm}
{% #1 = optional number of starting summands
% #2 = final number
% #3 = first symbol
% #4 = second symbol
\int_compare:nTF { #2 - #1 < 3 }
{% no dots necessary
#3\sb{1}#4\sb{1}
\int_step_inline:nnn { 2 } { #2 } { + #3\sb{##1}#4\sb{##1} }
}
{
\int_step_inline:nn { #1 } { #3\sb{##1}#4\sb{##1} + }
\dotsb
\int_step_inline:nnn { #2 - 1} { #2 } { + #3\sb{##1}#4\sb{##1} }
}
}
\ExplSyntaxOff

\begin{document}

First test: $\repsum{9}{F}{u}$

Second test: $\repsum[2]{6}{F}{u}$

Third test: $\repsum{5}{F}{u}$

Fourth test: $\repsum{3}{F}{u}$

Fifth test: $\repsum{2}{F}{u}$

Sixth test: $\repsum{1}{F}{u}$

The CUF Refined theory expands the summation as
$$u=\repsum{9}{F}{u}=F_\tau u_\tau$$
where the last expression exploits the Einstein notation.

\end{document}


Maybe this?

\documentclass{article}
\usepackage{tikz}
\newcommand{\cussum}[1]{
\begin{tikzpicture}[baseline=-.1cm]
\foreach \x in {1,2,...,#1}
{
\ifnum\x<#1
\node at (\x,0) {$F_{\x}u_{\x}+$};
\fi
\ifnum\x=#1
\node at (\x-.1,0) {$F_{\x}u_{\x}$};
\fi
}
\end{tikzpicture}
}
\begin{document}
\cussum{9} Minimal Working Examples are nice, aren't they \ldots
\end{document}


Here is the output:

Re-using my \replaceiandreplicate-macro from the discussion How to make a command to automate creation of prime factorization-like products?, a possibility of approaching the matter could be:

\documentclass{article}

\makeatletter
%%=============================================================================
%% Paraphernalia:
%%    \UD@firstoftwo, \UD@secondoftwo,
%%    \UD@PassFirstToSecond, \UD@Exchange, \UD@removespace
%%    \UD@CheckWhetherNull, \UD@CheckWhetherBrace,
%%=============================================================================
\newcommand\UD@firstoftwo[2]{#1}%
\newcommand\UD@secondoftwo[2]{#2}%
\newcommand\UD@PassFirstToSecond[2]{#2{#1}}%
\newcommand\UD@Exchange[2]{#2#1}%
\newcommand\UD@removespace{}\UD@firstoftwo{\def\UD@removespace}{} {}%
%%-----------------------------------------------------------------------------
%% Check whether argument is empty:
%%.............................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is empty>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
\newcommand\UD@CheckWhetherNull[1]{%
\romannumeral0\expandafter\UD@secondoftwo\string{\expandafter
\UD@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
\UD@secondoftwo\string}\expandafter\UD@firstoftwo\expandafter{\expandafter
\UD@secondoftwo\string}\expandafter\expandafter\UD@firstoftwo{ }{}%
\UD@secondoftwo}{\expandafter\expandafter\UD@firstoftwo{ }{}\UD@firstoftwo}%
}%
%%-----------------------------------------------------------------------------
%% Check whether argument's first token is a catcode-1-character
%%.............................................................................
%% \UD@CheckWhetherBrace{<Argument which is to be checked>}%
%%                      {<Tokens to be delivered in case that argument
%%                        which is to be checked has leading
%%                        catcode-1-token>}%
%%                      {<Tokens to be delivered in case that argument
%%                        which is to be checked has no leading
%%                        catcode-1-token>}%
\newcommand\UD@CheckWhetherBrace[1]{%
\romannumeral0\expandafter\UD@secondoftwo\expandafter{\expandafter{%
\string#1.}\expandafter\UD@firstoftwo\expandafter{\expandafter
\UD@secondoftwo\string}\expandafter\expandafter\UD@firstoftwo{ }{}%
\UD@firstoftwo}{\expandafter\expandafter\UD@firstoftwo{ }{}\UD@secondoftwo}%
}%
%%-----------------------------------------------------------------------------
%% Check whether brace-balanced argument starts with a space-token
%%.............................................................................
%% \UD@CheckWhetherLeadingSpace{<Argument which is to be checked>}%
%%                             {<Tokens to be delivered in case <argument
%%                               which is to be checked>'s 1st token is a
%%                               space-token>}%
%%                             {<Tokens to be delivered in case <argument
%%                               which is to be checked>'s 1st token is not
%%                               a space-token>}%
\romannumeral0\UD@CheckWhetherNull{#1}%
{\expandafter\expandafter\UD@firstoftwo{ }{}\UD@secondoftwo}%
}%
\expandafter\UD@CheckWhetherNull\expandafter{\UD@secondoftwo#1{}}%
{\UD@Exchange{\UD@firstoftwo}}{\UD@Exchange{\UD@secondoftwo}}%
{\UD@Exchange{ }{\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter}\expandafter\expandafter
\expandafter}\expandafter\UD@secondoftwo\expandafter{\string}%
}%
%%-----------------------------------------------------------------------------
%% Extract first inner undelimited argument:
%%
%%   \UD@ExtractFirstArg{ABCDE} yields  {A}
%%
%%   \UD@ExtractFirstArg{{AB}CDE} yields  {AB}
%%.............................................................................
\newcommand\UD@RemoveTillUD@SelDOm{}%
\long\def\UD@RemoveTillUD@SelDOm#1#2\UD@SelDOm{{#1}}%
\newcommand\UD@ExtractFirstArg[1]{%
\romannumeral0%
\UD@ExtractFirstArgLoop{#1\UD@SelDOm}%
}%
\newcommand\UD@ExtractFirstArgLoop[1]{%
\expandafter\UD@CheckWhetherNull\expandafter{\UD@firstoftwo{}#1}%
{ #1}%
{\expandafter\UD@ExtractFirstArgLoop\expandafter{\UD@RemoveTillUD@SelDOm#1}}%
}%
%%=============================================================================
%% \DefineReplacementMacro{<replacement-macro>}%
%%                        {<internal helper-macro>}%
%%                        {<item to replace>}%
%%
%%  defines <replacement-macro> to fetch two arguments,
%%  #1 = <replacement for item to replace>
%%  #2 = <token sequence with item to replace>
%%  , and -- after two expansion-steps to deliver:
%%  <token sequence with all instances of <item to replace> replaced
%%  by <replacement for item to replace>. >
%%
%% Internally an <internal helper-macro> is needed.
%%
%%  (!!! <replacement-macro> does also replace all pairs of matching
%%       explicit character tokens of catcode 1/2 by matching braces!!!)
%%-----------------------------------------------------------------------------
\newcommand\DefineReplacementMacro[3]{%
\newcommand#2{}\long\def#2##1#3{}%
\newcommand#1[2]{%
\romannumeral0\UD@ReplaceAllLoop{##2}{##1}{}{#2}{#3}%
}%
}%
\newcommand\UD@ReplaceAllLoop[5]{%
\UD@CheckWhetherNull{#1}{ #3}{%
\expandafter\UD@ReplaceAllLoop
\expandafter{\UD@removespace#1}{#2}{#3 }{#4}{#5}%
}{%
\UD@CheckWhetherBrace{#1}{%
\expandafter\expandafter\expandafter\UD@PassFirstToSecond
\expandafter\expandafter\expandafter{%
\expandafter\UD@PassFirstToSecond\expandafter{%
\romannumeral0\expandafter\UD@ReplaceAllLoop
\romannumeral0\UD@ExtractFirstArgLoop{#1\UD@SelDOm}{#2}{}{#4}{#5}%
}{#3}}%
{\expandafter\UD@ReplaceAllLoop\expandafter{\UD@firstoftwo{}#1}{#2}}%
{#4}{#5}%
}{%
\expandafter\UD@CheckWhetherNoReplacement
\romannumeral0\UD@ExtractFirstArgLoop{#1\UD@SelDOm}{#1}{#2}{#3}{#4}{#5}%
}%
}%
}%
}%
\newcommand\UD@CheckWhetherNoReplacement[6]{%
\expandafter\UD@CheckWhetherNull\expandafter{#5#1#6}%
{%
\expandafter\UD@ReplaceAllLoop
\expandafter{\UD@firstoftwo{}#2}{#3}{#4#1}{#5}{#6}%
}{%
\expandafter\UD@ReplaceAllLoop
\expandafter{\UD@firstoftwo{}#2}{#3}{#4#3}{#5}{#6}%
}%
}%
%%=============================================================================
%% \UD@ReplaceAlli -- Replace all "i" in undelimited Argument:
%%
%%   \UD@ReplaceAlli{<replacement for i>}{<token sequence with i>}
%%   yields  <token sequence with all i replaced by replacement for i>
%%
%%  <replacement for i> may contain i.
%%
%%  (This routine does also replace all pairs of matching explicit
%%   character tokens of catcode 1/2 by matching braces!!!)
%%
%%  The letter "i" as item to replace is hard-coded.
%%  You cannot replace öetters other than I with this macro.
%%.............................................................................
\DefineReplacementMacro{\UD@ReplaceAlli}{\UD@gobbletoi}{i}%
%%
%%=============================================================================
%% \replaceiandreplicate{<term with i>}%
%%                      {<loop-start-index>}%
%%                      {<loop-end-index>}%
%%
%% e.g.,
%%
%%  \replaceiandreplicate{p_i^{\epsilon_i}}{1}{3}
%%.............................................................................
\newcommand\replaceiandreplicate[3]{%
\romannumeral0\replaceiandreplicateloop{#3}{#2}{#1}{}%
}%
\newcommand\replaceiandreplicateloop[4]{%
\ifnum#1<#2 %
\expandafter\UD@firstoftwo
\else
\expandafter\UD@secondoftwo
\fi
{ #4}{%
\expandafter\expandafter\expandafter\UD@PassFirstToSecond
\expandafter\expandafter\expandafter{%
\UD@ReplaceAlli{#1}{#3}#4%
}{%
\expandafter\replaceiandreplicateloop
\expandafter{\number\numexpr\number#1-1\relax}{#2}{#3}%
}%
}%
}%

\makeatother

\parindent=0ex

\begin{document}

\begin{verbatim}
$\replaceiandreplicate{\ifnum i>1+\fi F_iu_i}{1}{9}$
\end{verbatim}

yields:\bigskip

$\replaceiandreplicate{\ifnum i>1+\fi F_iu_i}{1}{9}$

\begin{verbatim}
$\csname @gobble\expandafter\expandafter \expandafter \endcsname \replaceiandreplicate{+F_iu_i}{1}{9}$
\end{verbatim}

yields:\bigskip

$\csname @gobble\expandafter\expandafter \expandafter \endcsname \replaceiandreplicate{+F_iu_i}{1}{9}$

\end{document}