How to define new command for \{e_1,\cdots, e_n\}?

The following is an elementary implementation of what you're after:

enter image description here

\documentclass{article}

\newcommand{\set}[3]{%
  \{ #1#2{1},
  \ifnum\pdfstrcmp{#3}{3}=0
    #1#2 2
  \else
    \ldots
  \fi
  , #1#2{#3} \}
}

\begin{document}

$\set{a}{^}{3}$

$\set{e}{_}{n}$

\end{document}

We condition on what is presented as argument #3 using \pdfstrcmp. \pdfstrcmp{<strA>}{<strB>} does a string comparison between <strA> and <strB>, returning -1 | 0 | 1 if <strA> < | = | > <strB> (lexicographically).

The only difference between the two sets is the way the second term in the sequence is being set, so those elements are set without consideration of the condition. No consideration is added for n=1 or n=2.


A supplementary interface using a single argument:

enter image description here

\documentclass{article}

\newcommand{\set}[1]{\setaux#1\relax}
\def\setaux#1#2#3\relax{%
  \{ {#1}#2 1,
  \ifnum\pdfstrcmp{#3}{3}=0
    {#1}#2 2
  \else
    \ldots
  \fi
  , {#1}#2{#3} \}
}

\begin{document}

$\set{a^3}$

$\set{e_n}$

$\set{\alpha^{22}}$

\end{document}

With expl3:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\set}{mmm}
 {
  \lbrace
  \str_case:nnF { #3 }
   {
    {1}{#1#2{1}}
    {2}{#1#2{1},#1#2{2}}
    {3}{#1#2{1},#1#2{2},#1#2{3}}
   }
   { #1#2{1},\dots,#1#2{#3} }
   \rbrace
 }
\ExplSyntaxOff

\begin{document}

$\set{a}{^}{3}$

$\set{a}{^}{n}$

$\set{e}{_}{2}$

$\set{e}{_}{k}$

\end{document}

enter image description here

Alternative syntax and implementation: the call is \set[<opt>]{<letter><^|_>} where <opt> is a number or a generic subscript. In case of a number, the sequence is spelt out fully. In the unusual case you want a number, but you prefer the dots, use [{{9}}] like in the last example. The mandatory argument has the form a^ or a_.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\set}{O{n}m}
 {
  \lbrace
  \regex_match:nnTF { \A \d+ \Z } { #1 }
   {
    #2{1}
    \int_step_inline:nnnn { 2 } { 1 } { #1 }
     {
      ,#2{##1}
     }
   }
   {
    #2{1},\dots,#2{#1}
   }
  \rbrace
 }
\ExplSyntaxOff

\begin{document}

$\set[3]{a^}$ $\set{a^}$ $\set[k]{a^}$

$\set[2]{e_}$ $\set[4]{e_}$ $\set[{{9}}]{e_}$

\end{document}

enter image description here


This solution uses etoolbox and pgffor. Credits for @TH.'s answer on Testing for number.

This implementation also handles \mycmd{a}{^}{K}, where K is any positive integer.

\documentclass{article}

\usepackage{etoolbox}
\usepackage{pgffor}
\makeatletter
\newcommand\ifnumber[1]{%
    \begingroup
    \edef\temp{#1}%
    \expandafter\ifstrempty\expandafter{\temp}
    {\endgroup\@secondoftwo}
    {\expandafter\ifnumber@i\temp\@nnil}%
}
\def\ifnumber@i#1#2\@nnil{%
    \if-#1%
    \ifstrempty{#2}
    {\def\temp{X}}
    {\def\temp{#2}}%
    \else
    \def\temp{#1#2}%
    \fi
    \afterassignment\ifnumhelper
    \count@0\temp\relax\@nnil
    \endgroup
}

\def\numrelax{\relax}%
\def\ifnumhelper#1\@nnil{%
    \def\temp{#1}%
    \ifx\temp\numrelax
    \aftergroup\@firstoftwo
    \else
    \aftergroup\@secondoftwo
    \fi
}
\makeatother

\newcommand{\mycmd}[3]{%
        \ifnumber{#3}{%
                \ensuremath{%
                    \foreach \n in {1,...,#3}{#1#2{\n}\ifnum \n < #3 {,} \fi}
            }
        }{%
        \ensuremath{#1#2 1,\ldots,#1#2{#3}}%
    }%
}

\begin{document}

\mycmd{1}{^}{4}

\mycmd{a}{^}{3}

\mycmd{1}{^}{1}

\mycmd{a}{^}{n}

\mycmd{b}{_}{6}

\mycmd{3}{_}{n}

\end{document}

enter image description here