# Creating an index with sub-sub-subentries

For my PhD thesis I need to make a list of works quoted. I thought that using the LaTeX index would be a good idea. But unfortunately I cannot have more than 3 levels in the index. BTW I am using the memoir class and xelatex.

An example of an index could be:

Christian Writings
Origen
Homilies on Joshua
1.1, 5
2.2, 6
Homilies on Numbers
1.1, 7
New Testament
Matthew
1.1, 8
John
2.2, 9
Shepherd of Hermas
1.1, 8
Jewish Writings
Old Testament
Genesis
1.1, 11


I had to type it in code mode to help see the structure. Here is a minimal example:

\documentclass[12pt]{memoir}
\makeindex

\begin{document}

Lorem ipsum
\index{Christian Writings!Origen!Homilies!1.1}
\index{Christian Writings!New Testament!Matthew!1.1}
\index{Christian Writings!Shepherd of Hermas!1.1}

\printindex
\end{document}


This only outputs the third index entry (as the other two have too many '!'s).

Can anyone help?

-
Only 3 index levels are allowed by LaTeX and makeindex, according to makeindex documentation – Scribblemacher Nov 23 '11 at 18:51
Yeah, I know. That's the problem. – Tom de Bruin Nov 23 '11 at 18:58
Xindy is an index processor, more advanced than makeindex, that allows for more than three index levels. You have to create a xindy style file though. Herbert shows an example in this post on the Xindy mailing list (note also the last post). I'm not posting this as an answer as I couldn't actually make it work myself (having some problems running Xindy in Windows ...). – Torbjørn T. Nov 23 '11 at 19:34
If there are only a few main entries (Christian Writings, Jewish Writings, ...) in your index, you could typeset several indexes instead (using, e.g., the splitindex package). – lockstep Nov 23 '11 at 22:30

xindy is an index processor, more powerful than makeindex, that has full Unicode support, and allows many levels within an index.

But as no more than three levels is defined by default, it won't work "out of the box": you have to define your own xindy style file, and redefine the index environment. I'm stealing some code from Herbert, which he posted at the xindy mailing list, and adapting it to memoir.

# The style file

You can create a new file called e.g. style1.xdy, containing the following line:

(markup-indexentry :open "~n      \subIIIitem " :depth 3)


and place it in the same folder as your document. In the example below, as in Herberts code, this is done by the filecontents* environment. I suppose placing the file in one of the folders where xindys style files are found, will also work, e.g. texmf/xindy/modules/styles/.

# The document

Compile with:

xelatex file.tex
texindy -M style1.xdy file.idx
xelatex file.tex


\documentclass[12pt]{memoir}
\usepackage{filecontents}
\begin{filecontents*}{style1.xdy}
(markup-indexentry :open "~n      \subIIIitem " :depth 3)
\end{filecontents*}
\makeatletter
\newcommand\subIIIitem{\@idxitem \hspace*{40\p@}}
\renewenvironment{theindex}{%
\clearforchapter
\if@twocolumn
\@restonecolfalse
\else
\@restonecoltrue
\fi
\ifonecolindex
\onecolumn
\chapter*{\indexname}
\preindexhook
\else
\setlength{\columnseprule}{\indexrule}%
\setlength{\columnsep}{\indexcolsep}%
\preindexhook]%
\fi
\indexmark
\ifnoindexintoc\else
\phantomsection
\fi
\thispagestyle{indextitlepagestyle}\parindent\z@
\parskip\z@ \@plus .3\p@\relax
\let\item\@idxitem%
\raggedright\small\let\item\@idxitem \parskip .0pt\@plus .3\p@{}}
\makeatother

\usepackage{makeidx}\makeindex

\begin{document}
Lorem ipsum.
\index{Christian Writings!Origen!Homilies!1.1}
\index{Christian Writings!New Testament!Matthew!1.1}
\index{Christian Writings!Shepherd of Hermas!1.1}

\printindex
\end{document}


This produces:

-
What I enjoy about @ Herbert's code is his inclusion of the xindy style/configuration file style1.xdy as part of the source code using filecontents. This allows for portability of the code without missing out on some straggler file and its very important contents when moving code around. – Werner Nov 24 '11 at 7:58
@Werner Again (I think you told me this once before ...) good point. I think I had some weird and insufficient reason for not including it, but never mind that. I'll change my answer. – Torbjørn T. Nov 24 '11 at 8:18
I forget who I've told what, so forgive me. Don't want to nag about it... :) – Werner Nov 24 '11 at 8:22
@Werner No problem, I obviously could use the reminder. – Torbjørn T. Nov 24 '11 at 8:23

Since nobody (yet?) proposed a comprehensive solution, I'd like to suggest a workaround, that uses only up to two levels of index. A side benefit is that such indices, imho, are more readable than the multilevel ones:

\documentclass[12pt]{memoir}
\makeindex

\begin{document}

Lorem ipsum
\index{Christian Writings|seealso{Origen,
New Testament, Shepherd of Hermas}}
\index{Origen!Homilies!1.1}
\index{New Testament!Matthew!1.1}
\index{Shepherd of Hermas!1.1}

\printindex
\end{document}


-
As there are about 200 Christian writings, that is not going to work very well... – Tom de Bruin Nov 24 '11 at 2:08

Looking for a solution to a similar question, I decided to create an empty first level entry—in your case, "Christian Writings" (without page numbers) followed by entries at the same level "Origen", etc. This solution works in imakeidx but probably in makeindex too:

\index{010Christian Writings@\idxsechead{Christian Writings}|idxnopage}
\index{01Origen@Origen}
\index{010Ignatius@Ignatius}


The numbers in the sort-entry ensure their grouping with "Christian Writings" coming up first. For details about the solution, which the folks at this forum suggested to me, see How to add an entry to theindex at item level without page number?

-