BudiBadu Logo
Samplebadu

LaTeX by Example: Equations

LaTeX2e

The `equation` environment creates numbered equations that can be referenced. This sample shows how to label and align multi-line equations.

Code

\documentclass{article}
\usepackage{amsmath}

\begin{document}
    % Numbered equation
    Newton's second law is:
    \begin{equation}
        F = ma
        \label{eq:newton}
    end{equation}
    
    As seen in Equation \ref{eq:newton}, force is proportional to acceleration.
    
    % Multi-line aligned equations
    \begin{align}
        (a + b)^2 &= (a + b)(a + b) \\
                  &= a^2 + ab + ba + b^2 \\
                  &= a^2 + 2ab + b^2
    \end{align}
    
    % Unnumbered multi-line equations
    \begin{align*}
        f(x) &= x^2 \\
        g(x) &= sin(x)
    \end{align*}
\end{document}

Explanation

For scientific writing, numbering equations is crucial for reference. The equation environment automatically assigns a number to the equation (e.g., (1)) on the right margin. You can use the \label{key} command to assign an internal name to the equation, and \ref{key} (or \eqref{key} from amsmath) to refer to it later in the text. If the equation number changes, the reference updates automatically.

For derivations or systems of equations, the align environment from the amsmath package is indispensable. It allows you to align multiple lines of math, typically at the equals sign. You mark the alignment point with an ampersand & and the end of a line with double backslashes \\.

If you don't want an equation to be numbered, you can use the starred version of the environment, such as equation* or align*. This is common for intermediate steps in a proof or for simple definitions that don't need to be referenced elsewhere.

Code Breakdown

8
\label{eq:newton} assigns the key "eq:newton" to this equation number.
15
&= aligns the lines at the equals sign. The & is the alignment tab character.