Typevia
Document Structure

Understanding Document Classes

In LaTeX, the very first line of every file tells the program what kind of document you are creating. This is done through the Document Class. Think of a document class as a professional blueprint or template. It defines the overall layout, the default font sizes, how sections are numbered, and whether the document should have chapters or just sections.

By choosing a specific class, you are telling LaTeX: "I am writing a scientific paper," or "I am writing a full-length book." LaTeX then handles the heavy lifting of formatting according to the standards for that document type.

The Syntax of \documentclass

The command always appears at the very top of your .tex file:

\documentclass[options]{class}
  • class: The name of the document type (e.g., article, report).
  • options: (Optional) Keywords separated by commas that modify the behavior of the class (e.g., 12pt, a4paper).

Common Document Classes

Choosing the right class is the first step in any LaTeX project. Here are the most frequently used classes:

  1. article: The most common class. Used for short reports, scientific papers, essays, and documentation. It does not support chapters.
  2. report: Used for longer works that need chapters, such as a thesis or a multi-section technical report.
  3. book: Designed for real books. It includes features like front matter (preface), back matter (index/bibliography), and handles double-sided printing automatically (alternating margins).
  4. letter: Specifically designed for writing formal letters.
  5. beamer: Used for creating presentation slides (the LaTeX equivalent of PowerPoint).

Code Example: A Minimal Article

\documentclass{article}

\begin{document}
Hello, world! This is a simple LaTeX article.
\end{document}

Customizing with Document Options

You can modify how a class looks by adding options inside the square brackets []. Multiple options are separated by commas.

  • Font Size: 10pt (default), 11pt, or 12pt.
  • Paper Size: a4paper, letterpaper (standard US size), legalpaper.
  • Columns: onecolumn (default) or twocolumn.
  • Layout: oneside or twoside (adjusts margins for binding).

Code Example: A Customized Report

\documentclass[12pt, a4paper, twoside]{report}

\begin{document}
This report uses 12-point font on A4 paper and is formatted for double-sided printing.
\end{document}

Note: If you don't specify options, LaTeX defaults to 10pt font and letterpaper (usually), depending on your local TeX distribution settings.


The Anatomy of a LaTeX Document

A LaTeX file is strictly divided into two main parts: the Preamble and the Body.

1. The Preamble

The Preamble is everything between the \documentclass command and the \begin{document} command. This is where you:

  • Load packages (add-ons that give LaTeX more features).
  • Set the document metadata (Title, Author, Date).
  • Define global settings or custom commands.

2. The Body

The Body is everything contained between \begin{document} and \end{document}. This is where your actual text, images, and tables live.

Code Example: Visualizing the Structure

% --- PREAMBLE START ---
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc} % Set character encoding
\usepackage{graphicx}       % Allow images

\title{My First LaTeX Document}
\author{Jane Doe}
\date{\today}
% --- PREAMBLE END ---

\begin{document}
\maketitle % This command generates the title info from the preamble

Welcome to the document body! This is where the content appears.
\end{document}

Logical Document Structure

One of LaTeX's greatest strengths is its "Logical Design." Instead of manually highlighting text and making it big and bold to create a header, you tell LaTeX the meaning of the text (e.g., "this is a section header").

Sectioning Commands

LaTeX provides a hierarchy of commands to organize your content. Each command automatically handles the numbering and font styling.

  • \part{...} (Used in very large books)
  • \chapter{...} (Only available in report and book)
  • \section{...}
  • \subsection{...}
  • \subsubsection{...}
  • \paragraph{...}
  • \subparagraph{...}

Code Example: Organizing Content

\documentclass{report}

\begin{document}

\chapter{Introduction}
This is the first chapter of my report.

\section{Background}
Here is some background information.

\subsection{Historical Context}
Let's look even deeper into the history.

\section{Current Objectives}
In this section, we discuss what we are doing now.

\chapter{Methodology}
This starts a new page and a new chapter.

\end{document}

Tip: If you want a section that is not numbered and does not appear in the Table of Contents, add an asterisk * to the command: \section*{Introduction}.


Best Practices and Common Pitfalls

Best Practices

  • Keep the Preamble Clean: Group your packages together and use comments (%) to explain why you added a specific package.
  • Use Logical Sectioning: Never try to "fake" a section header by using \textbf{...} and \large. This breaks the Table of Contents and document indexing.
  • Choose the Right Class Early: Switching from article to book late in the writing process can lead to formatting headaches (like missing \chapter errors).

Common Pitfalls to Avoid

  1. Forgetting \end{document}: Every document must end with this command, or LaTeX will throw an error saying the document "ended prematurely."
  2. Using \chapter in an article: If you try to use \chapter while using \documentclass{article}, LaTeX will return an "Undefined control sequence" error.
  3. Too many sub-levels: Avoid going deeper than \subsubsection. If your document requires \subsubsubsubsection, you likely need to rethink your organization or move up to a report class with chapters.

Creating a Table of Contents

Because you used logical sectioning commands (\section, \subsection), LaTeX can generate a Table of Contents automatically. Simply place the following command where you want the table to appear (usually right after the title):

\tableofcontents

Warning: You usually need to compile your LaTeX document twice for the Table of Contents to appear correctly. The first pass records the section names/numbers, and the second pass places them in the table.