LaTeX sum, integral and piecewise functions

Use \sum_{i=1}^{n}and \int_a^b in math mode. Piecewise functions use the cases environment fromamsmath.

sum-integral.tex
\documentclass{article}
\begin{document}
\[
\sum_{i=1}^{n} i = \frac{n(n+1)}{2}
\qquad
\int_0^1 x^2 \, dx = \frac{1}{3}
\]
\end{document}

The subscript sets the lower bound and the superscript the upper bound. Use braces for anything longer than one character.

The operators

CommandPackageMeaning
\sumcoreSummation
\prodcoreProduct
\intcoreIntegral
\iintamsmathDouble integral
\iiintamsmathTriple integral
\ointcoreContour (closed-path) integral
\limcoreLimit; the subscript goes below in display math

Where the limits go

In display math, \sum and \prod put their limits above and below the symbol. Inline, they move to the side so the line height stays even. \int always puts them at the side. Override this with \limits or\nolimits straight after the operator.

limits.tex
Inline: $\sum_{i=1}^{n} x_i$ and $\sum\limits_{i=1}^{n} x_i$.
\[
\int\limits_0^1 f(x)\,dx \qquad \sum_{i \in S} x_i
\]

To place two conditions under one sum, use \substackfrom amsmath:\sum_{\substack{0<i<n \\ i \text{ odd}}} x_i.

Multiple and contour integrals

\iint and \iiint come fromamsmath. \oint is core LaTeX.

multiple.tex
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\iint_D f(x,y)\,dA \qquad
\iiint_V \rho\,dV \qquad
\oint_C \mathbf{F} \cdot d\mathbf{r}
\]
\end{document}

Spacing before dx

Without help, dx sits too close to the integrand. Add a thin space with \,. Many authors also set the differential upright, with \mathrm{d}x, so it is not read as the product d·x. Pick one style and keep it.

dx.tex
\[
\int_0^\infty e^{-x}\,\mathrm{d}x = 1
\]

If you write \mathrm{d} often, define a macro in the preamble:\newcommand{\dd}{\mathrm{d}}.

Piecewise functions with cases

The cases environment from amsmath draws a large left brace. Each row has a value, an &, and a condition. Write the condition with \text{...} if it contains words.

cases.tex
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
|x| =
\begin{cases}
  x  & \text{if } x \ge 0, \\
  -x & \text{if } x < 0.
\end{cases}
\]
\end{document}

Common mistakes

Templates that use this

Sums and integrals fill calculus and physics write-ups. Start from theamsart article, themaths homework template, or theacademic journal templates.

Related

For the fraction inside the first example, seefractions. To line several equations up on the equals sign, seealign. Symbols such as\infty are listed in themath symbols reference. Browsearticle templates for complete documents.