# Math Mode – How to Separate Long Math Text Under Sum Symbol into Different Lines

equationsmath-mode

I have the following equation, where there is a lot of stuff under the sum symbol:

$$d(\vec{x},\vec{y})= \sum_{Z_{xy}\in\vec{Z}_{xy},\forall x\in\vec{x},\forall y\in\vec{y}} f(Z_{xy})$$


In the resulting document, I find it kind of hard to read. Is there a way to write the equation to make the result more readable, e.g. putting the stuff under the sum symbol on different lines?

You can use the \substack command from the amsmath package, like this:

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


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:

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


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.

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


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.