0

I'm using hyperref with glossaries. Per default, glossaries use \hyperlink{<target>}{<text>} to generate the hyperlink. This results in the default link color being used (typically, red, or whatever is defined by linkcolor or linkbordercolor). I would like the color of the glossaries links to be different than the standard link color, say, blue instead of red.

I read the extended manual of glossaries but couldn't figure it out.

The closest I got was this:

\renewcommand*{\glstextformat}[1]{\hypersetup{linkbordercolor={0 0 1}}#1\hypersetup{linkbordercolor={1 0 0}}}

but glstextformat changes the text inside the hyperlink, such that I miss the moment where the hyperlink is created, so it's too late to change the color. So what happens with my command is, after the hyperlink is created, I quickly change the color of all the next links to blue and then immediately change it back, which makes no difference, of course.

I also tried glossaries-extra:

\usepackage{glossaries-extra}
\glssetcategoryattribute{acronym}{hyperoutside}{false}
\newcommand{\myformat}[2]{234 #1 #2 123 123}
\glssetcategoryattribute{acronym}{textformat}{myformat}

in the hope that the second argument to the textformat command will be the link to use in the hyperlink, but there was no second argument.

Thanks!

EDIT: here's a minimal working example:

\documentclass{book}
\usepackage{hyperref}
\usepackage{glossaries-extra}

\makeglossaries

% GLOSSARY
\newglossaryentry{entry}{
    name=glossary entry,
    description={Test description}}

% ACRONYMS
\newacronym{test}{TEST}{This Entry Stands for Test} 

\begin{document}

\hypertarget{tag}{Something something}

Want this \gls{entry} to be blue.

This is a simple hyperlink, which should be the default red \hyperlink{tag}{some link}.

Want this next glossary link to be blue again: \gls{test}.

Thanks.

\end{document}
5
  • 4
    you should provide a small but complete example that can be used for tests. May 16, 2021 at 20:56
  • I don't think an example would help but I added one nevertheless. This question requires in-depth glossaries knowledge, I assume the people who have the knowledge would not need an example.
    – Eris
    May 17, 2021 at 1:01
  • well you can naturally wait until the one with in-depth glossaries knowledge comes along and answers your question without needing an example, but until then people with the knowledge how to use \tracingmacros on an existing example can be useful too. You can try \pretocmd\glsxtrprotectlinks{\color{blue}}{}{\fail}, but imho glossaries has no easy hook for this and it doesn't use the hyperref commands in a way that would allow hyperref to do something. You should make a feature request for a real solution. May 17, 2021 at 8:47
  • > imho glossaries has no easy hook for this Yeah, you're probably right. I would expect such a customization to have a proper hook but I really can't find it in the manual. I'll wait a bit more and if nothing happens I'll try your suggestion, thanks!
    – Eris
    May 17, 2021 at 8:54
  • you can also try \pretocmd\gls{\hypersetup{linkbordercolor=blue}}{}{\fail} \newcommand*{\glsxtrpostlinkgeneral}{\hypersetup{linkbordercolor=red}}. But nothing feels really right. May 17, 2021 at 9:19

1 Answer 1

0

It's ugly, but it works.

\documentclass{book}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{glossaries-extra}

\let\oldgls=\gls
\renewcommand{\gls}[1]{{\hypersetup{linkbordercolor=blue}%
  \oldgls{#1}}}%

\makeglossaries

% GLOSSARY
\newglossaryentry{entry}{
    name=glossary entry,
    description={Test description}}

% ACRONYMS
\newacronym{test}{TEST}{This Entry Stands for Test} 

\begin{document}

\hypertarget{tag}{Something something}

Want this \gls{entry} to be blue.

This is a simple hyperlink, which should be the default red \hyperlink{tag}{some link}.


Want this next glossary link to be blue again: \gls{test}.

Thanks.

\end{document}
5
  • Thanks for the reply. As I mentioned, I tried to do this with \renewcommand*{\glstextformat}[1]{\hypersetup{linkbordercolor={0 0 1}}#1\hypersetup{linkbordercolor={1 0 0}}} but it didn't help.
    – Eris
    May 17, 2021 at 0:54
  • Your new solution is something that I had in mind as a last resort. There are a bunch of gls commands like \glspl, \Gls etc, so I'd have to redefine them all, but they're not too many. I was thinking of defining my own commands, but I like your solution with renewcommand more, thanks.
    – Eris
    May 17, 2021 at 9:03
  • BTW, it turns out that the color can be set locally. May 17, 2021 at 13:28
  • could you come up with a macro to call this for all the gls commands? In case you're good with Tex macros. The commands are \gls, \Gls, \GLS, \glspl, \Glspl, \GLSpl, \glsdisp. So basically something like \def\redefinegls#1{\let\old#1=\#1 \renewcommand{\#1}[1] {{\hypersetup{linkbordercolor=gls_links}\old#1{#1}}}} FOREACHINTHELIST \redefinegls{ELEMENT} I'm really bad with Tex macros as you can see.
    – Eris
    May 18, 2021 at 17:05
  • I would use \csname #1\endcsname and remove the backslashes, e.g \foreach \name in {gls,Gls,GLS, etc.} {\expandafter\let\csname old\name\endcsname=\csname \name\endcsname etc.} Actually, I wouldn't bother with a loop, just copy and edit over and over. May 19, 2021 at 0:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .