TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


\def \nothing #1{}


\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'.


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
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
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

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

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


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

So, in essence, \index has this structure:


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:

> \@esphack=macro:
-> \relax

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:


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
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

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

Your Answer


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.