Typevia
Mathematical Typesetting

Getting Started with Math in LaTeX

One of the primary reasons researchers, mathematicians, and students choose LaTeX is its unparalleled ability to typeset complex mathematical expressions. While word processors often struggle with alignment and formatting of formulas, LaTeX treats math as a first-class citizen.

In LaTeX, mathematical expressions are written in a specific "Math Mode." When you enter this mode, LaTeX automatically changes the font to italics (for variables), adjusts spacing between operators, and enables a vast library of mathematical symbols.

To get the most out of math in LaTeX, it is highly recommended to include the amsmath package in your document preamble:

\usepackage{amsmath}

Inline vs. Display Math

There are two primary ways to display math in LaTeX: Inline and Display.

1. Inline Math

Inline math is used for expressions that appear within a sentence. You wrap the expression using dollar signs $ ... $.

2. Display Math

Display math is used for important equations that need to be centered on their own line. The modern standard is to use \[ ... \].

Example:

The Pythagorean theorem states that $a^2 + b^2 = c^2$. 
This can be written as:
\[ a^2 + b^2 = c^2 \]

Note: You might see older tutorials using double dollar signs $$ ... $$ for display math. While this works, it is considered outdated and can cause spacing issues. Always prefer \[ ... \] for modern LaTeX.


Basic Arithmetic and Fractions

LaTeX makes it easy to write fractions, roots, and standard operators.

Fractions

Fractions are created using the \frac{numerator}{denominator} command.

Roots

Square roots use the \sqrt{value} command. For nth-roots (like a cube root), use the optional argument in square brackets: \sqrt[n]{value}.

Example:

An example of a fraction and a root:
\[ \frac{1}{x+1} + \sqrt{x^2 + y^2} = \sqrt[3]{27} \]

Superscripts and Subscripts

Adding exponents (superscripts) and indices (subscripts) is a fundamental part of math typesetting.

  • Superscripts: Use the caret symbol ^.
  • Subscripts: Use the underscore symbol _.

Important Rule: If your superscript or subscript contains more than one character, you must wrap them in curly braces {}. If you don't, LaTeX will only apply the formatting to the very first character.

Example:

% Incorrect: only the '2' is subscripted
$x_21$ 

% Correct: the entire '21' is subscripted
$x_{21}$

% Complex example:
\[ a_{n} = x^{n+1} + y^{i-j} \]

Greek Letters and Common Symbols

LaTeX uses intuitive commands for Greek letters. Generally, the command is just the name of the letter preceded by a backslash.

  • Lowercase: \alpha, \beta, \gamma, \pi, \theta
  • Uppercase: \Gamma, \Delta, \Omega, \Phi

In addition to Greek letters, you will frequently use symbols for relations and operators:

  • Operators: \times ($\times$), \div ($\div$), \pm ($\pm$)
  • Relations: \leq ($\le$), \geq ($\ge$), \neq ($\neq$), \approx ($\approx$)
  • Calculus/Sets: \infty ($\infty$), \partial ($\partial$), \in ($\in$), \forall ($\forall$)

Example:

The area of a circle is $A = \pi r^2$.
We can also say that:
\[ \lim_{x \to \infty} \frac{1}{x} = 0 \]

Handling Delimiters (Parentheses and Brackets)

Standard parentheses () and brackets [] work fine for simple expressions. However, if you have a tall expression like a fraction, standard parentheses will look too small.

To fix this, use the \left and \right] commands before your delimiters. These tell LaTeX to automatically scale the delimiters to the height of the content inside them.

Example:

% This looks cramped and unprofessional:
( \frac{a}{b} )

% This looks perfect:
\left( \frac{a}{b} \right)

% You can also use different types:
\left[ \frac{1}{2} \right] \quad \left\{ x^2 \right\}

Note: Every \left must have a corresponding \right. If you only want a delimiter on one side, use a period for the other: \left( ... \right.


Best Practices and Common Mistakes

To keep your LaTeX code clean and your output professional, follow these tips:

1. Whitespace in Math Mode

LaTeX ignores literal spaces inside math mode. $a + b = c$ is the same as $a+b=c$. LaTeX handles the spacing automatically based on mathematical rules. If you need to add space manually, use commands like \, (thin space) or \quad (large space).

2. Using Text inside Math Mode

If you need to write a normal word inside an equation, don't just type it. LaTeX will treat it as a string of variables and italicize it poorly (e.g., $velocity$ looks like $v \cdot e \cdot l \dots$). Instead, use the \text{...} command from the amsmath package.

% Bad:
\[ velocity = \frac{d}{t} \]

% Good:
\[ \text{velocity} = \frac{d}{t} \]

3. Common Pitfalls for Beginners

  • Forgetting to close Math Mode: If you start with $ but forget to end with $, LaTeX will throw an error (usually "missing $ inserted") or format your entire document as math.
  • Reserved Characters: Characters like %, &, and _ have special meanings in LaTeX. If you need them as literal text in math, you often need to escape them (e.g., \%).
  • Not using Packages: Many beginners struggle to align equations or use bold math symbols because they haven't loaded amsmath or bm.

4. Practice Example

Try combining everything you've learned into a single block:

\section*{The Quadratic Formula}
For a quadratic equation of the form $ax^2 + bx + c = 0$, the values of $x$ are given by:
\[ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \]
where $a \neq 0$.

By mastering these basics, you are now equipped to handle roughly 80% of the mathematical typesetting tasks required for standard academic and technical documents.