1

The packages braket and mathtools provide two different command to typeset set with dynamically sized braces.

For example with braket:

\documentclass{standalone}
\usepackage{braket}
\begin{document}
$\displaystyle \set{ \frac{x}{2} | x > 5 }$
$\displaystyle \Set{ \frac{x}{2} | x > 5 }$  
\end{document}

And the example with mathtools:

\documentclass{standalone}
\RequirePackage{mathtools}
\DeclarePairedDelimiterX\setc[2]{\{}{\}}{\,#1 \;\delimsize\vert\; #2\,}
\begin{document}
$\displaystyle \setc{ \frac{x}{2} }{ x > 5 }$
$\displaystyle \setc*{ \frac{x}{2} }{ x > 5 }$
$\displaystyle \setc[\bigg]{ \frac{x}{2} }{ x > 5 }$
\end{document}

(setc from https://tex.stackexchange.com/questions/448#124781)

I am currently using braket for its nicer interface. However, recently I found that in same cases I'd like to specify the bracket size manually as in the third case of the mathtools example. Is it somehow possible to combine the features of both packages into one command?

2

2 Answers 2

1

Using xparse on the same basis, here is a solution that allows a more natural syntax – at least closer to what one writes by hand: \set{x ; P(x)}. More over it lets one type enumerated sets, such as \set{1, 2, 3,…}. The only constraint is that if you have to use ; in the argument of \set, you have to write it as {;}.

I also rewrite \set* as \Set, and chose to define a default optional argument to \set with a classical trick – namely \big. My experience learnt me that plain \set looks generally too small. However, if you need or want it, you'll just have to write \set[].

Any way, this is the demonstration:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{fourier, heuristica}

\usepackage{xparse}
\usepackage{mathtools, nccmath}

\DeclarePairedDelimiterX{\set}[1]\{\}{\setargs{#1}}
\NewDocumentCommand{\setargs}{>{\SplitArgument{1}{;}}m}
{\setargsaux#1}
\NewDocumentCommand{\setargsaux}{mm}
{\IfNoValueTF{#2}{#1}{\,#1\nonscript\;\delimsize\vert\nonscript\:\allowbreak #2\,}}

%% The following makes \big the default for the \set command:
\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*}%

%%% Syntax: \set{x ; P(x)})
\begin{document}

\begin{align*}
   & \set{x ; x > 5} & & \Set{x ; x > 5}\\[4pt]
   & \set[\bigg]{ \frac{x}{2} ; x > 5} & & \Set{ \frac{x}{2} ; x > 5}\\[4pt]
 & \set[\Bigg]{ \frac{1}{2},\frac{1}{3},\frac{1}{4},\dotsm} & & \Set{ \mfrac{1}{2},\mfrac{1}{3},\mfrac{1}{4},\dotsm}
\end{align*}

\end{document} 

enter image description here

2
  • Thanks for sharing an alternitive solution. Do you use this also in your own documents or is it crafted just for me?
    – Meinersbur
    Jun 19, 2014 at 21:50
  • I made it a series of little things like that for a friend of mine, with a semi-colon as a set-builder symbol, but use your version for my documents. I guess I should use a key-value system to let the choice of the symbol to the final user.
    – Bernard
    Jun 19, 2014 at 22:05
4

Thanks to the hint from @Manuel and @egreg's answer https://tex.stackexchange.com/a/182943/45603 I found a nice solution.

\documentclass{standalone}

\usepackage{mathtools,xparse}
\NewDocumentCommand\set{sO{}m}{%
  \begingroup
  \IfBooleanTF{#1}
    {\setInn*{#3}}
    {\setInn[#2]{#3}}%
  \endgroup
}
\DeclarePairedDelimiterX\setInn[1]\{\}{%
  \activatebar
  #1%
}
\newcommand{\activatebar}{%
  \begingroup\lccode`~=`|
  \lowercase{\endgroup\def~}{\;\delimsize\vert\;}%
  \mathcode`|=\string"8000
}
\newcommand\Set{\set*}

\begin{document}
$\displaystyle \set{ \frac{x}{2} | x > 5 }$
$\displaystyle \set*{ \frac{x}{2} | x > 5 }$
$\displaystyle \set[\bigg]{ \frac{x}{2} | x > 5 }$
$\displaystyle \Set{ \frac{x}{2} | x > 5 }$  
\end{document}

(Kudos to cargo cult programming)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .