# LaTeX from the beginning

LaTeX describes a document's meaning and structure in text. You write sections, equations, figures and citations; a compiler lays them out as a PDF according to the document class and packages.

## Anatomy of a document

```latex
\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{hyperref}

\title{A clear title}
\author{Author name}
\date{}

\begin{document}
\maketitle
\section{Introduction}
The first paragraph.
\end{document}
```

The **document class** supplies the overall structure and style. A venue may provide its own `.cls` or `.sty`. The **preamble** is everything before `\begin{document}`: packages, settings, macros and metadata. The body holds the content.

Place `\usepackage` in the preamble, not inside a paragraph. `\maketitle` prints the title metadata. An empty `\date{}` suppresses a date for classes that use that field.

## Commands, arguments and environments

Commands begin with a backslash. Required arguments usually use braces, and optional arguments often use square brackets:

```latex
\textbf{bold text}
\emph{emphasized text}
\documentclass[11pt]{article}
```

Braces also group content so local settings do not leak farther than intended. Match every opening brace with its closing brace.

Environments delimit a region with matching names:

```latex
\begin{itemize}
  \item First point.
  \item Second point.
\end{itemize}
```

A mismatched `\end{...}` often causes an error reported after the original mistake. Indentation and one environment per logical block make source easier to inspect.

## Paragraphs, spaces and comments

A blank line starts a new paragraph. A single newline in ordinary source is usually just whitespace, so you can wrap long source lines for readability. Repeated ordinary spaces do not create an arbitrarily large gap.

Use `~` for a nonbreaking space, as in `Figure~\ref{fig:overview}`. `\\` forces a line break in environments where it is appropriate; do not use it as the general way to separate prose paragraphs.

`%` begins a comment and suppresses the remainder of that source line. This can also suppress the newline, which matters when editing macros or removing comments. A literal percent sign is `\%`.

## Special characters

Some characters have structural meanings and need commands when used literally:

| Character | Source for literal prose |
| --- | --- |
| Percent | `\%` |
| Ampersand | `\&` |
| Dollar | `\$` |
| Hash | `\#` |
| Underscore | `\_` |
| Braces | `\{` and `\}` |
| Backslash | `\textbackslash{}` |
| Tilde | `\textasciitilde{}` |
| Caret | `\textasciicircum{}` |

Underscores in ordinary prose often trigger a “Missing $ inserted” error because `_` means subscript in math. For web addresses, use a URL command from an appropriate package rather than manually escaping every punctuation mark.

## Structure and emphasis

Use `\section`, `\subsection` and `\subsubsection` to express hierarchy. The class decides numbering and typography. A starred form such as `\section*{Acknowledgments}` is generally unnumbered.

```latex
\section{Method}
\label{sec:method}
We introduce \emph{adaptive weighting}.
\subsection{Objective}
The objective combines two terms.
```

Use semantic emphasis sparingly. Avoid manually making bold lines to imitate section headings; proper headings support navigation and cross-references.

`itemize` creates bullets, `enumerate` numbered items, and `description` labelled items. Each item starts with `\item`.

## Inline and displayed mathematics

Use inline math for a short expression within a sentence and display math for an equation that needs its own line:

```latex
The sample has size \(n\), with mean \(\bar{x}\).
\[
  \bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i.
\]
```

Subscripts use `_`, superscripts use `^`, and braces group multiple characters: `x_{ij}`, `e^{i\theta}`. `\frac{a}{b}` creates a fraction; `\sqrt{x}` a square root. Greek letters include `\alpha`, `\beta` and `\lambda`.

Use `\text{...}` from amsmath for words inside math. Use operator commands such as `\log` and `\sin` instead of italic letter sequences. Math spacing differs from prose spacing.

## Number and align equations

```latex
\begin{equation}
  \mathcal{L}(\theta) = \frac{1}{n}\sum_{i=1}^{n}\ell_i(\theta)
  \label{eq:loss}
\end{equation}

\begin{align}
  a &= b + c \\
    &= d.
\end{align}

Equation~\eqref{eq:loss} defines the objective.
```

The `equation` environment numbers an equation. `align` from amsmath aligns at `&`; `\\` separates its rows. Starred variants omit numbering. Do not put an `align` environment inside another display-math delimiter.

## Labels and references

A `\label{...}` records a reference target. `\ref{...}` prints its number; `\eqref{...}` adds equation-style parentheses. Choose memorable unique keys instead of embedding fixed numbers in prose.

For figures and tables, put the label after the caption so it captures the correct counter. Rebuild after introducing labels. `??` generally indicates an unresolved reference or an incomplete build cycle.

## Build a learning loop

Use Split with source and Visual/PDF. Make one small edit, compile and inspect. When an error appears, fix the first error and check nearby braces or environments. Keep a working version so experiments with unfamiliar commands are easy to undo.

Continue with [practical LaTeX techniques](latex-advanced.md), [references](references.md) and [troubleshooting](troubleshooting.md).
