# Latex glossaries: Hyperref/Link only the first occurance of an entry in each section?

By default, the glossaries package creates a link to the glossary for each occurrence of an entry (e.g. each time I use \gls{entry}). With the links visualized (e.g. using hyperref colorlinks). This becomes really cluttered if an entry is used many times.

I would rather prefer that only the first occurrence of an entry is hyperlinked. Is this possible?

## migrated from stackoverflow.comApr 16 '13 at 21:04

This question came from our site for professional and enthusiast programmers.

While you can manually renounce the hyperlink by using \glsentrytext{entry}, or \glsentryname{entry}, or \gls*{entry} instead of \gls{entry}, this is cumbersome, as one would have to track the place of the first usage oneself.

An automatic approach is to patch \@gls@:

\documentclass[a4paper,10pt]{article}

\usepackage{glossaries}

\usepackage{etoolbox}
\makeatletter
%% patch first occurence of "\@gls@link[#1]{#2}{\@glo@text}", as this is the one for \glsused{#2}
\patchcmd{\@gls@}
{}{}
\makeatother

\makeglossaries
\newglossaryentry{pear}{
name=pear,
description={an oddly shaped fruit}
}

\begin{document}
\gls{pear}, \gls{pear}, \gls{pear}, \gls{pear}.

\printglossaries
\end{document}


If you also use upper case variants \Gls and \GLS, the respective internal macros \@Gls@ and \@GLS@ need similar patching:

\patchcmd{\@Gls@}
{}{}

\patchcmd{\@GLS@}
{}{}

• What about \glslink{...}? – patryk.beza Oct 2 '16 at 20:55

There's a hook \glslinkcheckfirsthyperhook that gets used by commands like \gls. It's original purpose was to assist with the opposite (don't hyperlink on first use) but it also can be used in this situation:

\documentclass{article}

\usepackage{glossaries}

\makeglossaries

\ifglsused{\glslabel}%
{%
}%
{}%
}

\newglossaryentry{sample}{name={sample},description={an example}}
\newglossaryentry{sample2}{name={another sample},description={another example}}

\begin{document}

First use: \gls{sample}, \gls{sample2}.

Next use: \gls{sample}, \gls{sample2}.

\printglossaries

\end{document}


This produces:

Edit: This works for all the \gls-like commands that query the first use flag (e.g. \Gls and \glsdisp). It doesn't work for the \glstext-like commands that don't query the first use flag (e.g. \glstext and \glslink).

• What about \glslink{...}, \Gls{...} and others? – patryk.beza Oct 2 '16 at 20:53
• @patryk.beza I've updated my answer. – Nicola Talbot Oct 3 '16 at 7:32
• @NicolaTalbot is it possible to use this method also for section- or chapter-wise logic? – languitar Nov 29 '17 at 9:49
• @languitar One possibility would be to hook into \section or \chapter etc to unset the first use flags. – Nicola Talbot Dec 1 '17 at 12:40