6

How can I define a new command called \seq[StartIndex]{Base}{EndIndex} with the following functionalities

\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
5
  • would it be acceptable to use \seq* in the case of k? Jul 9, 2015 at 3:30
  • Also, I'd recommend something more like \seq[a]{1,2,n} Jul 9, 2015 at 3:35
  • Did you mean \seq*[k]{a}{n}?
    – Say OL
    Jul 9, 2015 at 3:36
  • Yes (………filler text) Jul 9, 2015 at 3:39
  • @SeanAllred: yes, that is pretty fine.
    – Say OL
    Jul 9, 2015 at 3:41

3 Answers 3

3

With a regular expression match we can decide whether an argument only consists of digits and act of consequence.

An extended syntax featuring “open ended” sequences and subsequences is provided.

\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}

enter image description here

4
  • For example how could I write the factorial sequence? 1,1,2,...,n!,... or the powers of 2? 1,2,4,...,2^n,... Jul 9, 2015 at 10:09
  • 1
    @SophiaAntipolis That's a very different request.
    – egreg
    Jul 9, 2015 at 10:20
  • 1
    Maybe you could ask the question and self-answer, egreg ;-) Jul 9, 2015 at 12:30
  • The package l3regex should no longer be loaded with recent TeX distributions.
    – egreg
    Apr 15, 2018 at 8:26
5
\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}

output

4
  • Related: github.com/latex3/svn-mirror/issues/221 Jul 9, 2015 at 4:10
  • How about your recommendation \seq[a]{1,2,n}? I understood that you referred to if we use \seq{1,2,n}, it will produce 1,2,...,n
    – Say OL
    Jul 9, 2015 at 4:36
  • @SayOL That one is implemented in \Seq Jul 9, 2015 at 4:40
  • 1
    Then what about a_{k_1},a_{k_2},\dots,a_{k_n}? It will turn up eventually ;-)
    – daleif
    Jul 9, 2015 at 7:35
4

As requested:

enter image description here

The code:

\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}

I used a variation on David Carlisle's answer to Howto: if argument is numerical ... else.

2
  • What did you mean by \ifnum0=0?
    – Say OL
    Jul 9, 2015 at 7:03
  • 1
    @SayOL, that is not what it reads, it says \ifnum0=0\ArgI\relax, thus the test is false if \ArgI (which is expected to be an integer) is non-empty (and not 0). \relax is just there for the parser to stop looking for stuff for the comparison.
    – daleif
    Jul 9, 2015 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.