Applying \lowercase to index entries
I'd say that
\newcommand*{\AddIndexEntry}[2]{%
% #1 = indexed term, #2 = word to index this under
\par\noindent
\lowercase{\def\temp{#2}}%
Indexing: #2%
\expandafter\index\expandafter{\temp!#1}%
}
should be what you need.
How does \lowercase
works? It sends its argument to a further processor (it's not a macro, so it doesn't its work in TeX's “mouth”); the token list is converted using the \lccode
table: each character token that hasn't a zero \lccode
is converted to its lowercase correspondent, but symbolic tokens such as \def
or \temp
are untouched. The token list so obtained is put back in the input as if it had been there from the beginning. There's no expansion during this process: so if TeX finds \lowercase{\def\temp{Xyz}}
when it's executing things, then it "waits" a bit, processes the token list as explained, then it processes
\def\temp{xyz}
and goes along.
In case you have more than one index, you can use a modified form:
\documentclass{article}
\usepackage{imakeidx}
\newcommand*{\AddIndexEntry}[3][]{%
% #1 = indexed term, #2 = word to index this under
\par\noindent
\lowercase{\def\temp{#3}}%
Indexing: #3%
\if!#1!
\expandafter\index\expandafter{\temp!#2}%
\else
\expandafter\indexopt\expandafter{\temp!#2}{#1}
\fi
}
\newcommand{\indexopt}[2]{\index[#2]{#1}}
\makeindex
\makeindex[name=Name,title=Title,columns=1]
\begin{document}
\AddIndexEntry{aardvark}{aardvark}
\AddIndexEntry{Saved by Zero}{Saved}
\AddIndexEntry{Saved by Zero}{Zero}
\AddIndexEntry[Name]{zero}{zero}
\printindex
\printindex[Name]
\end{document}
The \indexopt
macro takes care of switching the arguments, so that the \expandafter
doesn't need to jump over the optional argument to \index
.