Document Classes, the Preamble, and Packages
The document class determines the basic structure of the document; the preamble contains global settings; packages add capabilities to the document. The example below demonstrates how the three work together.
EasyLaTeX compiler setting: pdfLaTeX with the standard article class. The examples also use graphicx, xcolor, and hyperref.
Minimal Preamble
The preamble is located after \documentclass and before \begin{document}:
\documentclass[a4paper,12pt]{article}
% Preamble: load packages and set global options
\begin{document}
This is the body.
\end{document}
The body contains the content to be displayed; the preamble specifies how the entire document works. Do not put \usepackage in the body.
Choosing a Document Class
\documentclass The curly braces specify the document class, and the square brackets provide options:
\documentclass[a4paper,12pt]{article}
| Document class | Applicable scenarios |
|---|---|
article | English articles, course assignments, short reports |
report | English reports, supports \chapter |
book | English books or long documents, supports prefaces and chapters |
beamer | Slides; configure fonts and themes according to the Beamer template |
Do not switch to book in short articles just to get \chapter. A document class primarily expresses the type of document, and only secondarily the number of available commands.
Loading Packages
Use \usepackage to load the functionality. The following three packages handle images, colors, and links, respectively:
\usepackage{graphicx} % \includegraphics
\usepackage{xcolor} % \textcolor, \colorbox and other color commands
\usepackage{hyperref} % Clickable contents, references, and URL
Package names do not include the .sty suffix. Loading a package does not mean that you must use it immediately; the corresponding effects are produced only when its commands are invoked in the body.
Setting Document Information
Title information is usually placed in the preamble, and then output in the body using \maketitle:
\title{Experiment Report}
\author{Alex Taylor}
\date{July 2026}
\begin{document}
\maketitle
\end{document}
Commands such as \title only store information and do not automatically typeset the title; only \maketitle generates the title area at the current location.
Define Your Own Commands
Repeated phrases or formatting can be encapsulated as commands in the preamble:
\newcommand{\LaTeXdoc}{\LaTeX{} documents}
Use in the body:
This document introduces \LaTeXdoc and its basic writing workflow.
This is suitable for stable, repetitive content. Do not encapsulate all formatting in custom commands from the outset; extract it when repetition actually occurs, making the document easier to read and debug.
Loading Order and Boundaries
-
The document class must be the first LaTeX command in the file;
-
Packages and global settings belong in the preamble;
-
hyperrefis usually placed after most packages to avoid overriding link settings; -
For the same functionality, prefer one default package setup and avoid loading overlapping alternatives simultaneously;
-
Before modifying a template provided by a school, journal, or publisher, read the template comments first; do not arbitrarily delete its document class or existing packages.
Complete example
\documentclass[a4paper,12pt]{article}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage[colorlinks=true,linkcolor=blue,urlcolor=blue]{hyperref}
\title{Preamble Example}
\author{Alex Taylor}
\date{\today}
\newcommand{\LaTeXdoc}{\LaTeX{} documents}
\begin{document}
\maketitle
This is an example of \textcolor{blue}{\LaTeXdoc}.
\href{https://www.latex-project.org/}{LaTeX Project} is the official LaTeX project website.
\end{document}