[Tex/LaTex] Creating a large “such that” symbol


I'm using the following macro in my LaTeX document:

\newcommand{\suchthat}{\, \mid \,} % nice "such that"

so that set notation like the following looks nice:

\{x_n \suchthat n \in \mathbb{N}}

However, when I use such definitions of sets inside \[...\] and when I need to make the brackets bigger, the \suchthat symbol looks way to small. Try compiling the following MWE as an example:

\usepackage{amssymb}                % for \mathbb font
\newcommand{\suchthat}{\, \mid \,}  % nice "such that"

\noindent This looks fine:
\[ S = \left\{ x_n \suchthat n \in \mathbb{N} \right\} \]

\noindent But this does not:
\[ S = \left\{ x_n \suchthat \frac{1}{n} \in \mathbb{N} \right\} \]


Is there something I can include in my macro to automatically resize the suchthat symbol as necessary?

Best Answer

Use \middle only if there's a \left before it; we can check this using \currentgrouptype (texdoc etex for more information):

\newcommand{\suchthat}{\;\ifnum\currentgrouptype=16 \middle\fi|\;}

a \suchthat b\qquad\left\{\frac{a}{b}\suchthat x\right\}

enter image description here