Based on the examples in the mathtools
documentation, here are some easy-to-use macros for absolute values and set builders. In addition to mathtools, they also use etoolbox
and xparse
.
For sets, only one argument is necessary: the syntax is \set{x ; P(x)}
, producing {x|P(x)}
, where the braces and the vertical bar are adjusted to the size of the contents, automatically with the \Set
command (a more natural and easy to remember notation for \set*
from mathtools, in my opinion), manually with the \set
command. Another difference with the mathtools version is that size of the manual version defaults to \big
, as I find the non adjusted version most of the time looks too small. If you prefer the original version, it suffices to comment the (very classical) patch.
Of course, as the semi-colon is used as a separator between the elements and the properties that defines them, any other ;
should be written {;}
. This is unlikely to happen very often.
The macro also works for sets defined as lists (no defining property).
Here is an illustration:
\documentclass[10pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{mathtools,nccmath}%
\usepackage{ etoolbox, xparse}
\DeclarePairedDelimiterX{\abs}[1]\lvert\rvert{\ifblank{#1}{\,\cdot\,}{#1}}
\let\oldabs\abs
\def\abs{\futurelet\testchar\MaybeOptArgAbs}
\def\MaybeOptArgAbs{\ifx[\testchar\let\next\OptArgAbs
\else \let\next\NoOptArgAbs\fi \next}
\def\OptArgAbs[#1]#2{\oldabs[#1]{#2}}
\def\NoOptArgAbs#1{\ifblank{#1}{\oldabs{}}{\oldabs[\big]{#1}}}
\def\Abs{\oldabs*}
\DeclarePairedDelimiterX{\set}[1]\{\}{\setargs{#1}}
\NewDocumentCommand{\setargs}{>{\SplitArgument{1}{;}}m}
{\setargsaux#1}
\NewDocumentCommand{\setargsaux}{mm}
{\IfNoValueTF{#2}{#1}{\nonscript\,#1\nonscript\;\delimsize\vert\nonscript\:\allowbreak #2\nonscript\,}}
%%% Syntaxe : \set{x ; P(x)})
\let\oldset\set
\def\set{\futurelet\testchar\MaybeOptArgSet}
\def\MaybeOptArgSet{\ifx[\testchar \let\next\OptArgSet
\else \let\next\NoOptArgSet \fi \next}
\def\OptArgSet[#1]#2{\oldset[#1]{#2}}
\def\NoOptArgSet#1{\OptArgSet[\big]{#1}}
\def\Set{\oldset*}%
\begin{document}
Let $ \abs{} $ denote the \emph{absolute value} function. We have
\[\Abs{\frac{x}{y}} =\frac{\abs{x}}{\abs{y}} \]%
\[ \abs[]{z}\quad \abs{z}\quad \abs[\Big]{z}\quad \abs[\bigg]{z}\quad \abs[\Bigg]{z} \]%
Let $ \mathbf{ U} = \set{z \in \mathbf C ; \abs[]{z} = 1}$ and $ \mathbf U_3 = \Set{1,\dfrac{-1 + i\sqrt{3}}{2},\dfrac{-1-i\sqrt{3}}{2}} = \set{1,\frac{1}{2}(-1 + i\sqrt{3}),\frac{1}{2}(-1-i\sqrt{3})} = \set[]{1,\mfrac{1}{2}(-1 + i\sqrt{3}),\mfrac{1}{2}(-1-i\sqrt{3})}$.
\end{document}
