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}
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}
  • 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}? – C.F.G Jan 14 '18 at 17:55
  • 2
    @C.F.G: I've added that option. – Werner Jan 14 '18 at 17:55
12

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

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

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.