3

Using the glossaries package, I want to emphasize the first occurrence of an entry both in the running text as well as in the glossary. I can achieve the former in the following MWE:

\documentclass{article}

\usepackage{glossaries}
\makeglossaries

\defglsentryfmt{%
  \ifglsused{\glslabel}{%
    \glsgenentryfmt%
  }{%
    % Typeset first use
    \textit{\glsgenentryfmt}%
  }%
}

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

\begin{document}
First occurrance of \gls{term}, second occurrance of \gls{term}.

\newpage

Third occurance of \gls{term}.

\printglossary{}

\end{document}

but that doesn't emphasize the page number in the glossary list. Any idea on how that can be done?

2

As from glossaries version 4.16 you can use \glswriteentry to automatically set format=textit on first use:

\documentclass{article}

\usepackage{glossaries}
\makeglossaries

\defglsentryfmt{%
  \ifglsused{\glslabel}{%
    \glsgenentryfmt%
  }{%
    % Typeset first use
    \textit{\glsgenentryfmt}%
  }%
}

\renewcommand*{\glswriteentry}[2]{%
  \ifglsused{#1}{}{\setkeys{glslink}{format=textit}}%
  #2%
}

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

\begin{document}
First occurrance of \gls{term}, second occurrance of \gls{term}.

\newpage

Third occurance of \gls{term}.

\printglossary

\end{document}

This is equivalent to:

\documentclass{article}

\usepackage{glossaries}
\makeglossaries

\defglsentryfmt{%
  \ifglsused{\glslabel}{%
    \glsgenentryfmt%
  }{%
    % Typeset first use
    \textit{\glsgenentryfmt}%
  }%
}

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

\begin{document}
First occurrance of \gls[format=textit]{term}, second occurrance of \gls{term}.

\newpage

Third occurance of \gls{term}.

\printglossary

\end{document}

In this example there's a problem here. There are two instances of "term" on the same page but they have different encap (format) values. The first has textit as the encap and the second has the default glsnumberformat as the encap. This means that the location list for "term" will be 1, 1, 2. This is reported by makeindex in the .glg file:

## Warning (input = test.glo, line = 1; output = test.gls, line = 5):
   -- Conflicting entries: multiple encaps for the same page under same key.

If you switch to xindy it will silently discard duplicate locations and the location list will be 1, 2. The textit location is discarded. I don't know what its rules are in this case, but it's obviously not a case of keep the first and discard the others or it would have kept the textit location.

I'm not sure how to get around that problem without some manual intervention (such as using one of the non-indexing commands). It's the same issue as trying to do \index{term|textit} and \index{term} on the same page. If there's some way of telling xindy which encap should have priority then that would be the simplest solution.

Update: to answer your questions in the comments:

Pre-version 4.16 you'll have to manually add the format key to commands like \gls, as in the second example above. I think this actually makes more sense as then you can set the format at the point in the document where the most important use of the entry occurs, otherwise you may as well leave the reader to start with the first location and progress from there. (The first use may not necessarily be an important use of the term as the first use may come with a "described in more detail later in chapter n".)

The slight space between the italic 1 and the comma is caused by the comma being inside a command (\delimN) which I suspect confuses the check for italic correction performed in \textit. This can be demonstrated with the following:

\documentclass{article}

\newcommand*{\delimN}{, }

\begin{document}
\textit{1}, 2.

\textit{1}\delimN 2.

{\itshape 1}\delimN 2.

\end{document}

Second italic one has a slight space before the comma

This can be avoided by providing a command that uses the declaration instead of the text-block command:

\documentclass{article}

\usepackage{glossaries}
\makeglossaries

\defglsentryfmt{%
  \ifglsused{\glslabel}{%
    \glsgenentryfmt%
  }{%
    % Typeset first use
    \textit{\glsgenentryfmt}%
  }%
}

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

\newcommand*{\firstloc}[1]{{\itshape #1}}

\begin{document}
First occurrance of \gls[format=firstloc]{term}, second occurrance of
\gls{term}.

\newpage

Third occurance of \gls{term}.

\printglossary

\end{document}

This produces:

image of glossary

  • In the worst case I can live with having multiple entries of the same page but different font. However, I don't know whether the publisher will have access to such a late version of glossaries. Is there a more backwards-compatible way of doing it? – gablin Jul 13 '15 at 8:07
  • I came across a command which I could abuse to achieve what I want, but I fear it might have unexpected consequences. I've provided it in a separate answer for your eyeballs. =) – gablin Jul 13 '15 at 14:32
0

I stumbled across the following command in the user manual - \glslinkcheckfirsthyperhook - together with an example on how it can be used. From that I derived the following MWE, which actually does what I'm looking for:

\documentclass{article}

\usepackage{hyperref}
\usepackage{glossaries}
\makeglossaries

\defglsentryfmt{%
  \ifglsused{\glslabel}{%
    \glsgenentryfmt%
  }{%
    % Typeset first use
    \textit{\glsgenentryfmt}%
  }%
}

\newcommand{\firsttermformat}[1]{{\itshape\glshypernumber{#1}}}
\renewcommand*{\glslinkcheckfirsthyperhook}{%
  \ifglsused{\glslabel}{%
    % Do nothing
  }{%
    \setkeys{glslink}{format=firsttermformat}%
  }%
}

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

\begin{document}
First occurrance of \gls{term}, second occurrance of \gls{term}.

\newpage

Third occurance of \gls{term}.

\printglossary{}

\end{document}

Although the hyperref package was not included in the MWE of my question, I've included it here because I'm using it in my book and there I lost the links for the first-occurrence page numbers in the index. Using \glshypernumber fixed that.

But since Nicola Talbot didn't mention the \glslinkcheckfirsthyperhook in her answer, I'm a bit worried that I'm using this command in ways it was not originally intended. Could you please comment on this, Nicola Talbot?

  • Actually, it's "her" not "his" :-) \glslinkcheckfirsthyperhook is primarily a hook into the check to determine whether or not to set hyper=true on first use. It's used by \@gls@link@checkfirsthyper before the keys from the first optional argument of commands like \gls is used, so it's possible to override any setting through explicitly using the optional argument. There's another hook immediately after the keys are set and that's \glslinkpostsetkeys so any changes made in that command may override the user's explicit settings in the optional argument. – Nicola Talbot Jul 13 '15 at 20:28
  • In addition, \glswriteentry occurs after that but before the actual link text and deals with writing information to the external files. Finally, commands like \gls have a hook that occurs after the link text (outside any hyperlink and after the first use flag is unset) and that's \glspostlinkhook. – Nicola Talbot Jul 13 '15 at 20:29

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.