Math Mode in LaTeX

LaTeX typesets formulas only in math mode. Put a formula inside a sentence between \( … \) or $ … $ (inline), on its own centered line between \[ … \] (display), or in \begin{equation} … \end{equation} to give it a number. Inside math mode spaces are ignored and letters are italic; use \text{…} for words.

Ways into math mode

  • \( a^2 + b^2 \)
    a 2 + b 2

    Inline math, the LaTeX form.

  • $ a^2 + b^2 $
    a 2 + b 2

    Inline math, the TeX form; same result.

  • \[ a^2 + b^2 = c^2 \]
    a 2 + b 2 = c 2

    Displayed on its own line, not numbered.

  • \begin{equation} E = mc^2 \end{equation}
    (1) E = m c 2

    Displayed and numbered.

  • \begin{equation*} … \end{equation*}
    Text mode, no math preview

    Displayed, not numbered (amsmath).

  • \text{for all } x
    for all  x

    Upright words with normal spacing inside a formula (amsmath).

  • \label{eq:energy} … \eqref{eq:energy}
    Text mode, no math preview

    Name an equation, then refer to it as (1).

  • x = 1 \tag{A}
    (A) x = 1

    Custom equation tag instead of a number.

  • \displaystyle \sum_{i=1}^{n} i
    ∑ i = 1 n i

    Force display size inside inline math.

Try it live

Change the formula and the preview redraws as you type. Copy the LaTeX, download a PNG or SVG, copy it for Word, or use Edit here on any example below to load it.

Templates

Math mode and equations examples

Each result below is drawn from the LaTeX under it. Copy the code, edit it on this page, or open it in the full editor.

  • Words inside a formula

    f ( x ) = { 1 if  x > 0 0 otherwise
    LaTeX
    f(x) = \begin{cases} 1 & \text{if } x > 0 \\ 0 & \text{otherwise} \end{cases}
  • Spacing commands

    a b a b a b a b
    LaTeX
    a\,b \quad a\;b \quad a\quad b \quad a\qquad b

    \, thin, \; thick, \quad and \qquad wide. Plain spaces do nothing in math.

  • Inline vs display size

    ∑ k = 1 n 1 k ∑ k = 1 n 1 k
    LaTeX
    \textstyle \sum_{k=1}^{n} \frac{1}{k} \quad \displaystyle \sum_{k=1}^{n} \frac{1}{k}

    Inline math shrinks fractions and puts limits beside the sum.

  • Numbered equation with a custom tag

    (Gauss) ∇ ⋅ E = ρ ε 0
    LaTeX
    \nabla \cdot \mathbf{E} = \frac{\rho}{\varepsilon_0} \tag{Gauss}
  • Units in text

    v = 12   m/s
    LaTeX
    v = 12\ \text{m/s}
  • Upright function names

    sin 2 ⁡ θ + cos 2 ⁡ θ = 1
    LaTeX
    \sin^2 \theta + \cos^2 \theta = 1

In a LaTeX document

These commands work in the text of a .tex document (Overleaf, TeX Live, MiKTeX), not inside math. The box on the right shows what the compiled PDF looks like.

  • Inline and displayed math in a paragraph

    LaTeX
    The roots of \(ax^2 + bx + c = 0\) are
    \[
      x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}.
    \]
  • Numbered equation and a reference

    LaTeX
    \usepackage{amsmath}  % preamble
    
    \begin{equation}
      E = mc^2 \label{eq:energy}
    \end{equation}
    Equation \eqref{eq:energy} relates mass and energy.

    The equation gets the number (1) at the right margin, and \eqref prints "(1)" in the sentence.

Inline or display

Inline math (\( … \) or $ … $) sits in the line of text and is set compactly: fractions shrink and sum limits move to the side. Displayed math (\[ … \]) gets its own centered line at full size. Use display for anything taller than a line or worth referring to.

Avoid $$ … $$ in LaTeX documents: it is plain-TeX syntax that gives inconsistent spacing and ignores the fleqn option. \[ … \] does the same job correctly. (In Markdown editors $$ is the normal display syntax.)

Numbering and referring to equations

equation numbers one displayed formula; equation* (amsmath) is the same without the number. For several aligned lines use align or align* — see the multiline equations page.

Put \label{eq:name} inside the environment and refer to it with \eqref{eq:name}, which prints the number in parentheses, or \ref{eq:name} for the bare number. Run LaTeX twice (Overleaf does this for you) so the numbers resolve instead of showing ??.

Text inside math

Math mode ignores spaces and sets letters as italic variables, so words typed directly look wrong. \text{…} (amsmath) switches to normal text that follows the surrounding size, including in subscripts. Add the spaces you need inside the braces: \text{ if }.

Common mistakes

  • ^ and _ only exist in math mode. In text LaTeX stops with "Missing $ inserted".

    Instead of
    The area is x^2.
    Write
    The area is $x^2$.
  • Letters in math mode are variables: "if" is set as italic i times f and the space disappears.

    Instead of
    $if x > 0$
    Write
    $\text{if } x > 0$
  • A blank line inside display math is an error; one equation holds one line. Use align for several.

    Instead of
    \begin{equation}
      a = b
    
      c = d
    \end{equation}
    Write
    \begin{align}
      a &= b \\
      c &= d
    \end{align}
  • $$ is plain TeX; \[ … \] is the LaTeX form with correct spacing.

    Instead of
    $$ E = mc^2 $$
    Write
    \[ E = mc^2 \]

In Overleaf, MathJax and Word

  • Overleaf and pdfLaTeX

    All forms work; equation*, align and \text need \usepackage{amsmath}, which Overleaf templates normally include. Unresolved references show as ?? until the next compile.

  • MathJax and KaTeX

    MathJax recognizes \( … \), \[ … \] and $$ … $$ by default; single $ for inline math has to be switched on in its configuration. Equation numbers need its tags option. Markdown apps such as Obsidian, Jupyter and GitHub use $ … $ and $$ … $$.

  • Microsoft Word

    In Word, Alt+= (Windows) inserts an equation box — that is Word's math mode. To reuse LaTeX, build the formula in the editor on this page and use Copy for Word to paste an editable equation.

Math mode and equations in LaTeX: questions

How do I enter math mode in LaTeX?
Use \( … \) or $ … $ for a formula inside a sentence and \[ … \] for one on its own line. Environments such as equation and align are math mode too.
How do I write normal text inside an equation?
Wrap it in \text{…} (amsmath): x > 0 \text{ for all } t. Include the spaces inside the braces.
What is the difference between $ and \[ in LaTeX?
$ … $ is inline math within the line of text; \[ … \] is displayed math on its own centered line at full size.
How do I number an equation and refer to it?
Use \begin{equation} … \label{eq:x} \end{equation} and write \eqref{eq:x} in the text; it prints the number, e.g. (1).
How do I stop an equation from being numbered?
Use \[ … \] or equation*. In align, add \notag to a line to drop its number.

Have the formula as a picture?

Paste a screenshot or photo of the equation and get its LaTeX, ready to copy or open in the editor.

Convert an image to LaTeX