2

Following my previous question, I would like to know how to have variable inputs for a newcommand in order to do the following

\documentclass{article}
\newcommand*{\repsum}[VARIABLE NUMBER]{
    \foreach \i in{1,...,#1}{
        \ifnum\i>1
            +#2_{\i}#3_{#4\i}   % This should be able to change according to the input, see below
        \else
            #2_{\i}#3_{#4\i}    % This should be able to change according to the input, see below
        \fi
    }}
\begin{document}

The CUF Refined theory expands the summation as

\begin{equation}
u=\repsum{4}{F}{u}=F_\tau u_\tau
\end{equation}

where the last expression exploits the Einstein notation. If we include nodes

\begin{equation}
 u=\repsum{4}{F}{u}{N}=F_\tau N_i u_{\tau i}
\end{equation}

\end{document}

enter image description here

Thank you!

1
  • optional arguments in latex should use [] not {} Commented Sep 5, 2018 at 11:49

1 Answer 1

1

Rather than defining new macros with several arguments, I propose a different syntax, where the second mandatory argument contains the format of the summands, with #1 standing for the current summation index.

The *-form of \xrepsum will print the full expression without ellipsis.

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

\ExplSyntaxOn
\NewDocumentCommand{\xrepsum}{sO{3}mm}
 {% #1 = star; if present, print the full summation
  % #2 = optional number of starting summands
  % #3 = final number
  % #4 = summands
  \cs_set_protected:Nn \__mascolo_repsum_summand:n { #4 }
  \cs_set_protected:Nn \__mascolo_repsum_summand_pre:n { + #4 }
  \cs_set_protected:Nn \__mascolo_repsum_summand_post:n { #4 + }
  \bool_lazy_or:nnTF { #1 } { \int_compare_p:n { #3 - #2 < 3 } }
   {% print the full summation either because we want it (*-form) or
    % there are too few summands
     \__mascolo_repsum_summand:n { 1 }
     \int_step_function:nnN { 2 } { #3 } \__mascolo_repsum_summand_pre:n
   }
   {
    \int_step_function:nN { #2 } \__mascolo_repsum_summand_post:n
    \dotsb
    \int_step_function:nnN { #3 - 1} { #3 } \__mascolo_repsum_summand_pre:n
   }
 }
\ExplSyntaxOff

\begin{document}

Full summation: $\xrepsum*{9}{F_{#1}u_{#1}}$

First test: $\xrepsum{9}{F_{#1}u_{#1}}$

Second test: $\xrepsum[2]{6}{F_{#1}u_{#1}}$

Third test: $\xrepsum{5}{F_{#1}u_{x#1}}$

Fourth test: $\xrepsum{3}{F^{#1}u_{#1}}$

Fifth test: $\xrepsum{2}{F_{#1}u_{#1x}}$

Sixth test: $\xrepsum{1}{F_{#1}u_{#1}}$

The CUF Refined theory expands the summation as

\begin{equation}
u=\xrepsum{4}{F_{#1}u_{x#1}}=F_\tau u_\tau
\end{equation}
where the last expression exploits the Einstein notation. If we include nodes
\begin{equation}
u=\xrepsum{4}{F_{#1}N_{#1}u_{x#1}}=F_\tau N_i u_{\tau i}
\end{equation}

\end{document}

picture

As an aside, avoid blank lines before displayed equations: they are always wrong. A blank line after displayed equation should be added only if the following text starts a new paragraph.

3
  • Thank you very much. This solves all my requests, even though it is way beyond my comprehension level. Specifically, I understand everything it is written inside and how it is working, but I would not be able to replicate it. Once I take my degree I'll focus on these advanced commands. Thank you!
    – L Mascolo
    Commented Sep 5, 2018 at 13:41
  • 1
    @LMascolo \int_step_function:nN does a cycle passing to a one-argument function the integers from 1 to the mandatory argument. The variant \int_step_function:nnN accepts the starting point and the end point; more generally, \int_step_function:nnnN accepts the starting point, the step and the end point. The main trick in the code is defining helper one-argument functions based on the stated template.
    – egreg
    Commented Sep 5, 2018 at 14:04
  • Thank you, I'll study the topic more. But thanks for your help :) You've been really kind
    – L Mascolo
    Commented Sep 5, 2018 at 14:05

You must log in to answer this question.

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