25.2.2 \index

Synopsis:

\index{index-entry-string}

Declare an entry in the index. This command is fragile (see \protect).

For example, as described in Indexes, one way to get an index from what’s below is to compile the document with pdflatex test, then process the index entries with makeindex test, and then compile again with pdflatex test.

% file test.tex
  ...
W~Ackermann (1896--1962).\index{Ackermann}
  ...
Ackermann function\index{Ackermann!function}
  ...
rate of growth\index{Ackermann!function!growth rate}

All three index entries will get a page number, such as ‘Ackermann, 22’. LaTeX will format the second as a subitem of the first, on the line below it and indented, and the third as a subitem of the second. Three levels deep is as far as you can nest subentries. (If you add \index{Ackermann!function!growth rate!comparison} then makeindex says ‘Scanning input file test.idx....done (4 entries accepted, 1 rejected)’ and the fourth level is silently missing from the index.)

If you enter a second \index with the same index-entry-string then you will get a single index entry with two page numbers (unless they happen to fall on the same page). Thus, adding as for Ackermann.\index{Ackermann} later in the same document as above will give an index entry like ‘Ackermann, 22, 151’. Also, you can enter the index entries in any order, so for instance \index{Ackermann!function} could come before \index{Ackermann}.

Get a page range in the output, like ‘Hilbert, 23--27’, as here.

W~Ackermann (1896--1962).\index{Ackermann}
  ...
D~Hilbert (1862--1943)\index{Ackermann!Hilbert|(}
  ...
disapproved of his marriage.\index{Ackermann!Hilbert|)}

If the beginning and ending of the page range are equal then the system just gives a single page number, not a range.

If you index subentries but not a main entry, as with \index{Jones!program} and \index{Jones!results}, then the output is the item ‘Jones’ with no comma or page number, followed by two subitems, like ‘program, 50’ and ‘results, 51’.

Generate a index entry that says ‘see’ by using a vertical bar character: \index{Ackermann!function|see{P\'eter's function}}. You can instead get ‘see also’ with seealso. (The text ‘see’ is defined by \seename, and ‘see also’ by \alsoname. You can redefine these either by using an internationalization package such as babel or polyglossia, or directly as with \renewcommand{\alsoname}{Also see}.)

The ‘see’ feature is part of a more general functionality. After the vertical bar you can put the name of a one-input command, as in \index{group|textit} (note the missing backslash on the \textit command) and the system will apply that command to the page number, here giving something like \textit{7}. You can define your own one-input commands, such as \newcommand{\definedpage}[1]{{\color{blue}#1}} and then \index{Ackermann!function|definedpage} will give a blue page number (see Color). Another, less practical, example is this,

\newcommand\indexownpage[1]{#1, \thepage}
  ... Epimenides.\index{self-reference|indexownpage}

which creates an entry citing the page number of its own index listing.

The two functions just described combine, as here

\index{Ackermann!function|(definedpage}
  ...
\index{Ackermann!function|)}

which outputs an index entry like ‘function, 23--27’ where the page number range is in blue.

Consider an index entry such as ‘α-ring’. Entering it as $\alpha$-ring will cause it to be alphabetized according to the dollar sign. You can instead enter it using an at-sign, as \index{alpha-ring@$\alpha$-ring}. If you specify an entry with an at-sign separating two strings, pos@text, then pos gives the alphabetical position of the entry while text produces the text of the entry. Another example is that \index{Saint Michael's College@SMC} produces an index entry ‘SMC’ alphabetized into a different location than its spelling would naturally give it.

To put a !, or @, or |, or " character in an index entry, escape it by preceding it with a double quote, ". (The double quote gets deleted before alphabetization.)

A number of packages on CTAN have additional functionality beyond that provided by makeidx. One is index, which allows for multiple indices and contains a command \index*{index-entry-string} that prints the index-entry-string as well as indexing it.

The \index command writes the indexing information to the file root-name.idx file. Specifically, it writes text of the command \indexentry{index-entry-string}{page-num}, where page-num is the value of the \thepage counter. On occasion, when the \printindex command is confused, you have to delete this file to start with a fresh slate.

If you omit the closing brace of an \index command then you get a message like this.

Runaway argument?  {Ackermann!function
!  Paragraph ended before \@wrindex was complete.

Unofficial LaTeX2e reference manual