Math mode basics

LaTeX has a special state, separate from ordinary text, called math mode. Mathematics is set with its own fonts, spacing, and typesetting rules, so even a lone x is treated as something different from the x in a sentence. This page sorts out the two doorways into math mode — inline math, set within the run of text, and display math, set on its own line — and the rules that hold inside it: spaces are ignored, letters become variables, and function names need dedicated commands.

What math mode is

When LaTeX sets a document it switches between several modes (internal states). The state you read and write ordinary prose in is text mode; the state for setting mathematics is math mode. The two differ not just in appearance but in the typesetting rules themselves. In math mode letters are set in a mathematical italic (math italic), spacing is computed from the structure of the formula rather than from a run of letters, and symbols such as + and = automatically get the correct space around them as operators and relations.

Why a separate mode at all? Because mathematical typesetting carries a host of conventions that ordinary prose does not. Variables are slanted, constants and function names are upright, subscripts are small, fractions and roots reach beyond the height of the letters, and even the gaps between symbols follow rules. Specifying all of this by hand would be hopeless. So you simply declare “math starts here,” and from then on LaTeX takes care of everything in the mathematical idiom — that is the idea of math mode. Write $x$ in the body and that single x is set as a mathematical variable, even if everything around it is Japanese.

Math mode comes in two broad forms: inline math, embedded in the flow of a sentence, and display math, set apart from the paragraph on its own line and given more room. The very same formula is sized and arranged differently depending on which you choose. We will take them in turn.

Inline math

Inline math drops a formula into the running text. The form LaTeX recommends is to wrap it in \( and \); the short TeX shorthand $$ is also widely used. The two are equivalent — a matter of taste. (There is also the environment form \begin{math}\end{math}, which does the same thing but is too verbose for inline use.)

latex
アインシュタインの関係式は \( E = mc^2 \) である。

The mass--energy relation is $E = mc^2$.

In each case the formula sits within the line at a restrained size so as not to disturb the line height much. E, m, and c are slanted as variables, and the 2 is set small as a superscript exponent. Note that $ uses the same character to open and close, so a forgotten closing $ swallows the rest of your text into math mode and produces a baffling error. The \(\) pair distinguishes open from close, making mismatches far easier to catch.

Because inline math is constrained by the line height, fractions and sums are set compactly. The details are covered below under “Inline vs display style.”

Display math

Important or large formulas are lifted out of the paragraph onto a line of their own — this is display math. The LaTeX-recommended form is \[\], which is an abbreviation that behaves exactly like the **displaymath environment** (\begin{displaymath}\end{displaymath}). Neither one adds an equation number. By default the formula is centered; the document-wide fleqn option makes it flush left instead.

latex
次の等式が成り立つ。
\[
  \int_0^1 x^2 \, dx = \frac{1}{3}
\]
これは基本的な積分である。

This outputs a formula centered on its own line, with the integral sign set large beyond the letter height, the range from 0 to 1 placed below and above the integral sign, and the fraction 1/3 set tall across a horizontal bar. A little vertical space separates it from the surrounding text.

When you want a number on the formula so you can refer back to it, use the **equation environment** instead of \[\]. It sets the formula on its own line and automatically adds a running number at the right margin (the default for horizontal writing). Combined with \label{…} and \ref{…}, you can cite it from the text as “by equation (3)” (numbering and alignment are treated in detail on a separate page).

latex
\begin{equation}
  e^{i\pi} + 1 = 0
\end{equation}

For an unnumbered display you can use \[\], or the equation* environment from the amsmath package (equivalent to displaymath, but able to draw on amsmath’s facilities). The table below sorts out the main ways to set display math.

FormNumbered?Notes
\[ … \]NoLaTeX-recommended; short form of displaymath
displaymathNoEnvironment form, identical to \[ \]
equationYesAuto-numbered; referenceable via \label
equation*NoNeeds amsmath; equivalent to displaymath
$$ … $$NoDiscouraged (see next section)

Why not `$$ … $$`

You will sometimes see display math wrapped in $$$$, but that is a plain-TeX habit and is discouraged in LaTeX. Although it looks similar to \[\], it is not officially supported by LaTeX, and it causes real problems.

  • You lose the proper space above and below that \[\] inserts, so the vertical spacing around the display changes.
  • The document-wide **fleqn option has no effect**.
  • LaTeX’s consistency checks (matching open/close, and the like) do not run.
  • \[\] are macros that can be redefined to change their behavior when needed; $$ offers no such flexibility.

The AMS “Short Math Guide for LaTeX” likewise warns strongly against $$$$. For display math, reach for \[\] (unnumbered) or the equation environment (numbered).

Inline vs display style

The same formula is set in a different style depending on whether it is inline or display. Inline math is set compactly in text style so as not to disturb the line height; display math is set with more room in display style. The difference shows up most clearly in the placement of limits on sums and integrals, and in the size of fractions and scripts.

  • Sum/integral limits: in display style the range on \sum sits above and below the symbol, whereas in text style (inline) it sits to its right side. The range on \int shifts position the same way.
  • Fractions: \frac is large in display style and small and compact in text style.
  • Superscripts/subscripts: scripts also change size and position with the style, shrinking by steps as they nest deeper.

You can override the style with commands. \displaystyle applies “the style used for math set on its own line,” and \textstyle applies “the style used for math set within a paragraph,” each from that point on. There are also \scriptstyle for scripts and \scriptscriptstyle for second-order scripts; type shrinks in that order. For example, to push a sum’s limits above and below within running text, use \displaystyle inside the inline formula.

latex
インライン: $\sum_{i=1}^{n} i$ では範囲が右脇に付く。

強制ディスプレイ: $\displaystyle\sum_{i=1}^{n} i$ では範囲が上下に付く。

In the example above, the first sets i=1 and n small at the right of the summation sign, while the second places the same range directly below and above it. Be aware, though, that heavy use of \displaystyle in running text tends to widen the line spacing, so keep it to where it is needed.

The rules inside math mode

Inside math mode three rules take over that differ from ordinary text. Without knowing them you will be puzzled when “the space I typed disappears” or “my English word comes out slanted and spread apart.”

First, spaces you type in the source are ignored. All spacing within a formula is decided by LaTeX from its structure, so a+b and a + b produce identical output. When you do want a gap, use dedicated commands such as \, (a thin space) or \quad and \qquad (wider spaces). Line breaks are ignored too, so you may wrap the source for readability.

Second, every letter is taken to be the name of a variable and set in math italic. So typing area in a formula gives not the word “area” but the product of four variables a, r, e, a, spaced as a run of variables.

Third, to put ordinary words (upright, correctly spaced) inside a formula you use a dedicated command. The standard is \text{…} from the amsmath package, which sets the enclosed string in the same font and spacing as body text. Without amsmath, \mbox{…} does much the same, but it has problems — its size does not follow inside subscripts, and its name does not describe the purpose.

latex
\[
  V = \frac{4}{3}\pi r^3 \quad \text{(半径 $r$ の球)}
\]

Here a \quad gap follows the formula for the volume of a sphere, and the phrase inside \text{…} is set upright in the body font. Note too that writing $r$ inside \text{…} switches that part back into math.

Operators and function names

It follows from the previous section that typing sin x literally looks like the product of three variables s, i, n. The right way is a dedicated command such as \sin. LaTeX predefines the names of common functions and operators; each is set upright (roman) with the correct spacing around it.

  • Trig & hyperbolic: \sin \cos \tan \cot \sec \csc; \sinh \cosh \tanh \coth; inverses \arcsin \arccos \arctan.
  • Logs & exponential: \log \ln \lg \exp.
  • Limits & bounds: \lim \limsup \liminf \sup \inf \max \min.
  • Algebra & others: \arg \det \dim \gcd \ker \hom \deg \Pr; modulo \bmod \pmod.

Some of these (\lim, \max, \sup, …) place a subscript directly below in display style. For instance \lim_{n\to\infty} sets n→∞ right under lim in a display (and at the lower right when inline).

latex
\[
  \lim_{n \to \infty} \left(1 + \frac{1}{n}\right)^{n} = e,
  \qquad \sin^2\theta + \cos^2\theta = 1.
\]

To set a function name that is not on the list in upright type, use \operatorname{…} from amsmath. Writing \operatorname{rank} A, for example, sets rank upright with proper spacing. If you use the same operator repeatedly, define it once in the preamble with \DeclareMathOperator{\rank}{rank} and then write \rank in the body. For an operator whose subscript should sit directly below (like \lim), use the starred form \DeclareMathOperator*{\argmax}{arg\,max}.