# How can I define an new index command that works better for paragraphs?

Suppose that, in the middle of my document, I have paragraph about owls: Here is a new paragraph about owls. Owls are extraordinary.

I want to index this paragraph using imakeidx, so I write, \index{owls}Here is a new paragraph about owls. Owls are extraordinary.

Problematically, however, if this paragraph ends up at the top of a new page k, then the entry for owls in the index refers to the previous page; it has a page "k-1" beside it rather than a "k" (see for example the minimal working example below).

The easiest solution is to move the \index command just inside the paragraph: Here\index{owls} is a new paragraph about owls. Owls are extraordinary.

For me, however, this is not ideal. I have a long document and many of the paragraphs have many associated \index commands. Moving these \index commands inside their corresponding paragraphs makes the paragraphs less readable for editing.

While the above describes the simplest case of \index, there is a similar issue when specifying a page range. I would like to simply write, \index{owls|(}Here is a new paragraph about owls. Owls are extraordinary.\index{owls|)} rather than, Here\index{owls|(} is a new paragraph about owls. Owls are extraordinary.\index{owls|)}.

This leads me to my question. How do I define a new command \indexn that yields correct page numbers in the index for the following two cases: \indexn{owls}Here is a new paragraph about owls. Owls are extraordinary. and, \indexn{owls|(}Here is a new paragraph about owls. Owls are extraordinary.\indexn{owls|)}?

A minimal working example of the issue is below.

\documentclass{book}

\usepackage{imakeidx}
\makeindex

\begin{document}

% This code block just generates 552 copies of the word "lorem".
\newcount\zz
\loop
lorem
\ifnum\zz<552
\repeat

\index{owls}Here is a paragraph about owls.  Owls are extraordinary.
%\index{owls|(}Here is a new paragraph about owls.  Owls are extraordinary.\index{owls|)}

\printindex

\end{document}

• \mbox{}\index{owls} or \leavevmode\index{owls} would work – David Carlisle Feb 25 '19 at 18:59
• This is exactly what I was looking for. Thank you. – SapereAude Feb 25 '19 at 22:34
• +1 for a quite detailed question – AndiW Feb 25 '19 at 22:48
• An index is about words, not paragraphs. – egreg Feb 25 '19 at 22:54

\mbox{}\index{owls} or \leavevmode\index{owls} would work