6

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}
bodo
  • 6,228

1 Answers1

6

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.

egreg
  • 1,121,712
  • It's a pity that this is the only way. Maybe in LaTeX3 everything will be better :-D – bodo Jun 20 '12 at 14:09