Next: , Previous: , Up: Front/back matter   [Contents][Index]

### 25.2 Indexes

If you tell LaTeX what terms you want to appear in an index then it can produce that index, alphabetized and with the page numbers automatically maintained. This illustrates the basics.

\documentclass{article}
\usepackage{makeidx}  % Provide indexing commands
\makeindex
% \usepackage{showidx}  % Show marginal notes of index entries
...
\begin{document}
...
Wilson's Theorem\index{Wilson's Theorem}
says that a number $n>1$ is prime if and only if the factorial
of $n-1$ is congruent to $-1$
modulo~$n$.\index{congruence!and Wilson's Theorem}
...
\printindex
\end{document}


As that shows, declare index entries with the \index command (see \index). When you run LaTeX, the \index writes its information, such as ‘Wilson's Theorem’ and the page number, to an auxiliary file whose name ends in .idx. Next, to alphabetize and do other manipulations, run an external command, typically makeindex (see makeindex), which writes a file whose name ends in .ind. Finally, \printindex brings this manipulated information into the output (see \printindex).

Thus, if the above example is in the file numth.tex then running ‘pdflatex numth’ will save index entry and page number information to numth.idx. Then running ‘makeindex numth’ will alphabetize and save the results to numth.ind. Finally, again running ‘pdflatex numth’ will show the desired index, at the place where the \printindex command is in the source file.

There are many options for the output. An example is that the exclamation point in \index{congruence!and Wilson's Theorem} produces a main entry of ‘congruence’ with a subentry of ‘and Wilson's Theorem’. For more, see makeindex.

The \makeindex and \printindex commands are independent. Leaving out the \makeindex will stop LaTeX from saving the index entries to the auxiliary file. Leaving out the \printindex will cause LaTeX to not show the index in the document output.

There are many packages in the area of indexing. The showidx package causes each index entries to be shown in the margin on the page where the \index appears. This can help in preparing the index. The multind package, among others, supports multiple indexes. See also the TeX FAQ entry on this topic, https://www.texfaq.org/FAQ-multind, and the CTAN topic, https://ctan.org/topic/index-multi.