如何定义一个\seq[StartIndex]{Base}{EndIndex}
具有以下功能的新命令
\seq{a}{n} % -> a_1,a_2,\dots,a_n
\seq[3]{a}{n} % -> a_3,a_4,\dots,a_n
\seq[k]{a}{n} % -> a_k,a_{k+1},\dots,a_n
答案1
通过正则表达式匹配,我们可以判断一个参数是否仅由数字和结果行为组成。
提供了具有“开放式”序列和子序列的扩展语法。
\documentclass{article}
\usepackage{xparse,l3regex,amsmath}
\ExplSyntaxOn
\NewDocumentCommand{\seq}{O{1}omm}
{
\IfNoValueTF{#2}
{ \sayol_seq:nnn {#1} {#3} {#4} }
{ \sayol_seq:nnnn { #1 } { #2 } { #3 } { #4 } }
}
\cs_new_protected:Nn \sayol_seq:nnn
{
#2\sb{#1},
#2\sb
{
\regex_match:nnTF { \A\d*\Z } { #1 }
{ \int_to_arabic:n { #1 + 1 } }
{ #1 + 1 }
}
,
\str_if_eq:nnTF { #3 } { * }
{ \dotsc }
{ \dots,#2\sb{#3} }
}
\cs_new_protected:Nn \sayol_seq:nnnn
{
#3\sb{#1\sb{#2}},
#3\sb{#1\sb
{
\regex_match:nnTF { \A\d*\Z } { #2 }
{ \int_to_arabic:n { #2 + 1 } }
{ #2 + 1 }
}}
,
\str_if_eq:nnTF { #3 } { * }
{ \dotsc }
{ \dots,#3{\sb{#1\sb{#4}}} }
}
\ExplSyntaxOff
\begin{document}
\verb!\seq{a}{n}! produces $\seq{a}{n}$
\verb!\seq{a}{*}! produces $\seq{a}{*}$
\verb!\seq[k][1]{a}{n}! produces $\seq[k][1]{a}{n}$
\verb!\seq[k][r]{a}{n}! produces $\seq[k][r]{a}{n}$
\verb!\seq[3]{a}{n}! produces $\seq[3]{a}{n}$
\verb!\seq[k]{a}{m}! produces $\seq[k]{a}{m}$
\verb!\seq[l]{b}{m}! produces $\seq[l]{b}{m}$
\verb!\seq[l]{b}{*}! produces $\seq[l]{b}{*}$
\end{document}
答案2
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\cs_new:Nn \sayol_seq_typeset:nnnn
{ {#1}\sb{#2}, {#1}\sb{#3}, \dots, {#1}\sb{#4} }
\cs_generate_variant:Nn \sayol_seq_typeset:nnnn { nnfn }
\cs_new:Nn \sayol_seq_calc:nnn % a 1 n
{ \sayol_seq_typeset:nnfn {#1} {#2} { \int_eval:n { #2 + 1 } } {#3} }
\NewDocumentCommand \seq { s O{1} m O{n} }
{
\IfBooleanTF {#1}
{ \sayol_seq_typeset:nnnn {#3} {#2} {#2+1} {#4} }
{ \sayol_seq_calc:nnn {#3} {#2} {#4} }
}
%%%%% alt syntax
\clist_new:N \l__sayol_tmp_clist
\int_new:N \l__sayol_tmp_int
\cs_new:Nn \sayol_seq_multi:nn
{
\clist_set:Nn \l__sayol_tmp_clist {#2}
\int_zero:N \l__sayol_tmp_int
\clist_map_inline:Nn \l__sayol_tmp_clist
{
\int_incr:N \l__sayol_tmp_int
\int_compare:nNnTF
{ \clist_count:N \l__sayol_tmp_clist }
=
{ \l__sayol_tmp_int }
{ \dots, {#1}\sb{##1} }
{ {#1}\sb{##1}, }
}
}
\NewDocumentCommand \Seq { O{a} m }
{ \sayol_seq_multi:nn {#1} {#2} }
\ExplSyntaxOff
\begin{document}
\[ \seq{a}[n] \] % -> a_1,a_2,\dots,a_n
\[ \seq[3]{a}[n] \] % -> a_3,a_4,\dots,a_n
\[ \seq*[k]{a}[n] \] % -> a_k,a_{k+1},\dots,a_n
\[ \Seq{1,2,n} \] % -> a_1,a_2,\dots,a_n
\[ \Seq[b]{1,2,5,n} \] % -> a_1,a_2,\dots,a_n
\end{document}
答案3
按照要求:
代码:
\documentclass{article}
\usepackage{amsmath}
\newcommand\seq[1][1]{%
\def\ArgI{#1}%
\seqt%
}
\def\eatrelax#1\relax{}
\newcommand\seqt[3][1]{%
\expandafter\eatrelax\ifnum0=0\ArgI\relax%
#2_{\ArgI},#2_{\ArgI+1},\ldots,#2_#3
\else\relax
#2_{\ArgI},#2_{\the\numexpr\ArgI+1\relax},\ldots,#2_#3
\fi
}
\begin{document}
\verb!\seq{a}{n}! produces $\seq{a}{n}$
\verb!\seq[3]{a}{n}! produces $\seq[3]{a}{n}$
\verb!\seq[k]{a}{m}! produces $\seq[k]{a}{m}$
\verb!\seq[l]{b}{m}! produces $\seq[l]{b}{m}$
\end{document}
我用了一个变体大卫·卡莱尔的回答到方法:如果参数是数值...否则。