Four files, two programs, one job
Type \index{Galois theory} and LaTeX does not index anything. It writes a line \indexentry{Galois theory}{3} into a file called main.idx and carries on typesetting. Nothing about that line is sorted, merged or formatted — it is a raw log of "this string was mentioned on this page", in reading order, one line per call.
The sorting is done by an external program, and there are two of them. makeindex is the classic: a small C tool shipped with every distribution, invoked as makeindex main.idx, which reads the .idx, collates the entries byte by byte and writes main.ind — a file containing a theindex environment with the hierarchy applied, duplicates collapsed, page lists compressed into ranges. It also writes main.ilg, its own log, which is where a mysteriously missing entry is usually explained.
xindy is the replacement, and the difference is the whole reason to care: makeindex compares bytes, xindy compares strings in a language. It ships with index style modules per language and script, so accented letters sort after their base letter (é under E, not under a punctuation block), and it handles Cyrillic, Greek and transliteration. The trade is that it needs its own input dialect, which is why packages default to makeindex.
Finally \printindex reads the .ind back into the document. That is the fourth file in the chain and the one that lets you understand every index bug: .tex → .idx → (.ist + makeindex/xindy) → .ind → PDF. If \printindex prints nothing, the question is always which of those four steps produced an empty or stale file.
makeidx versus imakeidx: who runs the tool
The old interface is the kernel-adjacent makeidx package: \usepackage{makeidx}, \makeindex in the preamble, \printindex where you want it — and you run makeindex between passes. On the command line that is pdflatex book, makeindex book, pdflatex book. It works, and it is the reason so many people believe an index "needs three compiles": it needs two compiles plus one index run, and the extra compile exists because the index changes the page numbers the entries were written with.
imakeidx keeps the same commands and adds the missing part: it calls makeindex for you at the end of the run (or at the end of latexmk), so a single compile produces a finished index. It also gives the index real column typesetting — the output is set with multicol, so columns are balanced and the index does not force a page break — and per-index options. Load it as \usepackage{imakeidx} and declare with \makeindex[title=Index, columns=2]. To run the tool externally after all, pass [makeindex=false]; to switch program, pass program=xindy. If you want the index to start on a fresh page, add \clearpage before \printindex yourself, or ask for it through the nonewpage option in the other direction.
With latexmk, imakeidx needs nothing special, but if you stay on makeidx the right flag is latexmk --makeindex -pdf book.tex, or $makeindex = 1; in .latexmkrc. Editors differ: several "LaTeX word count" and "build" buttons ignore the index step entirely, which produces the classic report of an index that works locally and is missing from the version the co-author compiled.
A warning about the two-stage design that bites exactly once per project: an entry can only be as correct as the page it was written from, and adding the index material may lengthen the document and shift every page. So after editing entries near the front of a long book, run the index step again — an .ind generated from an older .idx looks entirely plausible while being wrong on every line.
\text{entry} = (\text{key},\ \text{page}) \qquad \text{both halves come from the previous pass}
Entry grammar: levels, sort keys and the encapsutor
Five characters do all the work inside \index{...}. ! separates hierarchy levels, so \index{Integral!Lebesgue} nests under Integral and \index{Zeta function!values!at even integers} nests twice. Levels are discovered by sorting, not by declaration: a sub-entry automatically acquires the primary term as its heading, which is why a typo in the primary level produces a whole orphan heading. @ splits sort key from printed form: \index{Bourbaki@Bourbaki, Nicolas} sorts under the key and displays after the @. | introduces an encapsutor, a command applied to the page number: \index{group|textbf} bolds it.
An encapsutor can also mark a range. Open it at the first mention with \index{factorial|(textbf} and close it at the last with \index{factorial|)}; makeindex then writes \(textbf{245--248} and the encapsutor is applied to the range rather than to a single page — the convention for the pages where a concept is actually developed. " is the escape character, because makeindex reserves ! @ | and the quote itself: \index{"@\textbackslash\ bs} puts a literal symbol into the index by quoting the character it sorts by. And \see (from makeidx) produces a cross-reference line with no page number: \index{Cohomology!de Rham|see{De Rham cohomology}}.
The two forms that people get wrong most often are worth spelling out. Keys are compared before any LaTeX is expanded, so the part before @ should be plain ASCII, and math in an entry belongs after the @: \index{sigma-algebra@$\sigma$-algebra} sorts under s and displays the symbol. Second, an entry containing \\ or a paragraph break is not an error to LaTeX but is one to makeindex — keep entries to one line.
A practical habit: index the mention, not the heading. Putting \index{...} immediately after the word or formula it describes is what keeps the entry on the right page; putting it at the end of a paragraph risks landing on the next page, and putting it inside a \section title means it is executed again when the table of contents is typeset.
\text{Zeta function ! values ! at even integers} \;\longmapsto\; \zeta(2) = \tfrac{\pi^{2}}{6}
Accents, math symbols and the entries that sort wrong
This is the failure every non-English book meets. makeindex is an 8-bit text tool that never talks to LaTeX, so it sorts on the bytes it sees after the .idx line is written. An entry like \index{\'Ecole de Paris} — a backslash, an accent macro, braces — is sorted by its first non-blank byte, which is the backslash, so the whole name lands in the punctuation block at the front of the index, under a heading that does not exist. Accented proper nouns, entries beginning with math, and names containing braces all fail the same way.
The fix under makeindex is always the sort key: write the ASCII form before @ and the typeset form after it, enclosing anything fragile in braces so grouping survives — \index{Ecole@{\'Ecole} de Paris} or, with UTF-8 input, \index{Beyonce@Beyoncé}. That is not a workaround to be ashamed of: it is what the @ field exists for, and it also solves the "sort under the surname, display the given name" case for every entry in a bibliography-style index.
The fix under xindy is structural: xindy reads the characters, so accented entries sort correctly without keys at all, and you select the collation with its language module — \makeindex[program=xindy, options=-L german -C utf8] for German (including the DIN ordering variants), -L russian for Cyrillic, and so on. xindy additionally understands math entries encoded through its tex input style, which is the option to reach for when an index of notation is more than a list of symbol names.
Mathematical entries deserve a rule of their own: index the name, not only the symbol, and put the symbol in the display part. \index{sigma-algebra@$\sigma$-algebra} gives an entry a reader can find when they remember the word, and a second entry under "S" catches those who remember only the letter. An index of notation is normally a separate index for exactly this reason, and it is the one place where a key-less \index{\sigma}|-style approach is acceptable, because the reader expects the symbols to be sorted as strings.
\underbrace{\text{sorts under B}}_{\text{key}} \quad \text{prints as} \quad \underbrace{\text{Bourbaki, Nicolas}}_{\text{display}}
% ---- the style file, written by the document itself before the class ----
\begin{filecontents*}{book.ist}
preamble "\\begin{theindex}\n"
postamble "\n\n\\end{theindex}\n"
headings_flag 1 % emit a heading for each new letter
heading_prefix "\\vspace*{14pt}\\nopagebreak\\textbf{\\large "
heading_suffix "}\\nopagebreak\n"
symhead_positive "Symbols"
numhead_positive "Numbers"
delim_0 ", "
delim_1 ", "
delim_2 ", --"
encap_prefix "\\textbf{"
encap_infix "}--\\textbf{"
encap_suffix "}"
\end{filecontents*}
\documentclass[12pt,a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage[margin=2.5cm]{geometry}
\usepackage{hyperref}
\hypersetup{colorlinks=true, linkcolor=blue}
\hypersetup{hyperindex=false} % index page numbers as plain text, not links
\usepackage{imakeidx}
\makeindex[options=-s book.ist, title=Index]
\begin{document}
\chapter{Names and accented entries}
% Sort key before @, printed form after it. The key is what makeindex sorts;
% it must be plain ASCII, because makeindex is an 8-bit tool that never sees
% the fonts LaTeX will use.
Beyoncé\index{Beyonce@Beyoncé} appears beside Bourbaki\index{Bourbaki}.
\'Ecole\index{Ecole@{\'Ecole} de Paris} sorts under E, not under the backslash.
% makeindex reserves ! @ | and the quote character; the quote protects them,
% so these three entries display the characters themselves.
The reserved characters are \index{at@"@}, \index{bang@"!} and
\index{pipe@"|}.
% An index call belongs in the text, not in the heading: the heading is
% re-typeset for the table of contents, which would write the entry twice.
\section[Density]{Density}
% If a heading really must carry one, \protect it and give a short optional
% form: \section[Density]{Density\protect\index{Density}}
The Radon--Nikod\'ym derivative\index{Radon-Nikodym@Radon--Nikod\'ym}
$d\mu/d\nu$ is the density of $\mu$ with respect to $\nu$%
\index{Density}\index{Measure!density}.
% Entries inside a footnote are legal; keep them short, and remember that the
% page recorded is the page the footnote is set on.
Text.\footnote{Indexed fact.\index{Footnote!entries in}}
\printindex
\end{document}
Two or three indices in one document
A textbook usually needs at least two: a subject index for concepts and a notation index for symbols. With imakeidx you declare each one with a name — \makeindex[name=symbols, title=Index of Notation, columns=2] — and then write into it by name: \index[symbols]{\sigma-algebra}. An unqualified \index{...} always goes to the default index, so mixing the two forms is legal and is the usual arrangement. Print each with \printindex[symbols].
Under the older makeidx this needs a third-party package (multind, index) and a manual run of makeindex per index, which is the strongest practical argument for imakeidx. With several indices, each has its own .idx and .ind pair, and latexmk or imakeidx runs the tool once per file. Keep in mind that the page numbers each index records are the page numbers of that run, so every index needs to be regenerated together, not one at a time.
A helper macro is worth defining once, as long as it carries the sort key as an argument of its own: \newcommand{\sym}[2]{\ensuremath{#1}\index[symbols]{#2@\ensuremath{#1}} prints a symbol and indexes it under an ASCII key, which is the difference between a notation index that sorts and one that lands in the punctuation pile. For prose, \newcommand{\xdx}[2]{#1\index{#2}} does the same job. Both keep the entry next to the occurrence, which is what keeps the page numbers honest. The exception is a term you want indexed once at its definition but mentioned forty times: index only the definition, and add a |see entry at the first mention of the alternative spelling.
\documentclass[12pt,a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage[margin=2.5cm]{geometry}
% ---- two independent index streams ---------------------------------
\usepackage{imakeidx}
\makeindex[title=Subject index]
\makeindex[name=symbols, title=Index of Notation, columns=2]
% xindy instead of makeindex: correct Unicode sorting, language collation.
% \makeindex[program=xindy, options=-L english -C utf8 -I xindy]
% A notation entry needs an ASCII sort key, so the helper takes two arguments:
% #1 is what is typeset, #2 is what makeindex sorts on.
\newcommand{\sym}[2]{\ensuremath{#1}\index[symbols]{#2@\ensuremath{#1}}}
\begin{document}
\chapter{Measure}
Let \sym{\Omega}{Omega} be an open set and \sym{\mu}{mu} a measure on it, so
that \sym{\sigma(\mathcal{A})}{sigma(A)} is the smallest
$\sigma$-algebra containing \sym{\mathcal{A}}{A set}.%
\index{sigma-algebra@$\sigma$-algebra}%
\index{Measure!countably additive}
A function is integrable when
\[
\int_{\Omega} \lvert f \rvert \,d\mu < \infty .
\]
\index{Integral!Lebesgue}%
\index{Integrable function}
The \emph{support} of $f$ is the closure of the set where it is non-zero.%
\index{Support (of a function)}
\chapter{Function spaces}
The space $L^{p}(\Omega)$ is complete for $1 \le p \le \infty$;%
\index{Space!Lp@$L^{p}$|(textbf}
see \index{Space!Lp@$L^{p}$!duality|see{Duality of Lp spaces}}the duality with
$L^{q}$ where $1/p + 1/q = 1$.%
\index{Space!Lp@$L^{p}$|)}
\clearpage
\printindex % subject index
\printindex[symbols] % notation index, printed after it
\end{document}
Sorting and layout: what the .ist file changes
The index style file (extension .ist, passed with options=-s book.ist) is makeindex's only appearance control, and it holds decisions no LaTeX command can reach. headings_flag 1 asks for a letter heading before each group; heading_prefix and heading_suffix wrap it, and inside those strings \letter is replaced by the letter — which is why they have to be double-escaped in the style file. symhead_positive and numhead_positive name the pseudo-groups where symbols and digits land, and without them your \sigma entries appear under a bare heading-less block.
The delim_0, delim_1, delim_2 keys set the separators before a page number at each level — the conventional British style is ", " for the first two and ", --" for the third, so a tertiary entry reads as a dash rather than a comma list. encap_prefix/encap_infix/encap_suffix control how a bold range is written, and keyword_group keeps a group of terms together — useful for the "see also" clutter at the end of an entry.
The typographic part of the index is LaTeX's, in the theindex environment: \parindent=0pt, a small vertical space between groups via \indexspace, and level indentation set by the class. To change fonts or indentation you either hand imakeidx extra code with \indexsetup{othercode=\footnotesize}, or you redefine theindex in a class file. And to get the index into the table of contents, add the line yourself — \addcontentsline{toc}{chapter}{Index} before \printindex — which is the same trick every other unnumbered heading uses.
Build order, hyperref and the missing entry
The index interacts with hyperref in one visible way: by default hyperref turns index page numbers into links, and a page range then becomes one long blue band, which most readers find worse than no link at all. \hypersetup{hyperindex=false} keeps the numbers plain while leaving the rest of the document clickable; if you want links, the companion option hyperindexformat lets you apply a macro per number, and the |hyperpage encapsutor handles the ranges correctly.
Entries disappear for a short list of causes, and it is faster to run through them than to reason about them. The \index call sits in a moving argument and got expanded twice — protect it with \protect\index{...} or move it out of the heading. It sits in a \footnote of a float, which is typeset in a different place from where the source says. It sits inside a verbatim environment, where no command executes at all. It was never read, because the file containing it is inside an \include{} that an \includeonly{} list excludes. And finally the .idx is present but the .ind is stale, which the makeindex log .ilg will confirm by its timestamp.
The order that always converges is: compile, index, compile, index, compile — three LaTeX passes with the index step after the second, because the second pass changes pagination and therefore the very page numbers the index recorded. latexmk with imakeidx automates exactly this and stops when nothing changes; if you script the build yourself, do not "optimise" the final pass away, because an index that is one pagination out is worse than one that is missing.
Last, the editorial question that no package answers: how many entries. An index that lists every occurrence of "function" is not an index but a concordance, and the standard convention is to mark the pages where a concept is defined or developed (which is what the bold range is for), to index names once per substantive discussion, and to write cross-references for the synonyms a reader might actually try. An index built by adding \index{} at every mention takes longer to read than to look things up in the table of contents.
\{245, \mathbf{248}, \mathbf{249}, \mathbf{250}, 251\} \quad \longrightarrow \quad 245, \ \mathbf{248\text{--}251}
\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage[a4paper,margin=2.5cm]{geometry}
\usepackage{imakeidx} % loads makeidx AND runs makeindex for you
\makeindex[title=Index, columns=1]
\begin{document}
\section{Three special values}
The Gaussian integral%
\index{Integral!Gaussian}%
\index{Gaussian integral}
\[
\int_{0}^{\infty} e^{-x^{2}}\,dx = \frac{\sqrt{\pi}}{2}
\]
is the prototype of a factorial bound, since $n!$ eventually dominates
$a^{n}$ for every fixed $a$.%
\index{Growth!of a factorial}%
\index{Factorial|(textbf} % open a bold page range
Stirling's formula gives $n! \sim \sqrt{2\pi n}\,(n/e)^{n}$.%
\index{Stirling's formula}
The same factorial appears in the binomial
coefficient $\binom{n}{k} = n!/(k!(n-k)!)$%
\index{Binomial coefficient}%
\index{Factorial|)} % close the bold page range
\section{Values of the zeta function}
Euler found $\zeta(2) = \pi^{2}/6$%
\index{Zeta function!values!at even integers}
and, more generally, $\zeta(2m)$ is a rational multiple of $\pi^{2m}$ for every
$m \ge 1$.%
\index{Zeta function!values!at even integers}
For the half-integer argument of Gamma, see
\index{Gamma function!at one half}$\Gamma(1/2) = \sqrt{\pi}$.\index{Pi}
\printindex % reads main.ind
\end{document}
Lesson cheat sheet
One key line of code from each section — jump to it or copy it straight from here.
\text{entry} = (\text{key},\ \text{page}) \qquad \text{both halves come from the previous pass}
\text{Zeta function ! values ! at even integers} \;\longmapsto\; \zeta(2) = \tfrac{\pi^{2}}{6}
\begin{filecontents*}{book.ist}
\documentclass[12pt,a4paper]{report}
\documentclass[11pt,a4paper]{article}
Common mistakes
- Running only LaTeX and wondering why
\printindexprints nothing. The index is made by a second program; withimakeidxit runs automatically, withmakeidxyou must runmakeindex mainbetween passes and then compile twice more. - Indexing an accented name as
\index{\'Ecole}: the entry sorts on the backslash and lands in the punctuation block. Write the ASCII sort key before@—\index{Ecole@{\'Ecole} de Paris}— or use xindy. - Writing
\index{Integral!lebesgue}in one place and\index{Integral!Lebesgue}in another: two headings that look identical in print, and page numbers split between them. Sub-entry strings are compared exactly. - Putting
\index{...}inside a\sectiontitle without\protect: the heading is re-typeset for the table of contents, so the entry is written twice and possibly against the wrong page. - Expecting hyperref-linked index page ranges to look tidy. A bold range plus a coloured link is unreadable; set
hyperindex=false, or use the|hyperpageencapsutor rather than the default. - Deleting the
.idx,.indand.ilgfiles in the same script that cleans the build directory and then shipping an index built from an earlier run — or, conversely, committing the.indand never regenerating it after the chapter rewrites.
Questions about this lesson
What is the difference between makeidx and imakeidx?
\makeindex, \printindex and the small helpers \see and \indexspace, and leaves the index program to you: compile, run makeindex, compile again. imakeidx keeps the same commands, invokes the program for you at the end of the run, sets the index in balanced multicols without forcing a page break, and supports named multiple indices with per-index options such as title=, columns=, program=xindy and options=-s style.ist.Why is my index entry missing?
\include file never executes), is the entry inside a moving argument without \protect, is it in verbatim material, and is the .ind file older than the .idx? Then read the makeindex log .ilg, which reports entries it could not parse — LaTeX itself stays quiet about those.How do I index a name so it appears under the surname?
\index{Bourbaki@Bourbaki, Nicolas} sorts on the key before the at-sign and prints what follows it. The same mechanism rescues accented and symbolic entries, where the key has to be plain ASCII because makeindex sorts bytes, not characters.Can I have an index of notation as well as a subject index?
\makeindex[name=symbols,title=Index of Notation,columns=2], write into it with \index[symbols]{...}, and print it with \printindex[symbols]. Each index keeps its own .idx and .ind files, so regenerate them in one build rather than one at a time.Why do my accented index entries sort under symbols?
How many times do I have to compile a document with an index?
Live playground: edit the LaTeX, see it render
The preview updates as you type — break it on purpose to see how errors look.
Keep reading
Three ways into the same idea: the notation itself, the lesson that teaches it, and an article that applies it.