2

Consider the code:

\documentclass[openany]{book}
\usepackage{imakeidx}
\makeindex
\usepackage{idxlayout}

\begin{document} \thispagestyle{empty} \Large

This is a sentence. This is another \textbf{sentence}. \textbf{This} is too a sentence.

\vspace*{15pt}

This is a sentence. This is another \textbf{sentence}. \index{HEADING@\textbf{HEADING}! An index entry with a lot of words.} \index{HEADING@\textbf{HEADING}! A different index entry with some more words.} \textbf{This} is too a sentence. This is another sentence. This is a sentence.

\idxlayout{columns=1} \printindex \end{document}

with the first page of output:

enter image description here

Notice the (expected) proper horizontal spacing between the sentences in the first paragraph, even though in the code I had inserted extra spaces between the second and third sentences.

Remark: If I had inserted in the code, say, ten extra spaces between the said sentences, I would expect the same output.

Now, look at the output of the second paragraph---and in particular---the extra horizontal space that has been inserted between the second and third spaces. The difference in the code is---that I have inserted a couple of index commands, which, many times, appears to have no affect on the horizontal spacing between two consecutive sentences at all.

But here, it does.

QUESTION: Can anyone tell me what might be causing this unwanted phenomenon and is there a remedy for it? Perhaps, more importantly, is there a proper way to insert indexing code? Is it O.K. to leave a space between two consecutive index commands or should they be inserted consecutively without interruption as in one long command?

Note: I have added the bold for visual emphasis. The described phenomenon occurs whether or not bold is used. I compile the code with lualatex.

Thank you.

DDS
  • 8,816
  • 4
  • 9
  • 36
  • 3
    \index should normally be placed attached to the word you want to index, for a couple of reasons; one is to avoid unwanted spaces. – egreg Mar 03 '22 at 20:59
  • Thank you @egreg. Also, if I may, should two consecutive indices \index{} and \index{} be coded \index{} \index{} or \index{}\index{}? Or does it not matter? – DDS Mar 03 '22 at 21:09
  • Same problem, isn't it? – egreg Mar 03 '22 at 21:14

1 Answers1

3

The spacing you observe doesn't depend on the long index entries, but just on the fact that spaces around \index{...} count as spaces in output.

I reuse code by Marcin Woliński which allows to show spaces in order to illustrate what happens.

\documentclass{article}
\usepackage{makeidx}
\makeindex

\newcommand{\AND}{\unskip \hskip 2pt plus 1pt minus 1pt \cleaders\copy\ANDbox\hskip\wd\ANDbox \hskip 2pt plus 1pt minus 1pt \ignorespaces} \newsavebox\ANDbox \sbox\ANDbox{\textbullet}

\begin{document}

\catcode` =\active \let =\AND

This is a sentence. % This is another \textbf{sentence}. % \textbf{This} is too a sentence.

\vspace*{15pt}

This is a sentence. % This is another \textbf{sentence}. \index{a} \textbf{This} is too a sentence. % This is another sentence. This is a sentence.

\vspace*{15pt}

This is a sentence. % This is another \textbf{sentence}. \index{b} \index{c} \textbf{This} is too a sentence. % This is another sentence. This is a sentence.

\end{document}

enter image description here

You can see that in the second paragraph there are two spaces, due to a single \index command, and in the third paragraph there are three.

So, first of all, no spaces between the word to index and the \index command. Consider also that a space would allow a line break, which in turn could allow a page break and you'd find the index entry referring to a different page than the word it points to.

What's the best position of \index? In front of the word you want to index, which also has the benefit of disallowing hyphenation.

\documentclass{article}
\usepackage[english]{babel}
\usepackage{makeidx}
\makeindex

\newcommand{\AND}{\unskip \hskip 2pt plus 1pt minus 1pt \cleaders\copy\ANDbox\hskip\wd\ANDbox \hskip 2pt plus 1pt minus 1pt \ignorespaces} \newsavebox\ANDbox \sbox\ANDbox{\textbullet}

\begin{document}

\catcode` =\active \let =\AND

This is a sentence. % This is another \textbf{sentence}. % \textbf{This} is too a sentence.

\vspace*{15pt}

This is a sentence. % This is another \index{a}\textbf{sentence}. \textbf{This} is too a sentence. % This is another sentence. This is a sentence.

\vspace*{15pt}

This is a sentence. % This is another \index{b}\index{c}\textbf{sentence}. \textbf{This} is too a sentence. % This is another sentence. This is a sentence.

\end{document}

enter image description here

In tough paragraphing cases, you can decide what to do, maybe using

\index{entry}\nolinebreak\hspace{0pt}longwordwithindexthatmustbehyphenated

Example:

Wordthatcanbehyphenated wordthatcanbehyphenated
\index{entry}\nolinebreak\hspace{0pt}longwordwithindexthatmustbehyphenated
wordthatcanbehyphenated

enter image description here

egreg
  • 1,121,712