How to make an index in LaTeX

June 20, 2018*

*Last modified 11-Nov-19

I’m sure there are many ways to make an index in LaTeX, but I was asked this question recently and thought I’d put my response here, which is based on what I did in my combinatorics notes.

In the preamble, make a command \defw (short for “define word”), use the package imakeidx, and call \makeindex:

\usepackage{imakeidx}
\usepackage{xparse}
\NewDocumentCommand{\defw}{m o}{%
  {\emph{#1}}%
  \IfNoValueTF{#2}
    {\index{#1}}
    {\index{#2}}%
}
\makeindex

Generate the index at the end of the file with \printindex, like one does with a bibliography.

In the text, use \defw like this:

A graph $G$ is \defw{bipartite}[graph!bipartite] with bipartition
$(V_1,V_2)$ if every edge of $G$ contains one vertex of $V_1$ and one
vertex of $V_2$.

This will add the word “bipartite” to the index under the “graph” heading, and in the actual text it will put bipartite in italics.

You can also use it like this:

Let $X$ be a finite set with cardinality $|X| = n$. The \defw{power set}
is $\P(X)$, the collection of subsets of $X$. $|\P(X)|= 2^n$.

where you don’t specify a subheading. This part will just add “power set” to the index directly (not under a subheading).