41

I am looking for a pipe symbol that can be made to vary in height automatically.

One of the common ways of writing "such that" in mathematics is with the pipe symbol (|). The pipe can be represented in LaTeX directly as the character | or as \vert, but neither of these expands vertically in math presentation mode. I can adjust it manually this way:

\[\mathcal{H}=\left\lbrace h(x)=\sum_{i=0}^{k-1}a_{i}x_{i}\mod m\ \Bigg\vert
     \ a_{i}\in\mathbb{Z}_{m^{k}}\right\rbrace\]

Using \Bigg\vert

but I am looking for a true variable-sized option, parallel to \lbrace etc.

The amsmath package provides \lvert and \rvert, but I do not find them expanding as expected, and the amsmath documentation specifies that they should be used as binary delimiters (one on each side of something else), rather than alone.

7
  • 1
    As a side-note, rather use \[ ... \] for display math as opposed to $$ ... $$. See the l2tabu documentation.
    – Werner
    Oct 19, 2011 at 15:54
  • 1
    Not relevant to your question, but (as you can seen in our answers) you really shouldn't use $$ ... $$ for displayed math. See Why is [ .. ] preferable to $$ .. $$
    – Alan Munn
    Oct 19, 2011 at 15:54
  • @Werner, @Alan Munn, yes, I always use \[ ... ]`, but for some reason the code did not convert to mathematics and only the brackets were showing when I posted. So I thought I'd be safer with the $$`. I'll change them back now, though. Oct 20, 2011 at 3:32
  • not exactly relevant to the question asked, but what happened to the minus in the upper limit on the sum? Oct 20, 2011 at 11:38
  • 1
    @brannerchinese -- it's not that it's not nice looking; it's the wrong shape (too short and a bit fat; it's a hyphen, not a minus). take a look at the same symbol in the image in the answer by Alan Munn. i'm having trouble trying to figure out how you got a hyphen, which is normally restricted to text mode, not math. a coding incompatibility? Oct 21, 2011 at 6:22

4 Answers 4

36

You can use the braket package for this kind of thing.

\documentclass{article}
\usepackage{braket}
\usepackage{amsmath}
\usepackage{bbold}
\begin{document}

\[
\mathcal{H}=\Set{ h(x)=\sum_{i=0}^{k-1}a_{i}x_{i}\mod m\ |
     \ a_{i}\in\mathbb{Z}_{m^{k}}}
\]

\end{document}

It defines a \Set macro which automatically puts its contents in variable sized braces, and any instance of the | symbol inside will adjust accordingly. This solution also has the advantage of making your source code more semantic.

output of code

2
  • 2
    How about if there is a | symbol inside the set that denotes something else (e.g. conditional probability) and needs to be of normal size? May 29, 2014 at 23:06
  • 2
    @3lectrologos In the \Set macro, only the first | encountered will be large; other ones will be regular size. If you need a | on the left hand side of the large vertical, you can use \vert or \mid. For conditional probability I would use \mid.
    – Alan Munn
    May 30, 2014 at 1:40
33

You can also use \middle which goes with \left and \right:

\[
\mathcal{H}=\left\{ h(x)=\sum_{i=0}^{k-1}a_{i}x_{i}\mod m\ \middle|\ 
     a_{i}\in\mathbb{Z}_{m^{k}}\right\}
\]

This saves you having to use \left. or \right. unnecessarily.

2
  • 8
    One might add that \middle is an e-TeX feature. (The solution by Alan Munn will rely on this when available.) Oct 19, 2011 at 18:51
  • Ah, thank you - I had a vague recollection that this was a relatively recent feature but was mildly surprised to find I didn't need to load any packages to get this to work. That'll be why.
    – Ant
    Oct 19, 2011 at 19:12
13

You could use \left| stuff \right. or \left. stuff \right| depending on where you want the | and where it should take it's size from. For instance:

\[
  \left.\sum_{i \in I} x_i \right| x_i > 2
\]

takes the size from the sum. (Yes, I realize its a stupid example). Whereas

\[
  \sum_{i \in I} x_i \left| x_i > 2 \right.
\]

takes the size of the x_i > 2.

I see you added an example, that would become:

\[
  \mathcal{H}=\left\lbrace h(x)=\left.\sum_{i=0}^{k-1}a_{i}x_{i}\mod m\ \right|\ a_{i}\in\mathbb{Z}_{m^{k}}\right\rbrace
\]

With the result:

enter image description here

3
  • 1
    This doesn't work if you want to use the | in the following context: \left\{x | \sum_i x >10\right\} for example.
    – Seamus
    Oct 19, 2011 at 15:27
  • 4
    \left\{x \left| \sum_i x >10\right.\right\} will work fine if you want the | to be the size of the sum. Oct 19, 2011 at 15:31
  • Ah you need a second \right. and it needs a dot. OK.
    – Seamus
    Oct 19, 2011 at 16:02
9

Here is my solution with a syntax much closer to the mathematical meaning

\usepackage{mathtools}
\providecommand\given{} % is redefined in \Set
\newcommand\SetSymbol[1][]{\nonscript\:#1\vert \allowbreak\nonscript\:\mathopen{}}
\DeclarePairedDelimiterX\Set[1]{\{}{\}}{
  \renewcommand\given{\SetSymbol[\delimsize]}
  #1
}

Now we can simply use

\Set{ x\in A \given x^2 > 1 }

much much closer to the mathematical meaning.

In the OPs case, I'd use

\Set[\Big]{ h(x)=\sum_{i=0}^{k-1}a_{i}x_{i}\mod m \given  a_{i}\in\mathbb{Z}_{m^{k}} }

as there is no reason to scale the fences excessively.

I use the separate \SetSymbol macro, because there are very complicated set constructions, where one cannot use \Set (as in \{ and \} on separate lines), and thus in that case one may want to be able to refer to the given symbol for sets, just in case one want change the symbol later on.

Update: March 2015, added \mathopen{} and \allowbreak

You must log in to answer this question.

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