¶ Writing & documents

LaTeX for beginners: the structure of your first document

Preamble, classes, packages, sections and compiling: how to write your first LaTeX document, with a minimal annotated template and the typical mistakes.

LaTeX is a typesetting system: instead of formatting text through menus, you write a text file with commands, and a program turns it into a laid-out PDF. It is the de facto standard for scientific papers and theses with many formulas, but it is also useful for manuals, technical reports and any long document where consistency and output quality matter.

There is a learning curve, but it is gentler than people say. A first document needs about ten commands.

What you need to start

There are two routes:

  • Local installation. A distribution such as TeX Live (Windows, Linux, macOS) or MiKTeX (Windows), plus an editor. TeXstudio is a convenient choice because it has buttons to compile and preview; VS Code with the LaTeX Workshop extension also works well.
  • Online editor. Services such as Overleaf let you write and compile in the browser without installing anything, and make collaborative work easier.

For learning, an online editor removes installation problems. For confidential documents or offline work, a local installation is the better choice.

The minimal document

\documentclass[a4paper,11pt]{article}

\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{geometry}
\geometry{margin=2.5cm}

\title{Technical report}
\author{Systems office}
\date{\today}

\begin{document}
\maketitle

\section{Introduction}
This is the first paragraph.

A blank line starts a new paragraph.

\section{Analysis}
\subsection{Collected data}
Text of the subsection.

\end{document}

Everything before \begin{document} is the preamble: that is where you declare the document type, packages and settings. Everything between \begin{document} and \end{document} is the content.

The parts of the preamble

  • \documentclass{article} chooses the class: article for short texts, report and book for documents with chapters. Options such as paper size and font size go in square brackets.
  • babel with a language option sets the hyphenation rules and translates automatic words: «Contents», «Chapter», «Figure»; with italian they become «Indice», «Capitolo», «Figura».
  • fontenc with T1 improves the handling of accented letters in the PDF; lmodern loads a modern version of the default font.
  • geometry lets you set the margins in a readable way.

Recent versions of LaTeX read files in UTF-8 with nothing else needed, so you can type accented characters directly.

Commands and environments

Commands start with a backslash: \section{...}, \emph{...} for emphasis, \textbf{...} for bold. Environments enclose a block between \begin{...} and \end{...}:

\begin{itemize}
  \item first point
  \item second point
\end{itemize}

\begin{enumerate}
  \item first step
  \item second step
\end{enumerate}

An important principle: in LaTeX you state what an element is, not how it should look. You write \section, not «bold 14 point». The appearance is decided by the class, which is why documents stay consistent from the first page to the last.

Compiling

Compiling means having the program that produces the PDF read the .tex file. In the editor it takes a button; from the command line:

pdflatex report.tex

For the table of contents, cross-references and bibliography you need several passes, because LaTeX writes page numbers to an auxiliary file and reads them on the next pass. Tools such as latexmk repeat the compilation on their own as many times as needed.

Special characters

Some characters have a reserved meaning and must be preceded by a backslash to be printed:

Character Written as Why
% \% Starts a comment
& \& Separates columns in tables
$ \$ Opens math mode
# \# Marks macro parameters
_ \_ Subscript in math

The % sign is the trickiest: «20% discount» without the backslash makes the rest of the line disappear, treated as a comment.

Reading errors

When compilation stops, the message gives the line number. The most common beginner errors are an unclosed curly brace, an environment opened and never closed, a package with the wrong name or an unescaped special character. It pays to compile often: if the error appears after you have written three lines, you find it in seconds.

In short

  • LaTeX turns a text file with commands into a PDF laid out with great consistency.
  • The preamble declares class, packages and settings; the body holds the text.
  • For language support you need babel, fontenc and a suitable font; UTF-8 is supported.
  • You describe structure, not appearance: sections, lists, emphasis.
  • Compiling often makes errors easy to find.
MM
Marco Marzaduri

Marco Marzaduri works in cybersecurity, digital forensics and web development. A computer science graduate, he combines rigorous technical analysis with attention to the people who use technology.

Read also