# Why does imakeidx + nonewpage fail to write an idx file?

Example:

\documentclass{article}

\usepackage[nonewpage]{imakeidx}
\makeindex

\begin{document}

stuff\index{test}

\printindex

\end{document}


When I run lualatex on this file, I see among the output on stdout:

[1{/usr/local/texlive/2013/texmf-var/fonts/map/pdftex/updmap/pdftex.map}
\indexentry{test}{1}
] (./mwe.aux) )


The \indexentry seems to be written to stdout rather than to the .idx file--the .idx file ends up empty, and thus I get no index at all.

If I remove the nonewpage option to imakeidx the .idx file gets built as I expected. Alternatively, adding a \clearpage before \printindex also makes things work as expected.

What's going on here? I was just trying to remove the page break before my index!

FWIW I'm on MacTeX 2013, and I just ran the TeX Live updater yesterday.

I'm afraid it's a bug in imakeidx which calls for closing the idx file too early even if noautomatic (implied by nonewpage) is in force.

For the moment you can give the correct definition to \imki@putindex:

\documentclass{article}

\usepackage[nonewpage]{imakeidx}
\makeatletter
\def\imki@putindex#1{%
\ifimki@nonewpage\else
\imki@clearpage
%%% The following two lines are incorrectly switched in the package
\immediate\closeout\csname #1@idxfile\endcsname
\fi
\let\imki@indexname\indexname % keep \indexname
\@nameuse{imki@set@#1}\imki@decide
\if@tempswa % we can call the external program
\imki@exec{\imki@program\imki@options#1.idx}%
\else
\imki@finalmessage{#1}%
\fi
\ifKV@imki@intoc
\else
\fi
\ifx\imki@title\imki@check@indexname\else
\def\indexname{\imki@title}%
\fi
\@input@{#1.ind}
\let\indexname\imki@indexname % restore \indexname
}
\makeatother
\makeindex

\begin{document}

stuff\index{test}

\printindex

\end{document}


A fixed version of the package is on its way to CTAN.

• @dsedivec Sorry for the bug. ;-) – egreg Sep 13 '13 at 20:56
• Seems as if this bug is back in TeXLive 2019. – Peter Grill Sep 20 '19 at 5:48
• I updated all packages and problem still remains. The workaround in the answer no longer solves the problem, nor does adding a \clearpage (as mentioned in the question) before \printindex. – Peter Grill Sep 20 '19 at 6:09