Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to list all elements of a set. I tried

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[\left \{0, 1, 2, 3, 4, 5, 6\right \}\]

\[\left \{0,\, 1, \,2, \, 3, \,4, \,5, \,6\right \}\]
\end{document}

enter image description here

Is there anyway that I do better?

share|improve this question
1  
What is wrong with \[ \{ 0, 1, 2, 3, 4, 5, 6 \} \]? –  Gonzalo Medina May 19 at 3:35

4 Answers 4

up vote 2 down vote accepted

The simplest trick by me.

\documentclass[border=12pt,12pt]{standalone}
\usepackage{pgffor}
\def\set[#1,#2]{$\{#1\foreach \x in {#2}{,\,\x}\}$}

\begin{document}
\set[1,2,3] and \set[1,] and an empty set \set[,]
\end{document}

enter image description here

Version 2

Removing $ to let you use other math environment.

\documentclass[preview,border=12pt,12pt,varwidth]{standalone}
\usepackage{amsmath}
\usepackage{pgffor}
\def\set[#1,#2]{\{#1\foreach \x in {#2}{,\,\x}\}}

\begin{document}
\[
    \set[1,2,3]
\]
 and $\set[1,]$ and an empty set $\set[,]$
\end{document}

enter image description here

share|improve this answer
    
@ Money Oriented Progra How can I put this code \[\set[1,2,3,\ldots, 100]\] in mathmode? –  minthao_2011 May 19 at 4:15
1  
Money Oriented Progra Thank you very much. –  minthao_2011 May 19 at 4:35

A “classical” definition, where the comma is math active and defined to give a comma followed by a thin space.

\documentclass{article}
\usepackage{mathtools}

\DeclarePairedDelimiterX{\listset}[1]{\lbrace}{\rbrace}{%
  \begingroup
  \begingroup\lccode`~=`, \lowercase{\endgroup
    \def~}{\mathchar\commacode\,}%
  \mathcode`,=\string"8000
  #1
  \endgroup
}
\AtBeginDocument{\edef\commacode{\the\mathcode`,}}

\begin{document}

\[\listset{0, 1, 2, 3, 4, 5, 6}\]

\[\{0,\, 1, \,2, \, 3, \,4, \,5, \,6\}\]

\[\listset[\big]{0, 1, 2, 3, 4, 5, 6}\]

\end{document}

Look in the documentation of mathtools what optional is accepted to a declared paired delimiter.

enter image description here

The same without activating the comma, but using expl3:

\documentclass{article}
\usepackage{mathtools,xparse}

\DeclarePairedDelimiterX{\listset}[1]{\lbrace}{\rbrace}{\listsetaux{#1}}

\ExplSyntaxOn
\NewDocumentCommand{\listsetaux}{m}
 {
  \seq_set_split:Nnn \l_minthao_listset_seq { , } { #1 }
  \seq_use:Nn \l_minthao_listset_seq { ,\, }
 }
\seq_new:N \l_minthao_listset_seq
\ExplSyntaxOff

\begin{document}

\[\listset{0, 1, 2, 3, 4, 5, 6}\]

\[\{0,\, 1, \,2, \, 3, \,4, \,5, \,6\}\]

\[\listset[\big]{0, 1, 2, 3, 4, 5, 6}\]

\end{document}
share|improve this answer

A slight improvement on @Money Oriented Programmer's answer:

  1. does not need workarounds for singleton and empty sets
  2. uses count key of pgffor so you'll need a recent version. An alternative is using an \iffirstelem custom conditional
  3. you can use notation like \set{1,...,50} or \set{a,...,g} which can be handy (see the pfg/tikz manual "Utilities > Repeating things")
  4. use of \ensuremath to allow use outside and inside of mathmode
  5. avoid using [] delimiters since the argument is not optional and {} brackets are more suggestive of an actual set

Here's the code:

\documentclass[border=12pt,12pt,convert=png]{standalone}
\usepackage{pgffor}
\def\set#1{%
    \ensuremath{%
        \ifx!#1!\emptyset\else
            \{%
                \foreach[count=\i] \x in {#1}{%
                    \ifnum\i>1,\,\fi%
                    \x%
                }%
            \}
        \fi%
    }%
}

\begin{document}
\set{1,...,3} and \set{1} and an empty set \set{}
\end{document}

preview

You may also be interested in the braket package.

share|improve this answer
    
@ Rordaigorl I can not use \[\set{1,...,15}\]. –  minthao_2011 May 19 at 15:26
    
@minthao_2011 that is probably just because you cannot use \[\] with the standalone class. Use it with a standard class such as article and you will be fine –  Bordaigorl May 19 at 16:23
    
Rordaigorl Thank you very much. –  minthao_2011 May 19 at 23:48

To someone with a hammer, everything looks like a nail.

Thus, I too have my little hammer:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xinttools}
\newcommand{\InsertExtraAfterCommas}[2][\,]
     {\xintListWithSep{,#1}{\xintCSVtoList {#2}}}

\begin{document}
\[\left \{0, 1, 2, 3, 4, 5, 6\right \}\]

\[\left \{\InsertExtraAfterCommas{0, 1, 2, 3, 4, 5, 6}\right\}\]

\[\left \{\InsertExtraAfterCommas[\;\;\;]{0, 1, 2, 3, 4, 5, 6}\right\}\]
\end{document}

extra spaces

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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