You can use the `\substack`

command from the `amsmath`

package, like this:

```
\begin{equation}
d(\vec{x},\vec{y}) =
\sum_{\substack{Z_{xy}\in\vec{Z}_{xy}\\
\forall x\in\vec{x}\\
\forall y\in\vec{y}}}
f(Z_{xy})
\end{equation}
```

However, the result still doesn’t look good, because of the extra spacing around the sum symbol:

To fix this, you can use the `\mathclap`

command from the `mathtools`

package, like this:

```
\begin{equation}
d(\vec{x},\vec{y}) =
\sum_{\mathclap{\substack{Z_{xy}\in\vec{Z}_{xy}\\
\forall x\in\vec{x}\\
\forall y\in\vec{y}}}}
f(Z_{xy})
\end{equation}
```

But perhaps you might be happy using **only** `\mathclap`

, and not `\substack`

. The result looks good as long as the subscript is not too wide.

```
\begin{equation}
d(\vec{x},\vec{y}) =
\sum_{\mathclap{{Z_{xy}\in\vec{Z}_{xy},
\forall x\in\vec{x},
\forall y\in\vec{y}}}} f(Z_{xy})
\end{equation}
```

The `mathtools`

package also have several other useful commands for typesetting mathematics, including more commands for improving the display of subscripts and superscripts. I very much recommend taking a look at its documentation.

As pointed out in the comments, you can place `\hfill`

before the "where" statement, which will push that statement all the way to the right. This will align it flush right with the equation number, however.

If you wish to avoid the "where" statement being aligned with the equation number, you can use `\hphantom{...}`

to add horizontal space at the end of the "where" statement to make it look as if it 'belongs' to the equation. The picture immediately below has been cropped so you can better compare these two approaches. The picture on bottom used `\hpantom{(2)}`

at the end of the "where" statement to add that extra horizontal space so that it is not immediately below the equation number.

Of course, if you end up moving equations around and equation number (1) becomes, say, equation number (10), then you will have to manually redefine all of the `\hpantom{...}`

commands. You can avoid this by using the `\label{...}`

and `\eqref{...}`

commands of `amsmath`

.

Here is an MWE that will always get the horizontal spacing correct, regardless of the equation number. (And, of course, if you do want the "where" statement to be all the way flush with the right margin, then just delete the `\hpantom{...}`

command.)

```
\documentclass{article}
\usepackage{amsmath} % needed for \label and \eqref commands to work; must be loaded before breqn
\usepackage{breqn}
\begin{document}
\begin{dmath}
\label{eq:log-likelihood} % used for getting the horizontal space right inside of the \hphantom command, regardless of equation number
\mbox{log-likelihood ratio} = A \log{ \left( \frac{A/(A+B)}{(A+C)/N} \right) } +
B \log{ \left( \frac{B/(A+B)}{(B+D)/N} \right)} +
C \log{ \left( \frac{C/(C+D)}{(A+C)/N} \right)} +
D \log{ \left( \frac{ D/(C+D) }{ (B+D)/N } \right)}
\end{dmath}
\hfill where $N = A + B + C + D$\hphantom{\eqref{eq:log-likelihood}}
\end{document}
```

## Best Answer

Since the text used in

`\underbrace`

(or`\overbrace`

) is typically set in a different font size to not distract further from the equation, it is preferable to use`amsmath`

's`\text`

macro.The difficult overlapping braces uses

`math`

over`lap`

s from`mathtools`

. It follows a process of setting the`\over`

-/`\underbrace`

text first with a`\phantom`

base, after which the base (or part thereof) is re-set. However, if those aren't needed, using`amsmath`

only would suffice for usage of`\text`

.There are a number of ways of achieving the overlapping output. Another method of overlapping braces is discussed in section

63.2 Overlapping bracesof the`mathmode`

document.