Automatically add index with glossary entries in LaTeX documents

Using the LaTeX glossary package with the index package needs to write two commands to get a term to appear in both, the index list and the glossary. A simple hack may add indexes automatically when using a glossary command.

During writing documents with LaTeX I started using the glossary package at some point. This package allows to use a predefined set of terms including additional information, e.g., description of the term. Sometimes it is convenient to also index the term when used. To do so without the need to write an extra \index command the following hack can be added to the document preamble:

\usepackage{etoolbox}

\makeatletter

\apptocmd{\@gls@}{\shyam@index{#2}}{}{}
\def\shyam@index#1{%
  \ifthenelse{\equal{\csname glo@#1@type\endcsname}{ignored}}{}{%
  \index{#1@\glslink{#1}{\csname glo@#1@name\endcsname}}}%
}
\def\shyam@index@aux#1?#2\shyam@index{#1}

\makeatother

This hack adds an index of the parameter using the standard LaTeX glossary commands, e.g., \gls and displays it using the glossary \glslink command allowing the behavior as if the original glossary command was actually used in the document, e.g., links with the hyperlink package.

Leave a Reply

Your email address will not be published. Required fields are marked *