# Use \GlsXtrSetAltModifier to typeset glossaries link text?

I want to emphasize use of certain terms in the glossary, and when doing so I also want to emphasize the page number in the index.

From an earlier question, I found that I can use the format option to customize the font of the page number. Together with the \GlsXtrSetAltModifier command from the glossaries-extra package, I managed to achieve the following:

\documentclass{article}

\usepackage{glossaries}
\usepackage{glossaries-extra}
\makeglossaries

\newcommand{\emphPageNumber}[1]{{\itshape\glshypernumber{#1}}}
\GlsXtrSetAltModifier{!}{format=emphPageNumber}

\newglossaryentry{term}{name={term}, description=\nopostdesc}

\begin{document}
Emphasized use of \gls!{term}, normal use of \gls{term}.

\printglossary{}

\end{document}


However, I still need to manually modify the style of the entry itself (e.g. \emph{\gls!{term}}. Is there any easy way of achieving that simply using \gls! does that for me automatically?

You can take advantage of the \glslinkpostsetkeys hook, which is called after the keys (provided in the first optional argument of \gls, \glslink etc) are set. The format key sets the definition of \@glsnumberformat to the supplied format, so \glslinkpostsetkeys can be redefined to check that command. Since it's an internal command, it needs \makeatletter / \makeatother if the code is added to the .tex file (rather than a class or package).

The etoolbox command \ifdefstring can be used to check if \@glsnumberformat is emphPageNumber. The entry text is formatted according to \glstextformat, which by default just does its argument, but it can be changed here to use the appropriate formatting command:

\documentclass{article}

\usepackage{glossaries}
\usepackage{glossaries-extra}
\makeglossaries

\newcommand{\emphPageNumber}[1]{{\itshape\glshypernumber{#1}}}
\GlsXtrSetAltModifier{!}{format=emphPageNumber}

\makeatletter
\ifdefstring\@glsnumberformat{emphPageNumber}%
{\let\glstextformat\textit}%
{\let\glstextformat\@firstofone}%
}
\makeatother

\newglossaryentry{term}{name={term}, description=\nopostdesc}

\begin{document}
Emphasized use of \gls!{term}, normal use of \gls{term}.

\printglossary

\end{document}


Incidentally, \hyperit provided by the glossaries package does pretty much the same thing as your custom \emphPageNumber.

Using the makeglossaries Perl script in the build process, the above MWE produces:

Calling makeindex directly or using the makeglossaries-lite Lua script or the automake package option instead produces:

(The makeglossaries Perl script detects makeindex's "multiple encaps for the same page under same key" warning and tries to correct the problem.)

If hyperref isn't used, then there's a simpler method if you always want the text format to reflect the location format:

\documentclass{article}

\usepackage{glossaries}
\usepackage{glossaries-extra}
\makeglossaries

\newcommand{\emphPageNumber}[1]{{\itshape\glshypernumber{#1}}}
\GlsXtrSetAltModifier{!}{format=emphPageNumber}

\makeatletter
\renewcommand{\glstextformat}{\csname \@glsnumberformat\endcsname}
\makeatother

\newglossaryentry{term}{name={term}, description=\nopostdesc}

\begin{document}
Emphasized use of \gls!{term}, normal use of \gls{term}.
Bold use \gls[format=hyperbf]{term}

\printglossary

\end{document}


This can't be used with hyperref as it will try to form a hyperlink from the text with \glshypernumber, which expects a location as the argument.