# 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:

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

\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\}$

\end{document}


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

-
It appears that this question has been discussed extensively: tex.stackexchange.com/questions/25398/…, tex.stackexchange.com/questions/5502/…, tex.stackexchange.com/questions/3137/…. Not to be rude to jamaicanworm, but I think all the ground has been covered and perhaps this should be closed as a duplicate of, say, the second one. –  Ryan Reich Feb 25 '12 at 4:53
I don't agree with this being a "duplicate". –  egreg Feb 25 '12 at 10:27
Related Question: Applying \middle outside of a \left \right group –  Peter Grill May 2 '12 at 19:08

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

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

\begin{document}
$a \suchthat b\qquad\left\{\frac{a}{b}\suchthat x\right\}$
\end{document}


-
This solution seems simpler to use than @Ryan Reich's (i.e. no need for the * when calling \suchthat). Does it have any disadvantages compared to Ryan's? Also, would you recommend using \; or \mathrel{} for spacing? –  jamaicanworm Feb 25 '12 at 16:12
Using \; or \mathrel{} can give different results only if before or after \suchthat you put a relation symbol, which is unlikely. I'd go with the explicit spacing or with \ifnum\currentgrouplevel=16 \;\middle|\;\else\mid\fi (but it seems overthinking). –  egreg Feb 25 '12 at 16:33
@egreg: shouldn't the \currentgrouplevel in your last comment be a \currentgrouptype? –  Philippe Goutet May 2 '12 at 22:05
@PhilippeGoutet Yes, of course. Thanks for noting. –  egreg May 2 '12 at 22:06

This can be done with the \middle construction, which goes between \left and \right and resizes it to match:

\documentclass{article}

\makeatletter
\newcommand\suchthat{%
\@ifstar
{\mathrel{}\middle|\mathrel{}}
{\mid}%
}
\makeatother

\begin{document}
$\left\{ \frac{a}{b} \suchthat* (a,b) = 1 \right\} \qquad \{ x \suchthat y \}$
\end{document}


I have also handled the outer spaces a little more elegantly than with direct \, directives using the \mathrel primitive, which declares its operand to be a "relation", which I think "such that" is.

For convenience, my \suchthat takes a star that makes it resizable, since \middle is invalid outside \left...\right. (In my opinion, fixed size is the default, so it is the unstarred version.)

-
I'd appreciate someone telling me how to do the equivalent of \mathrel{\middle|} or \middle\mid, either of which would, if valid, produce the correct spacing without the hack I used. –  Ryan Reich Feb 25 '12 at 4:28
Cool.. Don't know why it never occurs to me to use \middle. –  Peter Grill Feb 25 '12 at 4:33
This is the first time I actually tested what you need to load to get it; turns out, nothing. I seem to recall it's an eTeX extension, and as such, it always seems to get second-class treatment in tutorials I've read. Funny how that happens to eTeX despite being twenty years old. –  Ryan Reich Feb 25 '12 at 4:35
Thanks for showing this. Is this documented anywhere? –  Marc van Dongen Feb 25 '12 at 4:45
texdoc etex, page 20, at the top. –  Ryan Reich Feb 25 '12 at 4:51

One way would be to pass a parameter to the \suchthat macro so that it can use a \vphantom to re size. This requires you to determine what portion should be used to re-size.

A better approach would be to pass the entire right hand side to the \suchthat macro.

Both produce similar results. Here is a comparison of the original on the left and new version on the right:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}

\newcommand{\Oldsuchthat}{\, \mid \,}
\newcommand*{\suchthat}[1]{\left|\vphantom{#1}\right.}
\begin{document}
\begin{alignat}{2}
&S = \left\{ x_n \Oldsuchthat n \in \mathbb{N} \right\} &\quad
&S = \left\{ x_n \suchthat{n} n \in \mathbb{N} \right\} \\[2.0ex]
%
&S = \left\{ x_n \Oldsuchthat \frac{1}{n} \in \mathbb{N} \right\} &\quad
&S = \left\{ x_n \suchthat{\frac{1}{n}} \frac{1}{n} \in \mathbb{N} \right\}
\end{alignat}
\end{document}


## Alternate:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}

\newcommand{\Oldsuchthat}{\, \mid \,}
\newcommand*{\suchthat}[1]{\,\left|\, #1 \right.}
\begin{document}
\begin{alignat}{2}
&S = \left\{ x_n \Oldsuchthat n \in \mathbb{N} \right\} &\quad
%
&S = \left\{ x_n \suchthat{n \in \mathbb{N}}  \right\} \\[2.0ex]
&S = \left\{ x_n \Oldsuchthat \frac{1}{n} \in \mathbb{N} \right\} &\quad
&S = \left\{ x_n \suchthat{\frac{1}{n} \in \mathbb{N}} \right\}
\end{alignat}
\end{document}

-