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 \)Inline math, the LaTeX form.
$ a^2 + b^2 $Inline math, the TeX form; same result.
\[ a^2 + b^2 = c^2 \]Displayed on its own line, not numbered.
\begin{equation} E = mc^2 \end{equation}Displayed and numbered.
- Text mode, no math preview
\begin{equation*} … \end{equation*}Displayed, not numbered (amsmath).
\text{for all } xUpright words with normal spacing inside a formula (amsmath).
- Text mode, no math preview
\label{eq:energy} … \eqref{eq:energy}Name an equation, then refer to it as (1).
x = 1 \tag{A}Custom equation tag instead of a number.
\displaystyle \sum_{i=1}^{n} iForce 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.
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
LaTeXf(x) = \begin{cases} 1 & \text{if } x > 0 \\ 0 & \text{otherwise} \end{cases}Spacing commands
LaTeXa\,b \quad a\;b \quad a\quad b \quad a\qquad b\,thin,\;thick,\quadand\qquadwide. Plain spaces do nothing in math.Inline vs display size
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
LaTeX\nabla \cdot \mathbf{E} = \frac{\rho}{\varepsilon_0} \tag{Gauss}Units in text
LaTeXv = 12\ \text{m/s}Upright function names
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
LaTeXThe 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
\eqrefprints "(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 ofThe area is x^2.WriteThe 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
equationholds one line. Usealignfor 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*,alignand\textneed\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 itstagsoption. 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 asequationandalignare 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
\[ … \]orequation*. Inalign, add\notagto a line to drop its number.
Related LaTeX reference
- How to Write a Multiline Equation in LaTeXalign and align* for aligned lines, aligned inside $$ … $$, split, gather and multline for long formulas and systems of equations.
- How to Write a Fraction in LaTeX\frac{numerator}{denominator}, \dfrac and \tfrac for size, continued fractions with \cfrac, slanted fractions and the a/b form.
- How to Write a Subscript in LaTeX_ for subscripts and ^ for superscripts, braces for more than one character, text indices, both at once and scripts in text mode.
- LaTeX equation editorSymbol panel, live preview, and PNG, SVG, MathML or Word export.
- All LaTeX reference pagesSymbols, math structures and text formatting in one list.
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