# imakeidx - grouping under italic term

I am using the imakeidx package to create an index. I want my index to be completely in abecedarian sequence, so I use the @-command for terms that are in math-mode or italic.

However, if I now want to group some items of the index (with the !-command) under an item in italic, something goes wrong. The first level item appears twince in the index, once normal and once in italic. And the second level items are group under the non-italic item.

The desired output of the index file for Term2 (see MWE below) is:

  \item \textit{Term2}, 1
\subitem -A, 1
\subitem -B, 1


Does anyone have an idea how to fix that (without doing it manually)? Thank you in advance!

MWE:

\documentclass{scrreprt}

\usepackage{imakeidx}
\makeindex[columns=1]

\begin{document}

% Works as desired without \textit
Term1 \index{Term1}
Term1-A \index{Term1!-A}
Term1-B \index{Term1!-B}

% Does not work as desired with \textit
\textit{Term2} \index{Term2@\textit{Term2}}
\textit{Term2}-A \index{Term2!-A@\textit{Term2}-A}
\textit{Term2}-B \index{Term2!-B@\textit{Term2}-B}

% Does also not work
\textit{Term3} \index{Term3@\textit{Term3}}
\textit{Term3}-A \index{Term3-A@\textit{Term3}!-A}
\textit{Term3}-B \index{Term3-B@\textit{Term3}!-B}

\printindex
\end{document}


Created index file:

\begin{theindex}

\item Term1, 1
\subitem -A, 1
\subitem -B, 1
\item Term2
\subitem \textit{Term2}-A, 1
\subitem \textit{Term2}-B, 1
\item \textit{Term2}, 1
\item \textit{Term3}, 1
\item \textit{Term3}
\subitem -A, 1
\item \textit{Term3}
\subitem -B, 1

\end{theindex}


You have the actual part in the wrong level within \index.

The top level is \index{Term2@\textit{Term2}} so the next level down is \index{Term2@\textit{Term2}!sublevel}. The sublevel can also be in the form sub-sort@sub-text but in this case there's no need for the sub-sort@ part.

\documentclass{scrreprt}

\usepackage{imakeidx}
\makeindex[columns=1]

\begin{document}

% Works as desired without \textit
Term1 \index{Term1}
Term1-A \index{Term1!-A}
Term1-B \index{Term1!-B}

% Works as desired with \textit
\textit{Term2} \index{Term2@\textit{Term2}}
\textit{Term2}-A \index{Term2@\textit{Term2}!-A}
\textit{Term2}-B \index{Term2@\textit{Term2}!-B}

\printindex

\end{document}


Another way to deal with this problem is to control how the indexes are formatted with an index style file (an .ist file). With an .ist file you can dictate which style guide goes with which index too, so you can have lots of formatted variations in one document spread out over a bunch of indexes.

To make an .ist file you make a .txt file and change its name in Finder/Explorer (is it still called this on Windows?) into .ist. So you change the .txt file type into an .ist file type. The .ist should be openable in NotePad or TextEdit or what have you.

When XeLaTex or whatever goes to format your indexes, it turns to your .ist file as a style guide to do it. This way you can dictate that all first level items are italic, all second level items are bold, all third level items are normal, etc., whatever you want! It takes the raw info of the .idx file, interpolates it with the style formatting code you specify in the .ist (putting each item before, in between, and after whatever you specify) and then churns out working code in the .ind file.

I include two examples, one simpler, one more complex. Include your .ist files in the folder you have your .tex file.

Tex file:

\documentclass[a4paper]{book}
\usepackage{imakeidx}
\makeindex[name=itindexplain,title=Plain Index Title, options = -s itindexplain.ist]
\makeindex[name=itindex,title=Example Index Title, options = -s itindex.ist]
\makeindex
\begin{document}
This is a test.
\index[itindexplain]{Item 0!Item 1!Item 2}
\index[itindexplain]{example!this .ist code requires a sub-item to work, since it closes the textit environment with a second curly bracket}
\index[itindex]{first test}
\index[itindex]{Item 0!Item 1!Item 2}
\printindex[itindexplain]
\printindex[itindex]
\end{document}


The contents of the itindexplain.ist file:

item_0 "\\indexspace \\vspace{-12pt} \n \\textit{\\item "
item_x1 "} \n \\subitem "
item_1 " \n \\subitem "

delim_0 " \\hfill "
delim_1 " \\hfill "
delim_2 " \\hfill "


And the contents of the itindex.ist file:

item_0 "\\indexspace \n \\item \\noindent \\vspace{8pt}"
item_x1 "\n \\subitem \\hspace{-.8cm} \\textit{"
item_1 "\\vspace{8pt} \n \\subitem \\hspace{-.8cm} \\textit{"
item_x2 "\n} \\subitem \\hspace{-.725cm}"
item_2 "\n \\subitem \\hspace{-.725cm}"

delim_0 "\\hfill"
delim_1 "\\hfill"
delim_2 "\\hfill"


Two important notes:

• Make sure your .ist file editor (notepad, textedit, whatever) doesn't change straight quotes into curly quotes automatically. This only works with straight quotes.
• Here's a website that lists the keys for playing around with .ist files.