Typevia
Document Structure

Understanding Document Structure in LaTeX

One of the most powerful features of LaTeX is its ability to handle document structure automatically. Unlike a standard word processor where you manually change font sizes and bold text to create headings, LaTeX uses semantic commands. This means you tell LaTeX what a piece of text is (e.g., a section or a chapter), and LaTeX takes care of the numbering, formatting, and inclusion in the Table of Contents.

This approach ensures perfect consistency throughout your document, regardless of whether it is five pages or five hundred pages long.

The Hierarchy of Sectioning Commands

LaTeX provides a set of commands that follow a strict hierarchy. Each command represents a level of importance. It is important to use these in order; for example, you shouldn't place a \subsection directly after a \chapter without a \section in between.

The standard levels, from highest to lowest, are:

  1. \part{...} (Used for very large volumes)
  2. \chapter{...} (Available only in report and book classes)
  3. \section{...}
  4. \subsection{...}
  5. \subsubsection{...}
  6. \paragraph{...}
  7. \subparagraph{...}

Note: The \paragraph and \subparagraph commands do not behave like standard paragraphs. In LaTeX, they are actually the lowest level of headings and are often formatted as inline bold text followed by the body of the paragraph.

Sectioning in the Article Class

The article class is the most common document class for shorter documents, such as essays or research papers. In this class, the highest level of organization is usually the \section.

Here is a practical example of how to structure an article:

\documentclass{article}

\begin{document}

\section{Introduction}
This is the first section of the document. LaTeX will automatically number this as "1".

\section{Methodology}
Here we explain how the research was conducted.

\subsection{Data Collection}
This is a subsection under Methodology. It will be numbered "2.1".

\subsection{Data Analysis}
This will be numbered "2.2".

\subsubsection{Statistical Software}
This is even deeper in the hierarchy, numbered "2.2.1".

\section{Results}
Back to the main level! This will be numbered "3".

\end{document}

When you compile this code, LaTeX calculates the numbers for you. If you later decide to move the "Results" section before the "Methodology," LaTeX will automatically update all the numbers in the next compilation.

Chapters in Reports and Books

If you are writing a thesis, a technical manual, or a book, you will likely use the report or book document class. These classes introduce the \chapter command.

A key difference is that \chapter usually starts on a new page and has a distinct, prominent style.

\documentclass{report}

\begin{document}

\chapter{The Beginning of the Journey}
Chapters are the primary units of a report or book.

\section{A Small Step}
Sections within a chapter are numbered relative to the chapter (e.g., 1.1).

\chapter{The Second Chapter}
This starts on a fresh page and is numbered "Chapter 2".

\end{document}

Warning: If you try to use the \chapter command inside a document using \documentclass{article}, LaTeX will throw an "Undefined control sequence" error. Always ensure your document class supports the commands you are using.

Unnumbered Sections and the Table of Contents

Sometimes you want a heading that looks like a section but doesn't have a number. This is common for "Introduction," "Preface," or "Bibliography" sections.

To create an unnumbered section, add an asterisk (*) to the command:

\section*{Preface}
This section will not have a number and, by default, will NOT appear in the Table of Contents.

\section{Introduction}
This is a standard numbered section.

Adding a Table of Contents

One of the greatest benefits of using sectioning commands is the automatic Table of Contents (ToC). You can generate one simply by placing the \tableofcontents command where you want it to appear (usually after the title or abstract).

\documentclass{article}

\begin{document}

\tableofcontents
\newpage

\section{Introduction}
Content goes here...

\section{Literature Review}
Content goes here...

\end{document}

Tip: You usually need to compile your LaTeX document twice to see the Table of Contents. The first pass records the section titles and page numbers into a side file (the .toc file), and the second pass reads that file to print the table.

Best Practices and Common Pitfalls

To create professional and maintainable documents, keep these tips in mind:

  1. Don't Manual Number: Never type \section{1. Introduction}. LaTeX handles numbering automatically. If you type the number yourself, you lose the ability to easily rearrange your document.
  2. Use Labels for Referencing: If you want to refer to a section later, use the \label{sec:name} and \ref{sec:name} system. This ensures that if the section number changes, the reference in your text updates automatically.
    \section{Experimental Setup} \label{sec:setup}
    As discussed in Section \ref{sec:setup}...
  3. Logical Order: Always use headings in a logical, descending order. Don't jump from a \section to a \subsubsection just because you like the font size of the sub-subsection.
  4. Consistency in Capitalization: Decide on a capitalization style (e.g., "Title Case" or "Sentence case") and stick to it for all your section titles.
  5. Short Titles for ToC: If you have a very long section title that looks messy in the Table of Contents, you can provide a shorter alternative in square brackets:
    \section[Short Title]{This is a Very Long and Detailed Section Title for the Main Page}

By using these structural commands correctly, you allow LaTeX to handle the "engineering" of your document's layout, leaving you free to focus entirely on the quality of your writing.