21

I am very lost. I haven't been liking LaTeX as it's quite hard for me to get the hang of it, so I've decided to practice on my own during the summer, but I am already stuck on something that seems very basic.

I want to write sets. How do I create the brackets {}? You know, say {2,4,6,8...} or {x|x is positive and even}. I tried ${2,4,6...}$ But this ignores the brackets.

I was searching if there is a specific command to create that say \Set{2,4,6...} or something similar. But no, it's an error and I cannot find such information.

I have given up, how do I create a set notation nicely??? Really simple things seem very... obscure to me with LaTeX and requires great hassle. Is there any where good, a website, for me to learn this? Had a hard time in university last year, some people got easily okay with it but this is just not my cup of tea.... Seems to unnecessarily complicate something that could be done simpler.... Help is greatly appreciated! Thanks in advance.

  • 2
    Welcome! This has nothing to do with latex3 so I will edit the tags in a moment. However, please try $\{thing 1, thing 2, thing 3\}$. – cfr Jul 1 '15 at 3:26
  • 2
    I'd recommend Kopka and Daly's A Guide to LaTeX. I realise that recommending books is a bit old-fashioned but I think it makes this kind of thing a lot easier, to be honest. All the basics covered in an order which makes sense and ensures that you have a sense of both the big picture and the details. The other thing is to make sure you have a good editor. Most editors will help you out with this kind of thing - especially with the code for symbols. – cfr Jul 1 '15 at 3:31
  • 1
    Welcome from me as well! My friend, it's no wonder you're not getting on with LaTeX if you haven't - haven't been taken through the basics (whether by a person, or a book). Myself, I actually got started with the wikibooks LaTeX manual (en.wikibooks.org/wiki/LaTeX), which I think is a very good place to start, but there are quite possibly better resources out there - @cfr's seem good. But if you don't understand the principles of LaTeX, you're not going to have an easy time with it. Not that learning by doing isn't the way to go - it is, but you have to understand 1st principles. – Au101 Jul 1 '15 at 3:41
  • 1
  • 2
    The general answers that help you learn LaTeX are good ones. There's an answer to your particular question here: tex.stackexchange.com/questions/209863/… – Ethan Bolker Jul 1 '15 at 16:09
38

To answer your specific question:

To quote from Leslie Lamport's "LaTeX: A Document Preparation System" (Leslie Lamport initially developed LaTeX):

The ten special characters

# $ % & ~ _ ^ \ { }

are used only in LaTeX commands.

...

Seven of those symbols can be produced by typing a \ in front of the corresponding character.

$ & % # _ { } are easy to produce.

In other words, to get { and } you must type \{ and \}. This is because { and } mean something very special to LaTeX. They are used to delimit arguments. So with something like \emph{Hi}, the emphasis starts at the { and finishes at the } (\emph is a command which is used to emphasise text; the standard behaviour is for it to italicise it). In other words, everything enclosed within the braces is emphasised.

As such, if you want to print the symbols { and }, you can't just type the plain symbol. Instead you must "escape" it. The \ is the escape character and it basically tells LaTeX to pay attention. emph is just a word like any other to LaTeX. But \emph is a command to LaTeX. What escaping a { does is cause LaTeX to print a { character.

So to get {2,4,6,8...}, all you need to do is type:

$\{2, 4, 6, 8\dots\}$

enter image description here

To get {x|x is positive and even}, you can do:

$\{x \mid x \text{ is positive and even}\}$

enter image description here

As raised in the comments - this relies on the amsmath package. Instead, you can do:

$\{x \mid x \mbox{ is positive and even}\}$

For most purposes, this has the same effect, but there are minor differences:

Difference between various methods for producing text in math mode

For really nice spacing, follow Hagen von Eitzen's advice and use:

$\{\,x \mid x \text{ is positive and even}\,\}$

enter image description here

As you can see, \, inserts a thin space. There are other commands for inserting space in LaTeX. However, \, is one of only two that can be used outside of math-mode.


But, really, it's important that you gain an understanding of the basic operating principles of LaTeX before you go further and start worrying about how to do specific things; otherwise you're gonna be completely at sea and everything's gonna seem like black magic and nonsense. So I really recommend you have a little look through some basic introductions before diving right in there.

However I learned by doing - I learned by trying it out, I learned by using LaTeX and looking things up whenever I wanted to do something I didn't yet know how to do. That's a very fulfilling, very enjoyable and very practical way to learn - although there are massive holes in my knowledge and, because my approach was very light on theory, I'm only just starting to learn about bad habits I've developed and principles I may have overlooked.

But still, as good as it is to dive in there and get your hands dirty, you have to, have to get a basic idea of what LaTeX is and how it works or you'll probably continue to have a pretty miserable time. It's no wonder you don't like it and everything seems arcane and difficult and a complete pain in the posterior to you!

  • Also, in setbuilder notations as this I'd add spacing as in $\{\,x \mid x \text{ is positive and even}\,\}$ – Hagen von Eitzen Jul 1 '15 at 9:46
  • Sorry @MarcvanDongen, didn't realise. If nothing else, it demonstrates quite nicely something I overlooked because I never read about it, because I just assumed I knew how to do it, because of the "dive right in" approach I took :P – Au101 Jul 1 '15 at 22:02
12

LaTeX puts at your disposal the package braket that helps you creating beautiful sets, kets and bras for the Dirac notation. In particular

\set{x, \mid x<2}

\ket{\psi}

\bra{\phi}

do the job as simple as that as shown below:

Let $A=\set{x\mid \psi(x)>0}$; also, given
$\ket{\psi}\in\mathcal{H}$, then $\psi(x)\coloneqq\braket{x|\psi}$.

enter image description here

  • 1
    Well, the \sets command from braket is definitely useful, but the question has nothing to do with bra and ket from Dirac notation in Hilbert space. I suggest to elaborate on your answer. – user31729 Jul 1 '15 at 21:58
  • To make your question useful and self contained, please make a MWE and also show the output itself. – Nasser Jul 1 '15 at 22:10
  • Is it not possible to display LaTeX outputs as per LaTeX tags in this section? I have tried (and failed) to display a working example. – gented Jul 1 '15 at 22:15
  • One saves a screen dump into a jpeg and crops it. Then, one can attach the image to their answer by clicking the icon that looks like a picture. – Steven B. Segletes Jul 2 '15 at 1:46
  • Thank you, I have done so. However, is there no way to show LaTeX markup inside the text? Other sections of the site do. – gented Jul 2 '15 at 20:15
7

Here's an option with mathtools package. I show here the different possible sizes and usage.

\documentclass{scrartcl}
\usepackage{mathtools}

\DeclarePairedDelimiterX\set[1]\lbrace\rbrace{\def\given{\;\delimsize\vert\;}#1}

\begin{document}
$\set{ x \given x > 0 }$
$\set[\big]{ x \given x > 0 }$
$\set[\Big]{ x \given x > 0 }$
$\set[\bigg]{ x \given x > 0 }$
$\set[\Bigg]{ x \given x > 0 }$
$\set*{ x \given x > 0 \rule{0cm}{2cm} }$ % \rule is just here so you can see that it autostretches
\end{document}

enter image description here

If you prefer to use \set{ x | x > 0 } rather than \set{ x \given x > 0 } you can do

 \DeclarePairedDelimiterX\set[1]\lbrace\rbrace{\setaux#1}
 \def\setaux#1|{#1\;\delimsize\vert\;}
  • To the pro users: I would welcome some help with the checking for a | inside the argument in the second option. I believe it has something to do with som sort of \def\setaux#1|#2|#3\endsetaux{\if\relax\detokenize{#2}\relax #1 \else #1 \;\delimsize\vert\; #2 \fi} and call it with \setaux #1||\endsetaux. But I'm not certain (it probably has an error). – Manuel Jul 1 '15 at 22:08
  • Manuel — looks about right to me :) – Will Robertson Jul 2 '15 at 1:19
1

I defined a new command \set based on xparse package. The result is nice-looking.

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand{\set}{o m}{%
  % <code>
  \IfNoValueTF{#1}
    {\{#2\}}
    {\{#1 \mid #2\}}%
  % <code>
}
\begin{document}
$\set{x_1, \dots}$ or ${\set[x \in X]{\phi(x)}}$
\end{document}

enter image description here

  • 1
    I edited your answer to make it more like an answer (with a complete and compilable code as well as an image of the result). If you don't like it, be free to roll back to the original version. – JouleV Feb 27 at 8:18

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.