# How can I automatically add index entries every time I reference a glossary entry?

I'm making heavy use of the glossaries package, and I have created a custom glossary with a type I call concepts.

I am trying to come up with a single command that both links to the concepts glossary and to my index. I have seen a few quick 'n' dirty ways to do this for \ac (and I could use the same approach for \gls), but I'd like to see a more robust approach -- one that affects the whole family of \gls commands (e.g. \glspl, \Gls, etc.).

Is it possible to do this without renewing each of those commands individually, to avoid repetition of code, or would that be required for this task?

I also want the whole set of glossary-references to link to the \name key of the glossary entry to avoid redundant glossary entries. The following should link to the same glossary entry (star), should all show up as unique hits for a single entry in the index, but should display different text according to which glossary reference command is used. That is, I want them to fall under the same index entry rather than one index entry called "star", another called "Stars", and yet another called "stellar". (Does that make sense?)

\mygls{star} should look like star'' and link to the glossary entry {star} and index entry {Star}.
\myGls{star} should look like Star''.
\myglspl{star} should look like stars''.
\myGlspl{star} should look like Stars''.
\myglslink{star}{stellar} should look like stellar''.


I recognize that this question is similar in spirit to other questions asked, but with a few differences that together make my question unique:

• I need this to be done for all glossary references in the text of my document -- not just a single index entry linking to the glossary entry itself. (Compare to this question.)
• I want more than just \gls overloaded: it should overload the other glossary reference commands as well. (Compare to the solution to this question.)
• I want to do this for glossary entries of a specific glossary -- not just the default acronym glossary. (Compare to this question.)

-
The glossaries package comes with a sample file called sample-index.tex that seems to do what you want. If you want it modified, add it as a MWE to your question and indicate how you want it changed. –  Nicola Talbot Dec 3 '13 at 9:27

Given that this question has been open for several months, perhaps you've already solved your problem, but I have written some macros to solve a virtually identical problem, so I thought I'd share them.

Other solutions to this type of problem use the label (or, as you requested) the name key to create the index. However there are times when you will almost certainly want the indexed entry to be different from the text form, for example if you want subitems along the lines of \index{category!subcategory}.

We can, however, take advantage of the extra keys available with a glossary entry and store the form we want indexed there. First, I define a command for a new key. This is just a convenience. It maps onto the predefined user1 key:

\newcommand*{\indexkey}{user1}


Then I create an extended glossary entry command. It behaves almost exactly like \newglossaryentry except that you can omit the name key (in which case it will default to label. By default it will also define the \indexkey field to also be label. If we want it to be anything else, we just include our index text in the definition:

\newcommand{\xglossaryentry}[2]{%
\newglossaryentry{#1}{%
name={#1},%
\indexkey={#1},#2}%
}


Then we create commands to create the gloss and index entry at the same time. I know you asked about renewing the gls-series commands, but I don't think that's the best approach. I find that I occasionally want unindexed references to the glossary, so instead I just defined new commands. To replace the basic \gls command, we get this:

\newcommand{\xgls}[2][]{\index{\glsentryuseri{#2}}\gls[#1]{#2}}


When you want an indexed glossary reference, invoke this version. It will expand the \indexkey string appropriately so that makeidx will treat it correctly. Otherwise, just use the plain \gls.

All the other commands are defined similarly. (See the MWE below.)

And here's the equivalent definition to create new acronym entries. Here, if you want to customize the index, you need to pass it as an option:

\newcommand{\xacronymentry}[4][]{%
\newacronym[\indexkey={#2},#1]{#2}{#3}{#4}%
}


To get glossary entries in a custom glossary, you don't need to do much else. Just define the glossary before you invoke \makeglossaries and add a type key to the relevant glossary entries.

Finally, your example list included a derivationally related form (the adjective stellar). If you have a recent version of glossaries, you can use \glsaddkey to define a custom form (with its associated capitalization variations) so that the glossary can show the noun and you can generate the adjective as needed in your text.

Here's an MWE to illustrate everything (edited to incorporate Nicola's suggestion):

\documentclass{article}
\usepackage{makeidx}
\usepackage{glossaries}

\makeindex

\newglossary{concept}{cps}{cpo}{Concepts}

\makeglossaries

\newcommand*{\indexkey}{user1}

\newcommand{\xglossaryentry}[2]{%
\newglossaryentry{#1}{%
name={#1},%
\indexkey={#1},#2}%
}
\newcommand{\xacronymentry}[4][]{%
\newacronym[\indexkey={#2},#1]{#2}{#3}{#4}%
}

\newcommand{\xgls}[2][]{\index{\glsentryuseri{#2}}\gls[#1]{#2}}
\newcommand{\xglspl}[2][]{\index{\glsentryuseri{#2}}\glspl[#1]{#2}}
\newcommand{\xGls}[2][]{\index{\glsentryuseri{#2}}\Gls[#1]{#2}}
\newcommand{\xGLS}[2][]{\index{\glsentryuseri{#2}}\GLS[#1]{#2}}
\newcommand{\xGlspl}[2][]{\index{\glsentryuseri{#2}}\Glspl[#1]{#2}}
\newcommand{\xGLSpl}[2][]{\index{\glsentryuseri{#2}}\GLSpl[#1]{#2}}

{\glsentrytext{\glslabel}}% default value

\xglossaryentry{basic}
{
name={basic entry},
plural={basic entries},
description={Here is a sample glossary entry},
\indexkey={entry!\emph{basic}|textbf}
}

\xglossaryentry{star}
{
type={concept},
description={A bit, hot, glowing thingy},
\indexkey={Star},
}

\xacronymentry{IBU}{IBU}{International Bitterness Units}
\xacronymentry[\indexkey={standard reference method}]{SRM}{SRM}{Standard Reference Mehtod}

\begin{document}

Ordinary form: \xgls{basic}.
Plural form: \xglspl{basic}.
Initial capital: \xGls{basic}.
Initial capital plural: \xGlspl{basic}.
All caps: \xGLS{basic}.
All caps plural: \xGLSpl{basic}

Twinkle, twinkle, little \xgls{star}.
Plural: \xglspl{star}.
Initial capital: \xGls{star}.
Initial capital plural: \xGlspl{star}.
All caps: \xGLS{star}
All caps plural: \xGLS{star}

The \xgls{SRM} measures the color of beer.

Flying Monkeys Alpha Fornication has an insanely high \xgls{IBU}.

\clearpage

Another reference to \xgls{basic}.

Imperial stouts have \xglspl{SRM} over 40.

Second reference to \xgls{IBU}.

\printglossaries

\printindex

\end{document}

-
It might be better to move \gls to the end of the definition of \xgls (\newcommand{\xgls}[2][]{\index{\glsentryuseri{#2}}\gls[#1]{#2}}) so you can still use the final optional argument of \gls if required. –  Nicola Talbot Mar 3 '14 at 15:43
Excellent point, @Nicola. I've incorporated the fix. –  k.t.hagen Mar 3 '14 at 16:38