3

I'm able to print an index alphabetically. However, if I use \ref in the index entry, the subentries are not in alphabetical order. I'd like Module 3 entry to come after Module 2 entry. How can I tell LaTeX to sort/alphabetize by the chapter number rather than chapter name?

MWE index subentry alphabetical issue

Here's a MWE:

\documentclass[twoside,openright,12pt]{report}
\def\usletterpaper{\usepackage[bottom=1in,hmarginratio=1:1,letterpaper]{geometry}}

\usepackage{makeidx}
\makeindex

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Node Management} \label{chap:NodeMgmt}
Chapter text here.

\section{Learning Check}
\index{Learning Check!Module~\ref{chap:NodeMgmt}}
Question\\
Answer\\

\section{Learning Check Answer}
\index{Learning Check Answers!Module~\ref{chap:NodeMgmt}@Module~\ref{chap:NodeMgmt}}
Question\\
Answer\\

%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{User Management} \label{chap:UserMgmt}
Chapter text here.

\section{Learning Check} 
\index{Learning Check!Module~\ref{chap:UserMgmt}}
Question\\
Answer\\

\section{Learning Check Answer}
\index{Learning Check Answers!Module~\ref{chap:UserMgmt}}
Question\\
Answer\\
Question\\
Answer\\

%%%%%%%%%%%%%%%%%%%%
\chapter{Application Management} \label{chap:AppMgmt}
Chapter text here.

\section{Learning Check}
\index{Learning Check!Module \ref{chap:AppMgmt} @Module~\thechapter}
Question\\
Answer\\

\section{Learning Check Answer}
\index{Learning Check Answers!Module~\ref{chap:AppMgmt}@Module\ref{chap:AppMgmt}}
Question\\
Answer\\

\printindex
\end{document}
1

The problem is that the \refs are not expanded when send to the .idx file. Therefore, the entries are not ordered by the numbers but by the content of \ref, the first one being \ref{chap:AppMgmt}, which will result in Module 3 in the end.

What must be done is to expand the reference before sending it to the .idx file. This can be done with \newcommand{\myindex}[1]{\expandafter\index\expandafter{#1}}. Then changing every \index for \myindex will give the desired output.

enter image description here

\documentclass[twoside,openright,12pt]{report}
\def\usletterpaper{\usepackage[bottom=1in,hmarginratio=1:1,letterpaper]{geometry}}

\usepackage{makeidx}
\makeindex


\newcommand{\myindex}[1]{\expandafter\index\expandafter{#1}}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Node Management} \label{chap:NodeMgmt}
Chapter text here.

\section{Learning Check}
\myindex{Learning Check!Module~\ref{chap:NodeMgmt}}
Question\\
Answer\\

\section{Learning Check Answer}
\myindex{Learning Check Answers!Module~\ref{chap:NodeMgmt}@Module~\ref{chap:NodeMgmt}}
Question\\
Answer\\

%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{User Management} \label{chap:UserMgmt}
Chapter text here.

\section{Learning Check} 
\myindex{Learning Check!Module~\ref{chap:UserMgmt}}
Question\\
Answer\\

\section{Learning Check Answer}
\myindex{Learning Check Answers!Module~\ref{chap:UserMgmt}}
Question\\
Answer\\
Question\\
Answer\\

%%%%%%%%%%%%%%%%%%%%
\chapter{Application Management} \label{chap:AppMgmt}
Chapter text here.

\section{Learning Check}
\myindex{Learning Check!Module~\ref{chap:AppMgmt}@Module~\thechapter}
Question\\
Answer\\

\section{Learning Check Answer}
\myindex{Learning Check Answers!Module~\ref{chap:AppMgmt}@Module~\ref{chap:AppMgmt}}
Question\\
Answer\\

\printindex
\end{document}
1
  • THanks Raoul. I let this one go and just now came back to it. Your logic makes total sense, but the command isn't working for me. Any other ideas?
    – PepPaddy
    Aug 22 '16 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.