[Tex/LaTex] How to add every glossary entry to the index

glossariesimakeidxindexinglinks

I know, there are similar questions around, but I couldn't find a solution to what I want. E.g. here, all acronyms in the document are set to the index. I think, indexing should be done manually so that every index reference shows just 1 or 2 pages in the document.
If the work of a glossary is done, I think this list should also appear in the index. This way, using the index will guide the lector at least to the short glossary explanation.


In detail: I am using imakeidx and glossaries. When I write \gls{} somewhere to set an entry in the main glossary, I don't want an index link to this place, but to the page where the glossary entry is written. Therefore the command \newglossaryentry{glo:AD}{name=Active Directory, description={Active Directory is an...}} should somehow automatically contain an \index{name} resp. \index{Active Directory}.

When I think, the appearance in the text is important enough, I will add manually \gls{glo:AD}\index{Active Directory} and obtain an index entry with two referenced page numbers.

Best Answer

Quick solution

Here is a shorter solution, it struggles with the issue that it writes the first occurence text to the glossary.

 \defglsdisplayfirst[\acronymtype]{#1#4\index{#1}}

It redefines the first display of the term to include a call to index using the \defglsdisplayfirst command and restricts this change to the acronym glossary. Right now the issue with this is that #1 is already defined to long term (acronym), but for normal glossary entries it should be just fine.

A workarround might be to redefine the new acronym command to set the description differently. But in the long run it would be nice to simply have access to the label in the \defglsdisplay(first) command to access the other keys as needed. I wrote a feature request to nicola.

From Nicloas response to the feature request: If one uses \glslabel one can access all the other information. Thus for acronyms use \index{\glsWhatKeyDoYouWantForTheIndex{\glslabel}} instead.

Primitive solution

I had a similar problem and deduce the awnser from that. I redefine the glossary command to call index as some of the awnsers in the related questions suggest, however the call to ìndex`is only made when its the first use.

\documentclass{article}    
\usepackage{makeidx}
\makeindex
\usepackage[acronym]{glossaries}    
 \makeglossaries
 \newacronym{gpu}{GPU}{graphics processing unit}    
 \let\oldGls\gls
 \renewcommand{\gls}[1]{%
    \ifglsused{#1}{ %
        \oldGls{#1}%
    }{%
        \oldGls{#1}%
        \index{\glsentryfirst{#1}}%
    }%
 }
\begin{document}
  %\gls{gpu}
  First use \gls{gpu}-computing\\
  subsequent \gls{gpu}-computing and \gls{gpu}
    \clearpage
    \gls{gpu}

  \printindex
\end{document}

I only covered the \gls-command, and did not check for the glossary the entries are in, this could cause trouble if you have multipleglossaries.

Indexing the entries inside printglossary

By using a custom style for printglossary one can add the index commands.

\documentclass{article} 
\usepackage{makeidx}
\makeindex   
\usepackage{hyperref}
\usepackage{glossaries}    
 \makeglossaries
 \newglossaryentry{gpu}{name=graphics processing unit, description={bla blup}}    

% \defglsdisplayfirst[acronym]{#1#4\index{#1}}
\newglossarystyle{myList}{%
  \glossarystyle{list}%
  \renewcommand*{\glossaryentryfield}[5]{%
    \item[\glsentryitem{##1}\glstarget{##1}{##2}\index{\glsentrytext{##1}}]
       ##3\glspostdescription\space ##5}%
  \renewcommand*{\glossarysubentryfield}[6]{%
    \glssubentryitem{##2}\index{\glsentrytext{##1}%
    \glstarget{##2}{\strut}##4\glspostdescription\space ##6.}%
}}
\begin{document}
  First use \gls{gpu}-computing
  \clearpage
    \printglossary[type=main, style=myList]
  \printindex
\end{document}

Simply go to your texmf tree, take a look at the style you want to use, and copy its definitions of \glossaryentryfield and \glossarysubentryfield out. create your own style be writing (place the index call at a reasonable point, otherwise you could use patch \appendto):

\newglossarystyle{yourStyle}{%
  \glossarystyle{originalStyle}%
  \renewcommand*{\glossaryentryfield}[5]{originalDef+\index{\glsentrytext{##1}}}%
  \renewcommand*{\glossarysubentryfield}[6]{originalDef+\index{\glsentrytext{##1}}}%
}
Related Question