# Index at the beginning of a document

Is it possible to insert an index at the beginning of a document, like so?:

\documentclass[11pt]{article}
\usepackage{imakeidx}
%\indexsetup{othercode=\small}
\makeindex[name=definitions,title=Definitions,columns=1,intoc=true,program=makeindex,options={-s index_style.ist}]

\begin{document}
\printindex[definitions]

bla bla \index[definitions]{bla}, lorem ipsum \index[definitions]{ipsum}

\end{document}


FYI the content of index_style.ist is:

headings_flag 1
delim_0 " \\dotfill "
delim_1 " \\dotfill "
delim_2 " \\dotfill "

• You can't have the automatically generated index at the beginning; for this you need to pass the noautomatic option and run makeindex manually. – egreg Oct 7 '13 at 15:56
• Is there a way to automate the process? – idle Oct 7 '13 at 16:00
• With arara, certainly. – egreg Oct 7 '13 at 16:41

You have to call imakeidx with the nonewpage option and run makeindex manually.

Unfortunately, there's also the need to apply a patch around a bug that should be solved in the next release of imakeidx; see Why does imakeidx + nonewpage fail to write an idx file?

In order to automate the work you have two roads:

1. Use latexmk

2. Use arara

I'd prefer the latter, as it's more easily customizable.

Example (thanks to Paulo Cereda, arara's author, for suggesting the files trick).

% arara: pdflatex
% arara: makeindex: { files: [definitions], style: index_style.ist}
% arara: pdflatex
\documentclass[11pt]{article}
\usepackage[nonewpage]{imakeidx}

% work around the bug. :(
\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
%%% end of bug fix

%\indexsetup{othercode=\small}
\makeindex[name=definitions,
title=Definitions,
columns=1,
intoc=true,
program=makeindex,
options={-s index_style.ist}]

\begin{document}
\printindex[definitions]

bla bla \index[definitions]{bla}, lorem ipsum \index[definitions]{ipsum}

\end{document}


If you run arara -v filename, you'll get this output.

There is a reason for the standard imakeidx automation not being possible in this case: in order to run makeindex, the .idx file(s) must be closed and contain all the required entries, which is not possible if the indices are not at the end of the document. The .idx files written during the previous run are not available any more, because the \makeindex declaration opens them (emptying them as a consequence) at begin document.

Also

latexmk filename


should work out of the box (with the patch).

• When will the patch be available on CTAN? – idle Oct 8 '13 at 18:36
• @idle Soon, sure. – egreg Oct 8 '13 at 19:44