Macros – Adjusting Mid-Bar in Set Builder Notation Command

macros

I would like to create a command which prints its arguments in a set builder notation. The problem

\documentclass{article} 
\newcommand{\set}[2]{\left\{\  #1  \ \left| \ #2 \ \right. \right\}  }


\begin{document}

$\set{A}{ (B) }$

\bigskip

$\set{A}{\Bigg(B\Bigg)}$

\bigskip

% but:

$\set{\Bigg(A\Bigg)}{B}$

\end{document}

enter image description here

is that my attempt to make the height of the middle bar automatically adjusted to the contents of the set, only works wih respect to the content of the right hand side of it. I know I could use the colon instead, but I just don't like the way it looks.

Is there any simple work around?

Thanks in advance

Best Answer

Here is a solution, adapted from a code in the documentation of mathtools, with a natural syntax: it uses two arguments in one, separated by a semi-colon. If the second argument does not exist, it also works, letting you define a set as a list of its elements.

The \set command has a starred version, adding a pair of implicit \left \right. It also can take an optional argument: \big,\Big, \bigg, \Bigg which adds a pair of implicit \bigl \bigr or one of its siblings.

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}
\usepackage{xparse}
%
\DeclarePairedDelimiterX{\set}[1]{\{}{\}}{\setargs{#1}}
\NewDocumentCommand{\setargs}{>{\SplitArgument{1}{;}}m}
{\setargsaux#1}
\NewDocumentCommand{\setargsaux}{mm}
{\IfNoValueTF{#2}{#1} {#1\nonscript\:\delimsize\vert\allowbreak\nonscript\:\mathopen{}#2}}%
\def\Set{\set*}%

\begin{document}

\begin{align*}
 & \set*{\frac{A}{B}; A, B\in K[X]} \\
 & \set[\Big]{A; A = \sqrt{B},\:B\in K[X]} \\
 & A\set[big]{2,3, 5,7,11,\dots}
\end{align*}

\end{document}

enter image description here