12

I want to make a new command that has three arguments:

  1. symbol (e, \omega,\cdots)
  2. superscript or subscript (^ or _)
  3. counter (n)

If n=3 then returns

\set{a}{^}{3}    ====> output: {a^1,a^2,a^3}

otherwise the output is as follow

\set{e}{_}{n}    ====> output: {e_1,\cdots, e_n}

3 Answers3

13

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}
Werner
  • 603,163
  • 2
    i would use \dots or \ldots precisely. Plus may be an auxiliar definition to have easier input \def\set#1{\setaux#1\relax} and \def\setaux#1#2#3\relax{..} that way one can write \set{e_3} or \set{\alpha^22} and get all set rather than \set{\alpha}{^}{22}. – Manuel Jan 14 '18 at 17:44
  • Is it possible to define this command just by one input : \set{\alpha^m}? –  Jan 14 '18 at 17:55
  • 2
    @C.F.G: I've added that option. – Werner Jan 14 '18 at 17:55
  • @Werner: Sorry for comment. My TeXlive 2021 has a few problems with the above code. Your MWE is working very well but in one of my long TeX writings it has problem with \set but it is OK with \sett. What is the problem? – C.F.G May 28 '21 at 07:08
  • @C.F.G: Perhaps you're writing the content to the .aux? – Werner May 31 '21 at 17:50
  • @Werner: I don't understand your comment! you meant I define that macro inside the .aux file? No I define it inside the .tex file. So why it works for \sett? – C.F.G May 31 '21 at 17:53
  • What does \show\set provide in your .log just before defining it? Perhaps \set is already defined by another package. – Werner May 31 '21 at 17:56
13

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

egreg
  • 1,121,712
3

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

bmv
  • 3,588