Introduction to Indices and Glossaries
In long documents like theses, books, or technical manuals, helping the reader navigate complex information is essential. Two of the most powerful tools for this are the Index and the Glossary.
- An Index is an alphabetical list of keywords and phrases found at the end of a document, indicating the page numbers where they occur.
- A Glossary is a list of specialized terms with their definitions, often including a list of acronyms.
LaTeX automates the tedious parts of creating these lists—sorting, page numbering, and formatting—so you can focus on your content.
Creating a Professional Index
To create an index, LaTeX needs to track every time you "mark" a word. We primarily use the makeidx package for this purpose.
The Basic Workflow
- Include the package: Add
\usepackage{makeidx}to your preamble. - Initialize the index: Add
\makeindexin the preamble. - Mark terms: Use the
\index{term}command throughout your document. - Print the index: Place
\printindexwhere you want the index to appear (usually at the very end).
Basic Example
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{makeidx}
\makeindex % Initializes index generation
\begin{document}
In this section, we discuss the behavior of electrons \index{electron}
and protons \index{proton}. Atoms \index{atom} are the building
blocks of matter.
\printindex % This is where the index will be generated
\end{document}Note: To see the index, you usually need to run LaTeX, then run a tool called
makeindex, and then run LaTeX again. Most modern editors (like TeXstudio or Typevia) handle this automatically, but it's important to know why your index might not appear on the first click!
Advanced Indexing Techniques
Simply listing words is often not enough. You might want to group topics or refer readers to other entries.
1. Sub-entries
If you want to group "Subatomic particles" and list "Electron" under it, use the exclamation mark !.
\index{Subatomic particles!electron}
\index{Subatomic particles!proton}2. Page Ranges
To index a discussion that spans several pages, use |( to start the range and |) to end it.
\index{Quantum Mechanics|(}
... multiple pages of text ...
\index{Quantum Mechanics|)}3. "See" and "See also"
If a user looks up "Computers," you might want to redirect them to "Microprocessors." Use the |see{...} syntax.
\index{Computers|see{Microprocessors}}4. Formatting Page Numbers
You can format the page number (e.g., to indicate a primary definition) using a backslash inside the index command:
\index{Gravity|textbf} % This will make the page number boldManaging Glossaries and Acronyms
While makeidx is great for keywords, the glossaries package is the gold standard for definitions and acronyms. It is highly flexible and handles pluralization and capitalization automatically.
Setting up the Glossary
In your preamble, you must define your terms.
\usepackage[acronym]{glossaries}
\makeglossaries
% Defining a glossary term
\newglossaryentry{latex}{
name=LaTeX,
description={A document preparation system for high-quality typesetting}
}
% Defining an acronym
\newacronym{gcd}{GCD}{Greatest Common Divisor}Using Terms in Text
The magic of the glossaries package is how it handles the first and subsequent mentions of a term.
\gls{label}: Standard usage.\Gls{label}: Capitalizes the first letter (useful at the start of a sentence).\glspl{label}: Plural form.
\begin{document}
The first time I use \gls{gcd}, it shows the full name.
The second time I use \gls{gcd}, it only shows the acronym.
Check out \gls{latex}; it is great for typesetting.
\printglossaries
\end{document}Note: Similar to the index, glossaries require an external tool (
makeglossaries) to sort the entries. If your glossary is empty, check your compiler settings.
Compiling Your Document
Indices and glossaries are "multi-pass" features. Because LaTeX doesn't know which page a term is on until it finishes the layout, the process usually looks like this:
- PdfLaTeX: Scans the document and writes terms to a temporary file (
.idxor.glo). - MakeIndex / MakeGlossaries: Reads those files, sorts them alphabetically, and writes a processed file (
.indor.gls). - PdfLaTeX: Reads the processed file and actually draws the Index/Glossary in the PDF.
Tip: If you are working locally, using a tool like latexmk or the "Magic Comments" in editors can automate these three steps into one click.
Best Practices and Common Pitfalls
Best Practices
- Be Consistent: Always index the same concept with the same term.
\index{Equation}and\index{equations}will create two separate entries. - Index Important Concepts Only: Don't index every single instance of a word. Index the locations where the concept is actually explained or significantly discussed.
- Placement: Place
\index{...}commands immediately after the word they refer to, without a space, to ensure the page number is accurate (e.g.,word\index{word}).
Common Pitfalls
- Missing
\makeindex: If you forget this in your preamble,\indexcommands will be ignored. - Special Characters: If you need to index a symbol like
@,!, or|, you must "escape" it using a double quote (e.g.,\index{"@}). - Forgotten Compilations: If your page numbers are wrong or your index is missing entries you just added, run the compilation sequence (LaTeX -> MakeIndex -> LaTeX) again.
Real-World Application: The "Glossary + Index" Combo
In a technical thesis, it is common practice to have a List of Abbreviations at the beginning (using the acronym option in glossaries) and an Index at the end. This structure provides a professional roadmap for your readers, making your document much more accessible.
