# \index after mathmode

I want to create a range index for some part of my document. But when I do the following:

\documentclass{article}

\makeindex

\begin{document}

\index{test|(}
Some Text
$1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, \ldots$
\index{test|)}

\paragraph{Test2:} More Text after too much space.

\end{document}


there is too much vertical space in front of the paragraph "Test2". I tried using \leavevmode or \ignorespace as recommended in » \index causing spurious whitespace « but that did not work.

Please notice also that I'm putting this construction into an abstract environment definition, so the specifics of the section to be indexed or what comes after may change. So to make that clearer, the example above would take the form:

\documentclass{article}

\newenvironment{idx}[1]{%
\index{#1|(}%
\def\currentidx{#1}}%
{\index{\currentidx|)}}

\makeindex

\begin{document}

\begin{idx}{test}
Some Text
$1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, \ldots$
\end{idx}

\paragraph{Test2:} More Text after too much space.

\end{document}

-
put the \index{test|)} inside the math expression – Herbert Jun 20 '12 at 7:23
That is not possible. Maybe I should have elaborated on why with a code example. – canaaerus Jun 20 '12 at 7:31
I think @Herbert's suggestion is correct. If you are trying to index the end of the math environment, that is inside the display math. Otherwise there is blank space added after display math and you are indexing the end of the space which might result in you indexing on to the next page. – Peter Grill Jun 20 '12 at 7:58
Also see Why is [ … ] preferable to $$?. – Peter Grill Jun 20 '12 at 8:01 ok, actually I'm always using amsmath's environments ;-) – canaaerus Jun 20 '12 at 14:05 ## 1 Answer Unfortunately, an \index command just after a math display restarts the suspended paragraph. I suggest you a strategy similar to \qedhere of amsthm. If your idx environment happens to end with a display or a list, put \idxhere before the end of that subenvironment: in these case the \index command after the subenvironment would not only create a new line, it may also spoil the reference, getting an off-by-one page number. \documentclass{article} \makeindex \newif\ifidxhere \newenvironment{idx}[1] {\global\idxherefalse \index{#1|(}% \def\currentidx{#1}% \ignorespaces} {\ifidxhere\else\index{\currentidx|)}\fi\ignorespacesafterend} \newcommand\idxhere{\index{\currentidx|)}\global\idxheretrue} \begin{document} \begin{idx}{test} Some Text $1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, \ldots \idxhere$ \end{idx} \paragraph{Test2:} More Text after too much space. Some Text $1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, \ldots$ \paragraph{Test2:} More Text after too much space. \end{document}  Never use $$ to get display math in LaTeX.

-
 It's a pity that this is the only way. Maybe in LaTeX3 everything will be better :-D – canaaerus Jun 20 '12 at 14:09