答案1
forloop
和的组合calc
:
\documentclass[10pt,a4paper]{article}
\usepackage{amsmath}
\usepackage{calc}
\usepackage{forloop}
\begin{document}
\hsize=8cm
\newcounter{k}\newcounter{j}%
\begin{align}
%forloop[ step ]{ counter }{ initial value }{ condition }{ code }
\nonumber
\forloop{k}{1}{\value{k} < 11}{
\setcounter{j}{2*\value{k}-1}
\\ y_{\arabic{k}}&=A_{\arabic{k},\arabic{j}} x_{\arabic{j}}
\addtocounter{j}{1}
+A_{\arabic{k},\arabic{j}} x_{\arabic{j}}
}
\end{align}
\end{document}
答案2
这里是使用 l3int 的另一种方法:
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse} %dt. Silbentrennung
\ExplSyntaxOn
\cs_generate_variant:Nn \int_set:Nn { Nx }
\NewDocumentCommand \Formula { O{1} O{1} m }
{
\int_step_inline:nnnn
{ #1 }%initial value
{ #2 }%step
{ #3 - #2 }%final value
{ \formula_func_aux:n { ##1 } \\ }%code
\formula_func_aux:n { #3 } %last line without \\
}
\cs_new:Npn \formula_func_aux:n #1
{
\int_gset:Nn \g_tmpa_int { #1 }
y \sb{ \int_use:N \g_tmpa_int } &=
\int_gset:Nn \g_tmpb_int { 2 * \g_tmpa_int -1 }
A \sb{ \int_use:N \g_tmpa_int , \int_use:N \g_tmpb_int }
x \sb{ \int_use:N \g_tmpb_int }
+
\int_gset:Nn \g_tmpb_int { 2 * \g_tmpa_int }
A \sb{ \int_use:N \g_tmpa_int , \int_use:N \g_tmpb_int }
x \sb{ \int_use:N \g_tmpb_int }
}
\ExplSyntaxOff
\begin{document}
\begin{align}
\Formula{2}
\end{align}
\begin{align}
\Formula[1][2]{6}
\end{align}
\end{document}
答案3
这也不使用计数器,但可能比 jfbu 的简单一些,但想法是一样的。
\documentclass{article}
\usepackage{amsmath}
\newcommand\y[1]{%
y_{\the#1}&=A_{\the#1\,\the\numexpr2*#1-1\relax}
+A_{\the#1\,\the\numexpr2*#1\relax}
x_{\the\numexpr2*#1\relax}}
\def\lp#1#2#3{\ifnum#1=#3\relax\stoplp\fi#2{\numexpr#1\relax}\\\lp{\numexpr#1+1\relax}#2{#3}}
\def\stoplp#1\\#2#3#4#5{#1}
\begin{document}
\begin{align}
\lp1\y{10}
\end{align}
\end{document}
答案4
一种相当通用的方法,具有“简单宏”和完全可定制的复杂宏。
\documentclass{article}
\usepackage{xparse,amsmath}
\ExplSyntaxOn
% #1 = number of x's
% #2 = number of equations
\NewDocumentCommand{\makeequations}{O{2}m}
{
\egreg_makeequations:nnnnnn { x } { y } { A } { 1 } { #1 } { #2 }
}
\tl_new:N \l_egreg_equations_tl
\seq_new:N \l_egreg_equation_seq
% #1 = variables on RHS
% #2 = variable on LHS
% #3 = coefficient letter
% #4 = starting point
% #5 = number of variables in RHS
% #6 = number of equations
\cs_new_protected:Npn \egreg_makeequations:nnnnnn #1 #2 #3 #4 #5 #6
{
\tl_clear:N \l_egreg_equations_tl
\int_step_inline:nnnn { #4 } { 1 } { #4+#6-1 }
{
\egreg_makeequation:nnnnn { #1 } { #2 } { #3 } { #5 } { ##1 }
\int_compare:nF { ##1 = #4+#6-1 }
{ \tl_put_right:Nn \l_egreg_equations_tl { \\ } }
}
\use:x
{
\exp_not:n { \begin{align} }
\exp_not:V { \l_egreg_equations_tl }
\exp_not:n { \end{align} }
}
}
\cs_new_protected:Npn \egreg_makeequation:nnnnn #1 #2 #3 #4 #5
{
\tl_put_right:Nn \l_egreg_equations_tl { #2\sb{#5} & = }
\seq_clear:N \l_egreg_equation_seq
\int_step_inline:nnnn { #4 * (#5-1) + 1 } { 1 } { #4 * #5 }
{
\seq_put_right:Nn \l_egreg_equation_seq
{
#3\sb{#5 \egreg_comma:nn { #5 } { ##1 } ##1}#1\sb{##1}
}
}
\tl_put_right:Nx \l_egreg_equations_tl
{ \seq_use:Nnnn \l_egreg_equation_seq {+}{+}{+} }
}
\cs_new:Npn \egreg_comma:nn #1 #2
{
\bool_if:nT
{ \int_compare_p:n {#1 > 9} || \int_compare_p:n { #2 > 9 } }
{ , }
}
\keys_define:nn { makeequations }
{
LHSvar .tl_set:N = \l_egreg_eqs_lhsvar_tl, LHSvar .initial:n = y,
RHSvar .tl_set:N = \l_egreg_eqs_rhsvar_tl, RHSvar .initial:n = x,
COEFF .tl_set:N = \l_egreg_eqs_coeff_tl, COEFF .initial:n = A,
EQS .int_set:N = \l_egreg_eqs_eqs_int,
VARS .int_set:N = \l_egreg_eqs_vars_int, VARS .initial:n = 2,
START .int_set:N = \l_egreg_eqs_start_int, START .initial:n = 1,
}
\NewDocumentCommand{\xmakeequations}{m}
{
\group_begin:
\keys_set:nn { makeequations } { #1 }
\egreg_makeequations:VVVVVV
\l_egreg_eqs_rhsvar_tl
\l_egreg_eqs_lhsvar_tl
\l_egreg_eqs_coeff_tl
\l_egreg_eqs_start_int
\l_egreg_eqs_vars_int
\l_egreg_eqs_eqs_int
\group_end:
}
\cs_generate_variant:Nn \egreg_makeequations:nnnnnn { VVVVVV }
\ExplSyntaxOff
\begin{document}
\makeequations{3}
\makeequations[3]{4}
\xmakeequations{EQS=3}
\xmakeequations{EQS=4,VARS=3,COEFF=R,LHSvar=p,RHSvar=q,START=100}
\end{document}
在简单的宏中,您只需说明方程的数量,以及右侧使用的变量的数量(可选)。
在复杂的宏中,使用键值语法,您还可以更改变量和系数使用的字母,以及起点。例如,您可以使用以下方式进行链式调用
\xmakeequations{EQS=3}
Some text in between
\xmakeequations{EQS=4,START=4}