[Tex/LaTex] Complicated alignment within multiline equation

equationshorizontal alignment

I'm having trouble getting the desired alignment behavior in a set of "sets" that I'm typesetting. I have a bunch of set definitions of the form

$$U = \{\text{$x$ $|$ $x$ satisfies some condition}\}$$

Of course, I want the equals signs to line up, which is easy enough to do with an align* environment.

The problem is that for some of the sets, the "some condition" takes a couple of lines to state. I'd like like those lines to align after the | symbol.
Of course, if I only had one set to define, then I could do the previous thing with the align* environment.

However, I want to achieve both of the above behaviors in the same set of equations. How do I do this?

EDIT: After seeing frabjous's answer below, I realized that I didn't emphasize (or even mention) one of the key difficulties. The U's and x's in my set definition are actually expressions with different lengths, so the second align point in each set definition is different. This prevents me from using an array environment. For instance, I could have something like the following:

X = {(p,q) | p=q+7 and something else very long that doesn't fit
on one line}

Y(1) = {x | x is prime}

Z' = {(a,b,c) | the variables a, b, and c satisfy a condition that
is too long for one line}.

Sorry for not using tex in the above, but I think it makes it clear what is happening.

Thanks for any help.

EDIT 2: Thanks to Antal S-Z's nice answer, I was able to create a macro that does exactly what I wanted. In case anyone desires the same behavior, here it is.

\newcommand\Set[3]{\ensuremath{\{\text{#1 $|$ \parbox[t]{\widthof{#3}}{#2\}}}}}

You call it with 3 arguments. To get a set {x|P}, the first argument should be x, the second P (with newlines in it) and the third the longest line in your condition P. This is compatible with align*, so you can then do things like

\begin{align*}
A &= \Set{x}{P}{Q}\\
B &= \Set{y}{R}{S}
\end{align*}

and it typesets things correctly.

Best Answer

I'd do this with an alignat* environment, but just put the text inside a parbox. That way, you can just align every equals sign and vertical bar; the line breaking will leave the text after the bar in ever case. This works, but is unfortunately slightly complicated. The best way to typeset a set comprehension (using etex) is with something like \left\{ x \;\middle|\; \phi \right\}1,2, but apparently you can't put an & between a \left and a \right. Manual scaling works, though, which gives this solution:

\newcommand{\pctext}[2]{\text{\parbox{#1}{\centering #2}}}
\begin{alignat*}{2}
  X    &= \biggl\{(p,q)   &&\;\bigg|\; \pctext{1.5in}{$p=q+7$ and something else
                                                      very long}\biggr\} \\
  Y(1) &= \{x             &&\;|\;      \pctext{1.5in}{$x$ is prime}\} \\
  Z'   &= \Biggl\{(a,b,c) &&\;\Bigg|\; \pctext{1.5in}{the variables $a$, $b$,
                                                      and $c$ satisfy a
                                                      condition that is too long
                                                      for one line}\Biggr\} \\
\end{alignat*}

This gives the following output:

Multiply aligned set comprehensions.

As you can see, this does work, though I think it's not ideal. There are some spacing issues (which I'm not sure how to fix, or even what best to do) stemming from the differing sizes of the braces. And the code's a bit cluttered; one could probably write a macro to abstract some of the boilerplate, but enclosing & in macros with align and friends tends to be problematic.


1: Actually, I typically use the braket package to write \Set{x | \phi}, but it uses \left and \right (and \middle if it can) under the hood.

2: Since it's assumed that | is a delimiter here, it's not spaced out, and so we use \;s to get the spacing right. If we use a relation such as \mid, we get the same (I think) spacing, but we can't \middle it.