Vertical Stacking in Series Notation Using LaTeX Fractions

latext series stacking

LaTex is a powerful tool for representing mathematical notations and the canonical syntax for many scientific publications. The syntax can be difficult to learn initially and even seasons developers note quirks of the language. Series notation using the Sigma (summation) symbol is one such case.

LaTeX uses the \sum{i = 1}^{n} notation to express a series and terms. When used inside fractional notation this notation is forced into \textstyle notation such that the terms are condensed horizontally. This can produce unwanted results but, fortunately there are some simple solutions.

TL;DR – Use either \displaystyle\sum{}^{}  or \sum\limits_{}^{} notations to ensure vertical stacking of series terms in LaTeX.

The Problem

Consider the following:

y = \sum_{i=1}^n (x_{1i} - \bar{x_{1}})(x_{2i} - \bar{x_{2}})

This code produces the following image:

random summation series equation 1

This is all well-and-good and displays as expected. Now consider what happens when we place this series into the numerator position of a fractionally-notated value:

y = \frac{ \sum_{i=1}^n (x_{1i} - \bar{x_{1}})(x_{2i} - \bar{x_{2}})}{n-1}

This syntax produces the following image:

random summation series equation fraction
LaTeX syntax resulting in a textstyle formatting of the Sigma series notation. Note the horizontally-stacked element circled in yellow.

The Solution

This display may be the preferred format given that fractional notation can become somewhat top-heavy with series notation. However, for those seeking to show the series notation with the terms stacked as normal, this is an issue. Fortunately, there are several solutions:

Solution 1: Use Display Style

By default, LaTeX renders the \sum element in \displaystyle. However, terms of fractions are rendered in \textstyle by default. When a \sum element is placed as a term of a \frac{}{} element, it is forced into \textstyle notation. The fix is to prepend a \displaystyle specification  as such:

y = \frac{ \displaystyle\sum_{i=1}^n (x_{1i} - \bar{x_{1}})(x_{2i} - \bar{x_{2}})}{n-1}

This results in the following rendered image:

covariance equation stacked displaystyle notation
This syntax results in the vertical stacking of the series notation terms as expected in default \displaystyle convention

Solution 2: Use \limits Notation

The same result seen in solution 2 can be realized by using the \limits_ notation as a prefix to the \sum elements terms as such:

y = \frac{ \sum\limits_{i=1}^n (x_{1i} - \bar{x_{1}})(x_{2i} - \bar{x_{2}})}{n-1}

Final Thoughts

LaTeX is an incredibly useful tool for developing visual presentations of mathematical and scientific concepts. It comes with a learning curve and often is only used sporadically to generate piecewise images for non-LaTeX focused projects (like this one!) For a deeper discussion of the implications of \textstyle and \displaystyle conventions check out this article from overleaf.

Zαck West
Full-Stack Software Engineer with 10+ years of experience. Expertise in developing distributed systems, implementing object-oriented models with a focus on semantic clarity, driving development with TDD, enhancing interfaces through thoughtful visual design, and developing deep learning agents.