makeidx (or imakeidx), call \makeindex in the preamble, mark terms in the body with \index{term}, and place \printindex where the index should appear. Compile the document with pdflatex, run makeindex on the generated .idx file, and run pdflatex again to insert the sorted index.
Basic index setup
\makeindex opens a <jobname>.idx file during compilation and writes each \index{...} entry along with its page number. \index{...} prints nothing in the body text; attach it directly to the indexed word without an intervening space so TeX cannot insert a page break between the word and its marker. \printindex reads the sorted <jobname>.ind file produced by makeindex and typesets a two-column Index section (or chapter in book and report).
index-basic.tex
Ready to use this syntax? Continue in the browser editor when you want to adapt the example in a real project.Open LaTeX Cloud Studio
When the same term is indexed on multiple pages—such as LaTeX on pages 1 and 2 above—makeindex merges them into a single entry with comma-separated page numbers.
Subentries, custom sort keys, and special characters
makeindex reserves four punctuation characters inside \index{...} to control hierarchy, sorting, formatting, and escaping:
Use
! to group related terms under a main heading; makeindex supports three levels (\item, \subitem, and \subsubitem). Use @ whenever the displayed entry contains math mode, formatting commands, or macros whose raw characters would sort out of alphabetical order—for example, \index{LaTeX@\LaTeX} sorts under LaTeX instead of the backslash. To include a literal !, @, |, or " in an entry, prefix it with ".
subentries-sort.tex
Page ranges, formatted page numbers, and cross-references
Append| followed by an operator to mark multi-page discussions, style individual page numbers, or point readers to another heading:
Write the formatting macro name after
| without a leading backslash (|textbf, not |\textbf), because makeindex automatically prepends \ when writing the .ind file. Both makeidx and imakeidx define \see and \seealso, which discard the entry’s page number and print the cross-reference label instead.
ranges-crossrefs.tex
Choosing between makeidx and imakeidx
makeidxis a small package in the standard LaTeX base distribution (makeidx.sty). Because\makeindexand\indexlive in the LaTeX kernel,makeidxonly adds\printindex,\see, and\seealso. It leaves runningmakeindexto your build tool or command line and uses your document class’s default index layout.imakeidxextends\makeindexwith key-value options such astitle=...(custom heading),columns=...(number of columns),intoc(adds the index to the table of contents), andname=...for maintaining multiple indexes in one document. When TeX runs with restricted or unrestricted shell escape enabled,imakeidxinvokesmakeindexautomatically during the LaTeX pass. If you compile with-no-shell-escapeor pass\usepackage[noautomatic]{imakeidx}, automatic execution is disabled and you runmakeindexexternally; add thequietpackage option (\usepackage[noautomatic,quiet]{imakeidx}) to suppressimakeidx’s end-of-run reminder warning.
imakeidx-options.tex
Build sequence
Locally on the command line, building an index takes three passes (or four ifintoc adds the index to the table of contents):
makeindex main), or let latexmk run makeindex and the follow-up LaTeX passes automatically:
makeidx and imakeidx are pre-installed.
Common mistakes
- Forgetting
\makeindexin the preamble: Without\makeindex, LaTeX ignores\index{...}calls and writes no.idxfile;\printindexthen prints nothing undermakeidxand stops withPackage imakeidx Error: Misplaced \printindex.underimakeidx. - Forgetting to run
makeindex: On the first pass beforemakeindexruns, LaTeX logsNo file main.ind.and skips the index without failing. Runmakeindex main.idxand recompile. - Running
imakeidxunder-no-shell-escape: Disabling shell escape turns offimakeidx’s automaticmakeindexcall and logsPackage imakeidx Warning: Remember to run (pdf)latex again after callingwith`makeindex main.idx'andor call (pdf)latex with -shell-escape.. Runmakeindexexternally (or uselatexmk), and pass[noautomatic,quiet]if you want a clean log. - Adding a backslash after
|: Writing\index{term|\textbf}makesmakeindexoutput\\textbf{1}in the.indfile, which breaks the line and prints literaltextbf1. Always omit the backslash (\index{term|textbf}).
Related pages
Ready to add an index to a book or long report? Start from the Book template in LaTeX Cloud Studio and replacemain.tex with the complete example.