14

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.com Apr 16 '13 at 21:04

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

7

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[colorlinks]{hyperref}
\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@}
  {\@gls@link[#1]{#2}{\@glo@text}}
  {\@gls@link[#1,hyper=false]{#2}{\@glo@text}}
  {}{}
\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@}
  {\@gls@link[#1]{#2}}
  {\@gls@link[#1,hyper=false]{#2}}
  {}{}

\patchcmd{\@GLS@}
  {\@gls@link[#1]{#2}{\MakeUppercase{\@glo@text}}}
  {\@gls@link[#1,hyper=false]{#2}{\MakeUppercase{\@glo@text}}}
  {}{}
  • 1
    What about \glslink{...}? – patryk.beza Oct 2 '16 at 20:55
4

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[colorlinks]{hyperref}
\usepackage{glossaries}

\makeglossaries

\renewcommand*{\glslinkcheckfirsthyperhook}{%
  \ifglsused{\glslabel}%
  {%
    \setkeys{glslink}{hyper=false}%
  }%
  {}%
}

\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:

image of document with hyperlinks for first use only

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

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.