使用可变参数循环 newcommand

使用可变参数循环 newcommand

继我之前的问题,我想知道如何为新命令提供变量输入,以便执行以下操作

\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

谢谢你!

答案1

我建议使用不同的语法,而不是定义带有多个参数的新宏,其中第二个强制参数包含格式被加数,代表#1当前求和指数。

* 形式\xrepsum将打印完整的表达式而不带省略号。

\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

另外,避免在显示方程式之前出现空行:它们是总是错误。仅当以下文本开始新段落时,才应在显示的公式后添加一个空行。

相关内容