18

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?

12

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}

enter image description here

  • 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
  • 1
    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
12

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}

enter image description here

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.)

  • 1
    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
  • 1
    Cool.. Don't know why it never occurs to me to use \middle. – Peter Grill Feb 25 '12 at 4:33
  • 1
    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
  • 1
    Thanks for showing this. Is this documented anywhere? – user10274 Feb 25 '12 at 4:45
  • 1
    texdoc etex, page 20, at the top. – Ryan Reich Feb 25 '12 at 4:51
9

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:

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.