# Tips for LaTeX Math Formatting

by Justin Skycak on

How to avoid some of the most common pitfalls leading to ugly LaTeX.

## Text Inside of Equations

Use $\textrm{my_variable} = 2$ $\textrm{my_variable} = 2.$

Not $my_variable = 2$ $my_variable = 2.$

## Parentheses Around Fractions

Use $\left( \dfrac{1}{2} \right)$ $\left( \dfrac{1}{2} \right).$

Not $( \dfrac{1}{2} )$ $( \dfrac{1}{2} ).$

This applies to brackets, braces, absolute value signs, etc as well.

## Exponents and Subscripts

Use $2^{10}$ $2^{10}.$

Not $2^10$ $2^10.$

## Vertical Bars

Whenever you want to use a vertical bar $|$ $\vert,$ it has to be in a math environment (e.g. between dollar signs, or in an align environment).

Otherwise, if you try to use it as plain text, it will turn into a horizontal bar: | ———.

## Integral Formatting

Use $\displaystyle \int x \, \mathrm{d}x$ $\displaystyle \int_0^1 x \, \mathrm{d}x.$

Not $\int x dx$$\int_0^1 x dx.$

## Evaluating at Bounds

Use $\left. \dfrac{x^2}{2} \right|{12}^{15}$ $\left. \dfrac{x^2}{2} \right\vert_{12}^{15}.$

Not $\frac{x^2}{2} \right|_12^15$ $\frac{x^2}{2} \vert_12^15.$

## Arrows Under Limits

Use $\lim\limits_{x \to 0} f(x)$ $\lim\limits_{x \to 0} f(x).$

Not $\lim_{x \to 0} f(x)$ $\lim_{x \to 0} f(x).$

## Expectation and Variance

Use $\mathrm{E}[X] \mathrm{Var}[X]$ $\mathrm{E}[X] \, \mathrm{Var}[X].$

Not $E[X] Var[X]$ $E[X] \, Var[X].$

## Conditional Probability

Use $P(\textrm{new event} \, | \, \textrm{old event})$ $P(\textrm{new event} \, \vert \, \textrm{old event}).$

Not $P(new event | old event)$ $P(new event \vert old event).$

## Multiplication

Use $P(A)P(B), \, P(A) \cdot P(B), \, P(A) \times P(B)$ $P(A)P(B), \, P(A) \cdot P(B), \, P(A) \times P(B).$

Not $P(A) * P(B)$ $P(A) * P(B).$

## Align Environments

Whenever you are manipulating an equation, use an align environment:


\begin{align*}
3x+2 &= 5 \\
3x &= 3 \\
x &= 1
\end{align*}

\begin{align*} 3x+2 &= 5 \\ 3x &= 3 \\ x &= 1 \end{align*}

Likewise, whenever you are simplifying an expression through multiple steps, use an align environment:


\begin{align*}
\int_1^2 x \, \textrm{d}x &= \left. \dfrac{x^2}{2} \right|_1^2 \\
&= \dfrac{2^2}{2} - \dfrac{1^2}{2} \\
&= \dfrac{4}{2} - \dfrac{1}{2} \\
&= \dfrac{3}{2}
\end{align*}

\begin{align*} \int_1^2 x \, \textrm{d}x &= \left. \dfrac{x^2}{2} \right|_1^2 \\ &= \dfrac{2^2}{2} - \dfrac{1^2}{2} \\ &= \dfrac{4}{2} - \dfrac{1}{2} \\ &= \dfrac{3}{2} \end{align*}

## Running Off the Page

If a line of math is running off the page, break it after a term midway and put the rest on the next line like this:


\begin{align*}
\int (x + x^2 + x^3 + x^4 + x^5 + x^6) \, \textrm{d}x
&= \int x \, \textrm{d}x + \int x^2 \, \textrm{d}x + \int x^3 \, \textrm{d}x \\
&\quad + \int x^4 \, \textrm{d}x + \int x^5 \, \textrm{d}x + \int x^6 \, \textrm{d}x \\
&\quad + \int x^7 \, \textrm{d}x + \int x^8 \, \textrm{d}x + \int x^9 \, \textrm{d}x
\end{align*}

\begin{align*} \int (x + x^2 + x^3 + x^4 + x^5 + x^6) \, \textrm{d}x &= \int x \, \textrm{d}x + \int x^2 \, \textrm{d}x + \int x^3 \, \textrm{d}x \\ &\quad + \int x^4 \, \textrm{d}x + \int x^5 \, \textrm{d}x + \int x^6 \, \textrm{d}x \\ &\quad + \int x^7 \, \textrm{d}x + \int x^8 \, \textrm{d}x + \int x^9 \, \textrm{d}x \end{align*}

Don’t keep it all on one line. So, don’t do this:

</code>
\begin{align*}
\int (x + x^2 + x^3 + x^4 + x^5 + x^6) \, \textrm{d}x = \int x \, \textrm{d}x + \int x^2 \, \textrm{d}x + \int x^3 \, \textrm{d}x + \int x^4 \, \textrm{d}x + \int x^5 \, \textrm{d}x + \int x^6 \, \textrm{d}x + \int x^7 \, \textrm{d}x + \int x^8 \, \textrm{d}x + \int x^9 \, \textrm{d}x
\end{align*}
</code>
\begin{align*} \int (x + x^2 + x^3 + x^4 + x^5 + x^6) \, \textrm{d}x = \int x \, \textrm{d}x + \int x^2 \, \textrm{d}x + \int x^3 \, \textrm{d}x + \int x^4 \, \textrm{d}x + \int x^5 \, \textrm{d}x + \int x^6 \, \textrm{d}x + \int x^7 \, \textrm{d}x + \int x^8 \, \textrm{d}x + \int x^9 \, \textrm{d}x \end{align*}

Tags: