Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm having a problem with the \index command causing unwanted whitespace:

\documentclass[a4paper,11pt,oneside]{book}
\usepackage{makeidx}
\makeindex

\def \nothing #1{}

\begin{document}

\noindent The Principle of Availability\\\index{principle of availability}
What is said must be intuitively accessible to the conversational participants, 
unless something goes wrong and they cannot be counted as `normal interpreters'.

\end{document}

If this is compiled, the line starting with "What is said" has a slight indent. This indent is gone if I add a \relax, or a % after the index{...} command, I guess since that gobbles up the newline following it. If there is no index command at all (i.e. the line ends after the \\), or if I replace it by \nothing{...} the spurious whitespace is also gone.

If I change \index to \nothing and have \nothing insert an empty hbox by defining it \def \nothing #1{\hbox{}}, the problem is back again. So I guess \index inserts something that makes LaTeX no longer ignore the newline character.

Is there a way to prevent \index from causing whitespace?

Note that the above is actually the result of some macro expansions, so "just move the \index to the next line" isn't going to help me. Also, I tried wrapping \index in my own macro that inserts a \relax after it, but that didn't work either.

share|improve this question
1  
How about defining \newcommand*{\myindex}[1]{\index{#1}\ignorespaces} and using \myindex in the document? –  lockstep Aug 25 '11 at 9:48
    
Seemingly, since \index is after \\ , you start a paragraph with it. I can't recall exactly, but \index does some funny things with surrounding spaces (see source2e.pdf), and probably this might not work in vertical mode. You might thus try \\\leavevmode\index{...} (or \newcommand\myindex[1]{\leavevmode\index{#1}} or something similar). –  mbork Aug 25 '11 at 13:22
2  
I do not know the makro producing your text, but would 'The Principle of Availability\index{principle of availability}\\' be possible for you? –  Stephen Aug 25 '11 at 17:42
add comment

2 Answers

In latex.ltx, the \index command is (properly) defined whenever you execute \makeindex. Otherwise it virtually does nothing.

Skip over this part if you're not interested in the detail...


The proper definition is

\show\index
> index=macro:
->\@bsphack \begingroup \@sanitize \@wrindex

while

\show\@wrindex
> \@wrindex=macro:
#1->\protected@write \@indexfile {}{\string \indexentry {#1}{\thepage }}\endgroup \@esphack

So, in essence, \index has this structure:

\@bsphack
\begingroup
...
\endgroup
\@esphack

Both \@bsphack and \@esphack act like grouping partners that perform certain operations before and after whatever it surrounds. This only happens while in horizontal mode1, which is the case with your example; \index{...} is placed after \\ and is the first token of a paragraph. \@esphack is suppose to apply \ignorespaces, but this is only the case if the value of \lastskip was greater than 0pt:

\show\@esphack
> \@esphack=macro:
-> \relax
   \ifhmode
     \spacefactor\@savsf
     \ifdim\@savsk>\z@
       \ignorespaces
     \fi
   \fi

Since there is no skip before \index, \lastskip=0pt and \ignorespaces never sees the light of day.

1 Read What are vertical and horizontal modes? for more information on this.


...aaaand, we're back!

If you must use \index, then you could redefine it this way after \makeindex:

\let\oldindex\index
\renewcommand*{\index}[1]{\oldindex{#1}\ignorespaces}

This stores \index in \oldindex and appends it with \ignorespaces, taking care of the spurious spaces immediately after \\ as well as in the additional small case described above.

Alternatively, make your own index command

\newcommand*{\myindex}[1]{\index{#1}\ignorespaces}% \myindex{<entry>}

which does the same as above. However, it requires you using \myindex rather than \index.

share|improve this answer
1  
There's no spurious space in lorem ipsum \index{blah} lorem ipsum. On the other hand, it's best to attach \index to the indexed word, so the reference is guaranteed to be correct: the space before \index might trigger a line break and the line might go to the next page (this is a very unfortunate combination of events, but Murphy's law is always valid). –  egreg Aug 26 '11 at 9:25
    
@egreg: You're right. I was fiddling around with different styles to identify where to modify what, and perhaps left a change in that caused the lorem ipsum \index{blah} lorem ipsum spacing problem. I've removed that from my answer. –  Werner Aug 26 '11 at 14:07
    
In the following code: In first approximation, none of the intermediate frequency radiation comes back to the radio-frequency side of the mixer (this is however possible and is called ``IF feedback\index{IF feedback}'')., redefining index (or defining myindex) to include \ignorewhitespaces does not remove the extra space before the closing double-quote. –  Niriel Jan 31 '12 at 9:49
add comment

that is the default behaviour, use it always this way:

\noindent The Principle of Availability\\
    
What\index{principle of availability} is said must be intuitively ... 
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.