# Using indexing packages with memoir

Friends, as you probably know, I have a quite complex document for cataloging songs. :)

As my normal workflow, I use the book document class with some dozens of packages loaded. I've been playing with memoir in other documents for a while and obtained very nice results. The idea of a "batteries-included" class is tempting and some of its features make much of my stuff very easy to achieve.

So everything was settled for migration, except for one thing: indices.

This particular document of mine requires several indices, up to 30. As we know, with a setting this crazy TeX can run out of registers in a blink of an eye. I've been using either splitidx or imakeidx to output the indices in only one big .idx file and then use the splitindex command line app to, well, split indices. :)

memoir has a built-in indexing support. As far as I went in the documentation, there's no mention of a limitation of number of indices, but it seems it's implied there, since we also define the index extension. I could not find any references on relying on an external indexing package, and it's known that imakeidx is not compatible with memoir. Sob! Edit: From version 1.1 on, imakeidx now supports memoir, yay! :)

I beseech for your wisdom, o memoir power users: how can I use a indexing package with memoir or, if possible, make it provide support for 16+ indices? :)

Edit: Terribly sorry, I should've made a M(n)WE. :)

\documentclass{memoir}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\makeindex[aaa]
\makeindex[bbb]
\makeindex[ccc]
\makeindex[ddd]
\makeindex[eee]
\makeindex[fff]
\makeindex[ggg]
\makeindex[hhh]
\makeindex[iii]
\makeindex[jjj]
\makeindex[kkk]
\makeindex[lll]
\makeindex[mmm]
\makeindex[nnn]
\makeindex[ooo]
\makeindex[ppp]
\makeindex[qqq]
\makeindex[rrr]
\makeindex[sss]
\makeindex[ttt]
\makeindex[uuu]
\makeindex[vvv]
\makeindex[www]
\makeindex[xxx]
\makeindex[yyy]
\makeindex[zzz]

\begin{document}

Hello world.

\end{document}


which blows up the evil

Writing index file lll.idx
! No room for a new \write .
\alloc@ ...else \errmessage {No room for a new #2}
\fi \fi
l.49 \makeindex[mmm]

?

-
I couldn't see anything in the memoir manual to suggest there was a built-in limit. I think you define the index filenames, rather than extensions (all .idx). Of course, registers may be a problem, but "have you tried it"? – Brent.Longborough May 8 '12 at 20:06
@Brent: I'm terribly sorry, I should have made a MWE, now added to the question. Sadly the registers blew up. :( – Paulo Cereda May 8 '12 at 20:19
hmm, there IS a limit as the \makeindex open a new write each time it is used, and the number of simultaneous writes is limited. But the idx files are actually first populated at end document (AFAIR). But (again AFAIR) the aux is only executed once at the end of the document, so one would in any case need too many simultaneous writes. One solution might be to change some internals, write all index entries into the main aux, write a tool to extract these lines from the main aux, and then process those files. It is doable. – daleif May 8 '12 at 20:47

Version 1.1 of imakeidx, just uploaded to CTAN allows to define as many indices as you want:

\documentclass{memoir}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[splitindex]{imakeidx}[2012/05/09]

\makeindex[name=aaa]
\makeindex[name=bbb]
\makeindex[name=ccc]
\makeindex[name=ddd]
\makeindex[name=eee]
\makeindex[name=fff]
\makeindex[name=ggg]
\makeindex[name=hhh]
\makeindex[name=iii]
\makeindex[name=jjj]
\makeindex[name=kkk]
\makeindex[name=lll]
\makeindex[name=mmm]
\makeindex[name=nnn]
\makeindex[name=ooo]
\makeindex[name=ppp]
\makeindex[name=qqq]
\makeindex[name=rrr]
\makeindex[name=sss]
\makeindex[name=ttt]
\makeindex[name=uuu]
\makeindex[name=vvv]
\makeindex[name=www]
\makeindex[name=xxx]
\makeindex[name=yyy]
\makeindex[name=zzz]

\begin{document}

Hello world.

\index[aaa]{A}
\index[bbb]{B}
\index[ccc]{C}
\index[ddd]{D}
\index[eee]{E}
\index[fff]{F}
\index[ggg]{G}
\index[hhh]{H}
\index[iii]{I}
\index[jjj]{J}
\index[kkk]{K}
\index[lll]{L}
\index[mmm]{M}
\index[nnn]{N}
\index[ooo]{O}
\index[ppp]{P}
\index[qqq]{Q}
\index[rrr]{R}
\index[sss]{S}
\index[ttt]{T}
\index[uuu]{U}
\index[vvv]{V}
\index[www]{W}
\index[xxx]{X}
\index[yyy]{Y}
\index[zzz]{Z}

\printindex[aaa]
\printindex[bbb]
\printindex[ccc]
\printindex[ddd]
\printindex[eee]
\printindex[fff]
\printindex[ggg]
\printindex[hhh]
\printindex[iii]
\printindex[jjj]
\printindex[kkk]
\printindex[lll]
\printindex[mmm]
\printindex[nnn]
\printindex[ooo]
\printindex[ppp]
\printindex[qqq]
\printindex[rrr]
\printindex[sss]
\printindex[ttt]
\printindex[uuu]
\printindex[vvv]
\printindex[www]
\printindex[xxx]
\printindex[yyy]
\printindex[zzz]

\end{document}

-
Absolutely brilliant! A tear of joy just fell off my eyes. Grazie mille, egreg! :) – Paulo Cereda May 9 '12 at 13:24

This is in fact the first time I've used memoir so this isn't exactly tested but this appears to work.

Arrange that all the index entries get written to the same .idx file but using variant \indexentry commands.

Run makeindex once for each desired index outputting to the separate .ind files each time specifying the appropriate keyword to use for that index (ignore the warnings about all the other index entries)

The printing of the indices is unaffected as any number of .ind files can be input.

so: id1.tex

\documentclass{memoir}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\makeatletter
\renewcommand*{\makeindex}[1][\jobname]{%
\if@filesw
\def\index{\@bsphack%
\@ifnextchar [{\@index}{\@index[\jobname]}}
\def\specialindex{\@bsphack\@spindex}%
\expandafter\let\csname #1@idxfile\endcsname\@empty
\makememindexhook
\mkindexonce
\fi}

\def\mkindexonce{%
\expandafter\newwrite\csname \jobname @idxfile\endcsname
\expandafter\immediate\openout \csname \jobname @idxfile\endcsname \jobname.idx\relax
\typeout{Writing index file \jobname.idx }%
\let\mkindexonce\relax}

\renewcommand{\@@wrindexm@m}[1]{\begingroup
\def\@idxfile{\@nameuse{#1@idxfile}}%
\def\@idxfil@{#1}%
\@sanitize
\@@@wrindexm@m}

\AtBeginDocument{%
\def\@@@wrindexm@m#1#2{%
\if@filesw
\immediate\write\csname \jobname @idxfile\endcsname{\string\indexentry\@idxfil@{#1}{#2}}%
\fi
\endgroup}%
}

\makeatother
\makeindex[aaa]
\makeindex[bbb]
\makeindex[ccc]
\makeindex[ddd]
\makeindex[eee]
\makeindex[fff]
\makeindex[ggg]
\makeindex[hhh]
\makeindex[iii]
\makeindex[jjj]
\makeindex[kkk]
\makeindex[lll]
\makeindex[mmm]
\makeindex[nnn]
\makeindex[ooo]
\makeindex[ppp]
\makeindex[qqq]
\makeindex[rrr]
\makeindex[sss]
\makeindex[ttt]
\makeindex[uuu]
\makeindex[vvv]
\makeindex[www]
\makeindex[xxx]
\makeindex[yyy]
\makeindex[zzz]

%
\begin{document}
\makeatletter

Hello world.\index[aaa]{one}\index[bbb]{tkwo}

\printindex[aaa]
\printindex[bbb]
\end{document}


If you process that you get a id1.idx that looks like:

\indexentryaaa{one}{1}
\indexentrybbb{tkwo}{1}


so now you need

aaa.is

 keyword "\\indexentryaaa"


and

bbb.is

keyword "\\indexentrybbb"


then you can produce the two separate indices with

makeindex -s aaa.is  -o aaa.ind id1
makeindex -s bbb.is  -o bbb.ind id1


which produces

aaa.ind

\begin{theindex}

\item one, 1

\end{theindex}


and

bbb.ind

\begin{theindex}

\item tkwo, 1

\end{theindex}


which get input at the end. As is they bot get the heading "The Index" but you could change that in either the tex (redefining theindex) or in the makeindex styles (writing a different prefix than the default \begin{theindex} or presumably since memoir supports multiple indices it has a better way of setting the headings:-)

-
Wow, David! This answer is superb. :) Thank you very much for teaching me the ways of the TeX force! :) – Paulo Cereda May 9 '12 at 13:18

Maybe not a direct answer to your question, but a work-around: I suggest you try Markus Kohm’s scrwfile. The package is design to provide a way around the limited number of file handles in LaTeX. It is included in his excellent KOMA-Script-bundle.

The author has started the work on improving the English manual. He describes scrwfile on pages 259ff. You will find KOMA-Script at CTAN and BerliOS.

Also, have a look at the other answers to the similar question some weeks ago:

-
Small mistake: www,komascript.de instead of www.komascript.de – Speravir May 8 '12 at 22:41
@Speravir Fixed. Thank you! – Sveinung May 9 '12 at 12:40
Thank you very much for mentioning KOMA-Script. :) I'm eager to try it too, as it seems as powerful as memoir. Also, the morewrites package from Bruno Le Floch seems also to solve the index issue. Thanks! – Paulo Cereda May 9 '12 at 13:14