Index

Multiple Indexes with LaTeX

I recently taught a course on British Civilization at the Institut Libre Marie Haps in Brussels. I used \LaTeX to write my notes on the history of the UK and decided to write an index of names and another one of important Acts and Documents.

Since I had to dig to find out how to do this without a glitch in \LaTeX and create shortcuts in TextExpander, I thought I would put the procedure I use online in case others could use this info. Remember to read the Makeindex package documents for indexing options. If you use the memoir package you might want to read its information about indexing too.

To create an index you should use MakeIndex.

Creating an Index

To start with you insert the commands \\makeindex in your preamble before \\begin{document} and \\printindex where you want the index (usually at the end). A simplified file would be (I put the text in another file and call it using \\include or \\input).

%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode
\\documentclass[12pt,a4paper,book]{memoir}
\\usepackage{xltxtra}
\\makeindex
\\begin{document}
Your text
\\printindex
\\end{document}

To have items in your index you mark them in your text with the \\index{text} command. For example, to build a index of proper names I would have the following.

In 1066 William\\index{William I, the Conqueror} won the battle of Hastings.

Then your run Makeindex. There are different ways to do it. One of them in Textmate 2 is Bundles>LaTex>Run Makeindex.

Then you compile your document.

Writing Multiple Indexes

Suppose you want several indexes (or indices). In my course notes, for example, I have an index of important Acts and Documents and an index of proper names.

What you have to do is to have as many \\makeindex and \\printindex commands as you have indexes and to add the names of the indexes in your markup. In my case to have an index called names and an index called documents (you can pick any name you want for the index) I would have this in the preamble.

%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode
\\documentclass[12pt,a4paper,oneside,book]{memoir}
\\usepackage{xltxtra}
\\makeindex[names]
\\makeindex[documents]
\\begin{document}
Your text
\\printindex[names]
\\printindex[documents]
\\end{document}

The problem is that both indexes are printed with the title “Index”. To customize the printed titles of the indexes you need to use the \\renewcommand. So the end of the example would be


\\renewcommand{\\indexname}{Index of Names}
\\printindex[names]
\\renewcommand{\\indexname}{Index of Acts and Documents}
\\printindex[documents]
\\end{document}

In your documents you would add an option in the indexing mark up. This would be an example.

In 1066 William\\index[names]{William I, the Conqueror} won the battle of Hastings.
The Magna Carta was signed in 1215\\index[documents]{Magna Carta}

You then run the Makeindex command. You only need to do it once, even if you have several indexes. Then you compile the document as before.

Speeding the Indexing Process Up

Now, if you have a long document you don’t really want to have to enter all these \\index commands. There are several solutions to avoid the drudge. An obvious one is to do a search replace throughout the document (replace all William with William\\index[names]{William I, the Conqueror}. But you want to be careful before you click Replace all. To keep the example of English history, not all the occurrences of “William” will refer to the same person. Doing one replacement at the time would probably be wiser.

In some cases it might be better to index as you write or reread the final version and use a macro. In my case I first created a snippet recording a macro in Textmate 2. But then I decided to create a shortcut in TextExpander. This allows me to use the shortcut in another text editor if I want to (I am testing Sublime Text) and, since I use TextExpander with Dropbox, to have my shortcuts available on another computer if need be.

The idea of the shortcut is that you want to select a word or groups of words, copy them in the clipboard, type the command and put the word(s) between the brackets. But you also need to paste them back before the \\index command since they would be gone as soon as you start typing. So you need to select and copy them, paste them, type \\index[nameofindex]{paste them} and place your cursor after the final bracket (and insert a space if you want to). The resulting shortcut in TextExpander is this (I chose ::idxn and ::idxd as abbreviations).

%clipboard\\index[documents]{%clipboard}%|
%clipboard\\index[names]{%clipboard}%|

The %clipboard pastes the content just copied, the %| places the cursor where it appears after the shortcut is executed. When I am in Textmate and want to insert a word in an index, I select and copy it (Ctrl-w and Ctrl-c, unless you want to create a snippet to select and copy and assign a keyboard shortcut to it) and then type the appropriate shortcut. Et voilà!

The same idea can of course be used for other similar situations. There might be a better way to do this, but it works for me.

By the way, to learn how to index properly I used Indexing Books by Mulvany.