1

If there are two \index{} commands containing the same string, but one with a line break, then that item appears as two different items in the index. See the MWE below (copy it with the line break in the first \index command) where the entry is foofoofoo barbarbar (once in one line in the .tex, once broken over a line). Is there a way to avoid that? One could escape all spaces with ~, but then line breaks of the entries in the index are not ideal anymore. Also, the entries foofoofoo~barbarbar and foofoofoo barbarbar are considered different, too, so forgetting to insert a tilde in one entry will also lead to a different entry. I couldn't find much/any information about line breaks in index commands interestingly...

\documentclass{scrartcl}

\usepackage{imakeidx}
\makeindex

\begin{document}
With editors that break lines automatically (to adhere to the, say 80, column
rule; for example Emacs), blanks in index commands are often automatically
broken over lines.

As an example to demonstrate this behavior, this leads to \index{foofoofoo
  barbarbar} (broken over lines in `.tex`) to appear as a different\clearpage
entry than the entry \index{foofoofoo barbarbar}. How can this be prevented? One
way would be to always avoid blanks in `index` commands via `~` but then the
items in the index are not broken over lines in an ideal way.

\printindex
\end{document}

1 Answer 1

1

The argument to \index is gathered in an almost-verbatim fashion, so in the .idx file you get

\indexentry{foofoofoo   barbarbar}{1}
\indexentry{foofoofoo barbarbar}{2}

The MakeIndex program by default respects multiple spaces, but you can tell it not to with the -c command line option.

OPTIONS
 -c  Compress intermediate blanks (ignoring leading and trailing blanks and
     tabs).  By default, blanks in the index key are retained.

You can pass it with the optional argument to \makeindex.

\documentclass{scrartcl}

\usepackage{imakeidx}
\makeindex[options=-c]

\begin{document}
With editors that break lines automatically (to adhere to the, say 80, column
rule; for example Emacs), blanks in index commands are often automatically
broken over lines.

As an example to demonstrate this behavior, this leads to \index{foofoofoo
  barbarbar} (broken over lines in `.tex`) to appear as a different\clearpage
entry than the entry \index{foofoofoo barbarbar}. How can this be prevented? One
way would be to always avoid blanks in `index` commands via `~` but then the
items in the index are not broken over lines in an ideal way.

\printindex
\end{document}

enter image description here

1
  • ... excellent, thank you very much, egreg! Commented Apr 22, 2022 at 22:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .