## Yet more LaTeX indexing woes

Still more indexing woes. LaTeX indexing, while superficially easy to use, turns out to be rather finicky, especially for anchor placement—that is, where the index (hyper)link actually points to.

Let’s see what I have found to circumvent some of the difficulties.

Naïvely, I expected the \index command to ignore surrounding spaces and glue itself to whatever came next. But, you’ve guessed, that’s not how LaTeX works (and I should’ve known). If an \index entry lies alone on a line, LaTeX considers it followed by a space, which, of course, detaches it from what follows. There’s no obvious problem until you manage to get a paragraph that starts at the top of the page. Then, your index entries that precede it may actually be left on the previous page. The solution is to glue the \index to the following text using % signs.

Let’s examine the following LaTeX snippet:

Notice the % at the end of lines:

LaTeX ignores all leading white spaces (from the start of the line to the start of the text) and lines ending with % (without spaces before) are considered as a line continuing line, that is, without a newline. The net effect is that the use of %, as shown in the image, is that all \index entries are seen as contiguous. More, they’re all using the same anchor: the start of the paragraph. If the paragraph starts at the top of the page, the index entries will point on the same page, not on the preceding page, as they would if they weren’t by %.

*
* *