4

In my textbook, I want to include both an index and a glossary. For the glossary, for which I'm currently using the glossaries package, I would prefer to have pointers (and hyperlinks) that don't go straight to one or more occurrences in the main text, but instead point to a single index entry (typically for the same term).

Maybe a sketch is in order.

Conventional scenario:

Index 
    Combustion engine -> page 23, page 56, ...

Glossary 
    Combustion engine: an endangered species -> page 23, page 56, ...

What I would like instead:

Index 
    Combustion engine -> page 23, page 56, ...

Glossary 
    Combustion engine: an endangered species. -> (page of) index entry 
                                                 on combustion engine

And while we are at it, we might as well also generate a pointer to the glossary entry within the index item.

Motivation:

  1. No cluttering up the glossary with potentially long lists of page references that are already maintained in the index.

  2. I usually give fairly extensive explanations in the glossary entries themselves, and the main text typically does not add much to those; thus, I don't want to frustrate the reader by inviting him to sift through the main text in search of detail that isn't there.

  3. While writing, I don't want to think about questions like: should this occurrence go into the index only, the glossary only, or both? Thinking about what to include in the index is distraction enough.

PS: Before commenting on the above sample text, note that the book is about neither engines nor endangered species.

3

Here is what I came up with: I ditched the glossaries package and instead implemented a couple of macros based directly on hyperref. The index is generated as usual by makeindex, but I use a few tricks in order to let it generate the hyperlinks and -targets to match the ones created in the glossary. The glossary can simply be written as a description environment (although in real life I would use the enumitem package to customize it).

% arara: pdflatex
% arara: makeindex
% arara: pdflatex
% arara: pdflatex

% we need two pdflatex runs after makeindex for this to work. 
% arara is great for this kind of per-document build configuration. 

\documentclass[12pt]{article}

\usepackage[margin=1in,papersize={6in,8in}]{geometry}

% create a custom style file for makeindex that wraps the standard 
% index item commands such that we can get hold of their text contents
% 
% e.g:
%
% \inx{drugs}
%     \subinx{lithium}\hyperpage{2}, \hyperindexformat{\glsitem}{3}
%     \subinx{molecular size}\hyperpage{1}

\begin{filecontents*}{\jobname.mst}
line_max        2048

item_0 "\n\\inx{"
item_1 "\n    \\subinx{"
item_01 "\n    \\subinx{"
item_x1 "}\n    \\subinx{"
item_2 "\n        \\subsubinx{"
item_12 "\n        \\subsubinx{"
item_x2 "}\n        \\subsubinx{"

delim_0 "}"
delim_1 "}"
delim_2 "}"
\end{filecontents*}

% load and run makeindex
\usepackage{makeidx}
\makeindex

% we use hyperref to implement the reciprocal links between glossary and index
\usepackage[colorlinks,linkcolor=blue]{hyperref}

% implement the commands

% raise hypertargets by one line to get them into the viewport
\newcommand*{\raisedtarget}[1]{\raisebox{\ht\strutbox}{\hypertarget{#1}{}}}

% a glossary entry. Mandatory arguments are term and definition. 
% The optional argument becomes a prefix for the index key. 
\newcommand{\glsentry}[3][]{%
    \edef\glskey{#1#2|glsitem}%
    \expandafter\index\expandafter{\glskey}%
    \item [#2] \raisedtarget{gls-#2} #3 \hyperlink{inx-#2}{\pageref*{inx-#2}\emph{i}}
}

% wrap the index \item and \subitem commands to store the current index term
\newcommand*{\inx}[1]{%
    \global\edef\currentinx{#1}%
    \item #1 
}

\newcommand*{\subinx}[1]{%
    \global\edef\currentinx{#1}%
    \subitem #1 
}

\newcommand*{\subsubinx}[1]{%
    \global\edef\currentinx{#1}%
    \subsubitem #1 
}

% format the index page references generated by glossary entries  
\newcommand{\glsitem}[1]{%
    \label{inx-\currentinx}% we need the label to typeset the index page in the glossary ...
    \raisedtarget{inx-\currentinx}% ... and the explicit hypertarget to make the link work
    \hyperlink{gls-\currentinx}{#1\emph{g}}%
}


\begin{document}

\section{Introduction to pharmacology}

... document text snipped ...    

\section*{Glossary}

\begin{description}

% the [drugs!] prefix makes the link go to a subitem in the index
\glsentry[drugs!]{lithium}{An alkali metal used in batteries that moonlights as an antidepressant}

% no prefix will point the link to a main index item
\glsentry{Paracelsus}{A famous physician from the 16th century who investigated the relationship of drugs and poisons by way of self-experimentation}

\end{description}

\printindex

\end{document}

The glossary looks like this:

enter image description here

The index like this:

enter image description here

| improve this answer | |

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.