Set Builder Notation
I created macros to simplify writing sets with set builder notation, so that "\setdef{x \suchthat x < 0}
" produces \(\{x \mathrel{|} x < 0\}\). The heights of the braces and center bar automatically adjust to the height of the contents of the braces, and the center bar has appropriate spacing on either side.
Set definitions often include conjunctions ("and"s) and disjunctions ("or"s), so we define \and
and \or
so they can be used within the argument of \setdef
to create nicely formatted logical statements. I used the convention that "or" is written out, but "and" is represented by a comma. To use \(\vee\) for "or" and \(\wedge\) for "and", modify the definitions to \def\or{\vee}
and \def\and{\wedge}
.
Examples
Code | Output |
---|---|
\setdef{x \suchthat P(x) \or Q(x)} |
$$\left\{x \mid P(x) \text{ or } Q(x) \right\} $$ |
\setdef{x \suchthat \frac{1}{1+x^2} = 1 \and x > 0} |
$$\left\{x \mathrel{}\middle|\mathrel{} \frac{1}{1+x^2} = 1,\ x > 0\right\} $$ |
\setdef{A, B, C} |
$$\left\{A, B, C\right\} $$ |
Definition
Code:
% To insert a set {A | B} (using set builder notation), % type "\setdef{A \suchthat B}". The heights of the braces % and center bar will adjust automatically to the height % of the contents. \newcommand*{\setdef}[1]{ \begingroup \def\or{\textup{ or }}% \def\and{,\ }% \left\{#1 \right\}% \endgroup } \newcommand{\suchthat}{% \ifnum\currentgrouptype=16 % If the command is used between "\left" and "\right", % then use "\middle" to make "|" match the height of % the delimiters. \mathrel{}\middle|\mathrel{} \else % Otherwise, we just insert "|" with the spacing of a % math relation. \mathrel{|} \fi }Output:
VS Code Snippets
In VS Code, the following code can be added to the LaTeX snippet file to enable nice autocompletion of the macro:"Set builder notation":{ "prefix": ["\\setbuild"], "body": [ "\\setdef{$TM_SELECTED_TEXT${1:x} \\suchthat $2} $0" ] }, "Set definition":{ "prefix": ["\\setdef"], "body": [ "\\setdef{$TM_SELECTED_TEXT$1} $0" ] }
More LaTeX macros are available at paulwintz.com/latex-macros.