# error when generating index

The .ilg file is fulll of errors, and no \index command is accepted.

This is makeindex, version 2.15 [TeX Live 2011] (kpathsea + Thai support).
Scanning input file thesis.idx...
!! Input index error (file = thesis.idx, line = 1):
-- Illegal page number \thepage.
...
-- Illegal page number \thepage.
!! Input index error (file = thesis.idx, line = 28):
-- Illegal page number \thepage.
done (0 entries accepted, 28 rejected).
Nothing written in thesis.ind.
Transcript written in thesis.ilg.


There is issued \makeindex before document, and the \printindex command is redefined (and issued at the end after \bibliography).

\let\my@prnt@nd@x\printindex
\renewcommandx\printindex{%
\cleardoublepage%
\my@prnt@nd@x%
}


The .idx file has these lines (among others):

...
\indexentry{oscila\textcommabelow tie,~ecua\textcommabelow tie}{\thepage }
\indexentry{natural~numbers}{\thepage }
\indexentry{category,!balanced~definition}{\thepage }
...


For some reasons, in the class .dtx, i have redefined \protected@write which seems to be used by \@wrindex.

% \begin{macro}{\protected@write}
% \begin{macro}{\protected@write@immediate}
%    \begin{macrocode}
% redefine \protected@write to use \protected@write@immediate below
\long\def\protected@write#1#2#3{\protected@write@immediate#1{#3}[#2]}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Code borrowed from multitoc.dtx' CTAN /macros/latex/contrib/ms %%
% \write with appropriate handling of |\protect| and |\thepage|; args:
% an output stream, some text to write, and some initialization code.
\newcommandx*\protected@write@immediate[3][3]{%
\begingroup
\let\thepage\relax
#3%
\let\protect\string
\edef\reserved@a{\immediate\write#1{#2}}%
\reserved@a
\endgroup
\if@nobreak\ifvmode\nobreak\fi\fi%
}
%%%                                                                 %%
%%%                                                                 %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}


There is only a blank page after the bibilography, and usually i had all index entries in the following page, but nothing is generated; appreciate any help or fix on this. Thank s.

-
It's a really bad idea to redefine \protected@write to begin with. Of course, if you declare \let\thepage\relax, \thepage becomes unexpandable and will be written literally in the file. It's quite unclear why you want to make all writes \immediate: it's a plainly wrong thing to do. –  egreg May 31 '12 at 15:58
that's it, a simple backup/restore \TeXnique has resolved it. –  sjbiaga May 31 '12 at 18:22

There should be very good reasons why you want to make all writes \immediate. It's usually a wrong thing to do, for many reasons. Here's one.

Suppose you want to produce a table of contents, which is a quite common thing in LaTeX documents. Suppose your depth of listing in the TOC is at least 1, so that sections are included. Suppose that a section title is found when TeX hasn't yet finished a page, but when it completes its tasks connected with this action, it realizes that the section title must be moved to the next page.

Here is the problem: the entry for the TOC in the auxiliary file will already have been written. And it will be wrong, off by one.

With a delayed write, the entry would be written only when the section title actually appears in a page. Therefore, doing all writes \immediate is plainly wrong; the same situation would appear for the index, as TeX might find an entry to index in a line that will eventually end up in the next page.

If you read the documentation of multitoc, you'll see that the code for \immediateaddtocontents (that uses \protected@write@immediate) was for adding something without the page number. That's why Martin Schröder does \let\thepage\relax: the token \thepage becomes unexpandable and will be written literally to an auxiliary file (and successively used macros will gobble it).

For index production it must be expandable, otherwise you'll get (as you indeed do) \thepage in the .idx file, which is pretty useless: you're losing the association between the entry and the page number. But it's not sufficient to remove that instruction: the write operation must not be immediate.

By the way, there is a much simpler way to make all write operations immediate:

\let\@@write\write
\def\write{\immediate\@@write}


(a double \immediate` prefix is equivalent to one).

-