# New command for special elements in parenthesis

Sequences of elements like

$\{x_n\}_{n\in\mathbb{N}}$


are to awful to type. And they are very common, not only sequences of elements, but families of sets and some other sorts of mathematical structures.

I'd like to create a command in which I can type a letter, say x, and a set, say $\mathbb{N}$ such that the output be the sequence like

$\{x_n\}_{n\in\mathbb{N}}$

-
Welcome to TeX.sx! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. –  egreg Feb 13 '12 at 14:43

Although it’s preferred to see what you tried before, here’s an example how one can do it:

\documentclass{article}

\usepackage{amssymb}

\newcommand{\xinN}[2][N]{%
\{#2_n\}_{n\in\mathbb{#1}}%
}

\begin{document}
$$\xinN{x}$$

$$\xinN{y}$$

$$\xinN[C]{z}$$

At inline text $\xinN{k}$.
\end{document}


The command has a mandatory argument which is what you called x, and an optional argument which defaults to N.

-
For the reasons I give in tex.stackexchange.com/a/34837/575, I want to object to the use of \ensuremath. Basically, particularly since the asker is clearly a beginner, I think it is best for them to maintain a strong mental separation between the idea of "math TeX" and "text TeX". –  Ryan Reich Feb 13 '12 at 16:27
@RyanReich: Thank you for that comment. I removed the \ensuremath from my answer. –  Tobi Feb 14 '12 at 11:11

I need to learn to type faster. :)

For the sake of variety, I'll add another answer. My approach is similar to Tobi's, but it uses xparse instead:

\documentclass{article}

\usepackage{amssymb}
\usepackage{xparse}

\DeclareDocumentCommand{\elementinset}{O{N}m}{\{#2_n\}_{n\in\mathbb{#1}}}

\begin{document}

$\elementinset{x}$

$\elementinset[Z]{x}$

\end{document}


Personally, I like the way it handles mandatory and optional arguments. :)

Edit: \ensuremath is gone too. :)

-

if the set in the subscript is always blackboard bold, you can define a command like this:

\newcommand{\mycmd}[2]{\ensuremath
\{x_#1\}_{#1\in\mathbb{#2}}}


and use it like

\mycmd{n}{N}


the \ensuremath means that you don't have to use $...$ when it stands alone in running text. the first argument will fill in two positions (indicated by #1) and the second will provide the letter naming the set.

and of course you can use any name you want to replace \mycmd; it's best if you give it a name that actually means something.

-

Here is another solution using the xstring package so as to have a very easy way to write the sequences.

\documentclass{article}
\usepackage{amssymb}
\usepackage{xstring}

\makeatletter
\newcommand{\sequ}[1]{%
% No argument is one short cut for \sequ{u,n}.
\ifx\relax#1\relax
\def\@setFound{0}
\def\@name{u}
\def\@index{n}
\else
% Looking for one set.
\IfSubStr{#1}{;}{
\def\@setFound{1}
\StrBehind{#1}{;}[\@set]
\StrBefore{#1}{;}[\@withoutSet]
}{
\def\@setFound{0}
\def\@withoutSet{#1}
}
% Looking for one name and one possible index.
\expandafter\IfSubStr\expandafter{\@withoutSet}{,}{
% We have to use a trick so as to use \@withoutSet like one string variable.
% This trick was given to me by the author of the package xstring.
\expandafter\StrBefore\expandafter{\@withoutSet}{,}[\@name]
\expandafter\StrBehind\expandafter{\@withoutSet}{,}[\@index]
}{
\def\@name{\@withoutSet}
\def\@index{n}
}
\fi
% Display it !
\ensuremath{%
\ifnum\@setFound=1
\left( {\@name}_{\@index} \right)_{\@index \in \mathbb{\@set}} %
\else
\left( {\@name}_{\@index} \right)%
\fi
}%
}
\makeatother

\begin{document}

\verb+$\sequ{x,k;Z}$+ : $\sequ{x,k;Z}$

\medskip

\verb+$\sequ{u;N}$+ : $\sequ{u;N}$

\medskip

\verb+$\sequ{x}$+ : $\sequ{x}$

\medskip

\verb+$\sequ{}$+ : $\sequ{}$

\end{document}

-