Gallery: Mixed Glossary with Emphasis (glossaries-extra.sty)

image of sample document with first use emphasized (except for the short forms)
This is an alternative to Mixed Glossary with Emphasis that uses the glossaries-extra package. (See also Mixing Styles.)

There are several minor variations listed below. The examples all use the sample files example-glossaries-brief.tex and example-glossaries-acronym.tex which should all be installed with the base glossaries package. Note that in all the examples, I’m applying the abbreviation style to the acronym category (specified in the optional argument to \setabbreviationstyle) since the sample files use \newacronym rather than \newabbreviation.

“First use” means the first time an entry is referenced using one of the following commands: \gls, \Gls, \GLS, \glspl, \Glspl, \GLSpl, \glsdisp (the “\gls-like” commands). Other referencing commands, such as \glstext, \glsxtrshort, \glsxtrfull and \glslink (the “\glstext-like” commands), don’t alter or query the first use flag.

The examples all use the compact mcolindex glossary style, and they also load the hyperref package so the \gls-like and \glstext-like commands all create a hyperlink to the entry’s line in the glossary. The default with the colorlinks option is to show the hyperlink in red. Some of the examples here override that by applying a specific colour (teal or violet). The entries all have a “1” after the description. This is the page number on which the entry was referenced. In these sample documents all the entries were referenced on page 1. If you don’t want these numbers to appear, use the nonumberlist option.

All examples listed below are based on the following document code:

% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex
\documentclass{article}

\usepackage[colorlinks]{hyperref}
\usepackage[stylemods=mcols]{glossaries-extra}

\makeglossaries

% set abbreviation style here with the category set to "acronym"

\loadglsentries{example-glossaries-brief}
\loadglsentries{example-glossaries-acronym}

\begin{document}

\section{No First Use Reference}

Regular: \glstext{ac}.
Short form: \glsxtrshort{aeu}.
Long form: \glsxtrlong{aeu}.
Full form: \glsxtrfull{aeu}.

\section{First Use}

Regular entries: \gls{ac}, \gls{accumsan}, \gls{amet},
\gls{bibendum}, \gls{consectetuer}, \gls{diam}.

Acronyms: \gls{aeu}, \gls{afm}, \gls{anp}, \gls{cas},
\gls{cdg}, \gls{cea}, \gls{dia}.

\section{Next Use}

Regular entries: \gls{ac}, \gls{accumsan}, \gls{amet},
\gls{bibendum}, \gls{consectetuer}, \gls{diam}.

Acronyms: \gls{aeu}, \gls{afm}, \gls{anp}, \gls{cas},
\gls{cdg}, \gls{cea}, \gls{dia}.

\section{No First Use Reference}

Regular: \glstext{ac}.
Short form: \glsxtrshort{aeu}.
Long form: \glsxtrlong{aeu}.
Full form: \glsxtrfull{aeu}.

\printglossary[style=mcolindex]
\end{document}

The comment line:

% set abbreviation style here with the category set to "acronym"

is where the code fragments in the examples below should be put.

The initial comment lines are arara directives. You can remove them if you don’t use arara. If you don’t use arara, you need to run the following commands:

pdflatex filename or basename
makeglossaries basename
pdflatex filename or basename

(See Incorporating makeglossaries or makeglossaries-lite or bib2gls into the document build.)

Separate Regular and Abbreviation Fonts

example image

This method uses the long-short abbreviation style but modifies the font used by \gls-like and \glstext-like commands so that regular terms appear in violet with emphasis and abbreviations (including full form) appear in teal with emphasis:

\setabbreviationstyle[acronym]{long-short}

\renewcommand{\glsxtrregularfont}[1]{\emph{\color{violet}#1}}
\renewcommand{\glsxtrabbreviationfont}[1]{\emph{\color{teal}#1}}

These commands aren’t used in the glossary, so no font change is applied there.

Download: PDF (57.87K), source code (1.22K).

Separate Regular and Abbreviation Fonts (First or Subsequent)

example image

By default the commands \glsxtrregularfont and \glsxtrabbreviationfont are only used within the \gls-like and \glstext-like commands. This means that, as long as those commands aren’t used outside of those contexts, they can use the commands provided for the post-link hook, such as \glslabel (the entry’s label) and \glsxtrifwasfirstuse, which are set at the start of \gls, \glstext etc.

There’s a difference between \glsxtrifwasfirstuse{true}{false} and \ifglsused{label}{true}{false}.

\ifglsused tests if the entry identified by the label has been marked as having been used. (That is, it tests if the entry’s first-use flag has been set.) This boolean first-use flag (switch) is set at the end of the \gls-like commands (so that the display commands can determine whether or not this is the first use). That is, the switch is set after the link text but before the post-link hook.

\glsxtrifwasfirstuse is always \let to \@secondoftwo at the start of the \glstext-like commands (regardless of the state of the first use flag) except for \glsxtrfull (see below). At the start of the \gls-like commands, \glsxtrifwasfirstuse is \let to \@firstoftwo if the first use flag hasn’t been set otherwise \glsxtrifwasfirstuse is \let to \@secondoftwo.

So I could either define \glsxtrregularfont and \glsxtrabbreviationfont to use \glsxtrifwasfirstuse:

\renewcommand{\glsxtrregularfont}[1]{\textcolor{violet}{\glsxtrifwasfirstuse{\emph{#1}}{#1}}}
\renewcommand{\glsxtrabbreviationfont}[1]{\textcolor{teal}{\glsxtrifwasfirstuse{\emph{#1}}{#1}}}

or to use \ifglsused with \glslabel:

\renewcommand{\glsxtrregularfont}[1]{\textcolor{violet}{\ifglsused{\glslabel}{#1}{\emph{#1}}}}
\renewcommand{\glsxtrabbreviationfont}[1]{\textcolor{teal}{\ifglsused{\glslabel}{#1}{\emph{#1}}}}

The first case (\glsxtrifwasfirstuse) consistently displays the \glstext-like commands without emphasis but not \glsxtrfull. The second case (\ifglsused) uses emphasis for the \glstext-like commands if they haven’t already been used (that is, in the first section of the example document) and doesn’t use emphasis if they have already been used (that is, in the last section of the example document).

For consistency, I’ve used the first approach, but what about the awkward \glsxtrfull which sets \glsxtrifwasfirstuse to \@firstoftwo instead of \@secondoftwo? This oddity is to ensure that the inline full form (obtained with \glsxtrfull) matches the first use full form (obtained with \gls) for styles like long-short. You can, however, change this behaviour by redefining \glsxtrsetupfulldefs. The default definition is:

\newcommand*{\glsxtrsetupfulldefs}{%
  \let\glsxtrifwasfirstuse\@firstoftwo
}

This uses an internal command, which are only intended for use in class or package files. If I want to change \glsxtrifwasfirstuse to \@secondoftwo for the inline full forms within my document code, I either need \makeatletter and \makeatother:

\makeatletter
\renewcommand*{\glsxtrsetupfulldefs}{%
  \let\glsxtrifwasfirstuse\@secondoftwo
}
\makeatother

or I need to use \renewcommand instead of \let:

\renewcommand*{\glsxtrsetupfulldefs}{%
  \renewcommand*{\glsxtrifwasfirstuse}[2]{##2}%
}

I’ve used the second method in this example.

Download: PDF (59.93K), source code (1.57K).

Separate Regular and Abbreviation Fonts (long-postshort-user)

example image

This method is a minor modification to the above that uses the long-postshort-user abbreviation style instead of long-short. The “post” styles shift material out of the link text and into the post-link text (which is also outside of \glsxtrabbreviationfont). There’s now a difference between the \glsxtrfull inline display style (which includes the abbreviation in the link text) and the first use of \gls (which has the abbreviation in the post-link hook).

\setabbreviationstyle[acronym]{long-postshort-user}

\renewcommand{\glsxtrregularfont}[1]{\textcolor{violet}{\glsxtrifwasfirstuse{\emph{#1}}{#1}}}
\renewcommand{\glsxtrabbreviationfont}[1]{\textcolor{teal}{\glsxtrifwasfirstuse{\emph{#1}}{#1}}}

As with the previous example, \glsxtrsetupfulldefs is modified:

\renewcommand*{\glsxtrsetupfulldefs}{%
  \renewcommand*{\glsxtrifwasfirstuse}[2]{##2}%
}

The short form appears in the normal black font for the first use of \gls. The post-link hook isn’t part of the hyperlink.

Download: PDF (56.61K), source code (1.38K).

Separate Regular and Abbreviation Fonts (long-postshort-user modification)

example image

A minor modification to the previous example. This document applies a colour change to the short form in the post-link hook but doesn’t use \emph, so the short form is upright.

\renewcommand{\glsfirstabbrvuserfont}[1]{\textcolor{teal}{#1}}

Note that this colour change isn’t applied to the parentheses punctuation characters. Again the post-link hook isn’t part of the hyperlink.

Download: PDF (56.63K), source code (1.44K).

Emphasize First-Use Long Form

example image

This method uses the long-short-user style provided by glossaries-extra, with a minor modification:

\setabbreviationstyle[acronym]{long-short-user}

\renewcommand*{\glsfirstlonguserfont}[1]{\emph{#1}}

This applies emphasis to the long form on first use. Note that \glsxtrfull also uses \glsfirstlonguserfont to encapsulate the long form, but the description in the glossary is set to the long form encapsulated with \glslonguserfont, so the long form isn’t emphasized in the glossary. The regular terms aren’t modified in this example.

Download: PDF (56.48K), source code (1.13K).

Emphasize First-Use Except Short Form

example image

This example combines some of the previous examples to emphasize the first use for both regular terms and abbreviations but not the short form:

\setabbreviationstyle[acronym]{long-short-user}

\renewcommand*{\glsfirstlonguserfont}[1]{\emph{#1}}

\renewcommand{\glsxtrregularfont}[1]{\textcolor{violet}{\glsxtrifwasfirstuse{\emph{#1}}{#1}}}
\renewcommand{\glsxtrabbreviationfont}[1]{\textcolor{teal}{#1}}

\renewcommand*{\glsxtrsetupfulldefs}{%
  \renewcommand*{\glsxtrifwasfirstuse}[2]{##2}%
}

Note that I’ve removed \emph and the first use test from \glsxtrabbreviationfont since that’s now dealt with by the abbreviation style. However, despite the redefinition of \glsxtrsetupfulldefs, this results in emphasis for the long form in \glsxtrfull because the abbreviation style explicitly uses \glsfirstlonguserfont, which has been redefined here to use \emph.

I can either provide a new abbreviation style based on long-short-user that uses \glslonguserfont instead of \glsfirstlonguserfont for the inline form:

\renewcommand{\glsxtrabbreviationfont}[1]{\textcolor{teal}{#1}}
\newabbreviationstyle{modified-long-short-user}
{%
  \GlsXtrUseAbbrStyleSetup{long-short-user}%
}
{%
  \GlsXtrUseAbbrStyleFmts{long-short-user}%
  \renewcommand*{\glsxtrinlinefullformat}[2]{%
    \glslonguserfont{\glsaccesslong{##1}\ifglsxtrinsertinside##2\fi}%
    \ifglsxtrinsertinside\else##2\fi
    \glsxtruserparen{\glsfirstabbrvuserfont{\glsaccessshort{##1}}}{##1}%
    }%
  \renewcommand*{\Glsxtrinlinefullformat}[2]{%
    \glslonguserfont{\glsaccesslongpl{##1}\ifglsxtrinsertinside##2\fi}%
    \ifglsxtrinsertinside\else##2\fi
    \glsxtruserparen{\glsfirstabbrvuserfont{\glsaccessshortpl{##1}}}{##1}%
  }%
  \renewcommand*{\glsxtrinlinefullplformat}[2]{%
    \glslonguserfont{\Glsaccesslong{##1}\ifglsxtrinsertinside##2\fi}%
    \ifglsxtrinsertinside\else##2\fi
    \glsxtruserparen{\glsfirstabbrvuserfont{\glsaccessshort{##1}}}{##1}%
  }%
  \renewcommand*{\Glsxtrinlinefullplformat}[2]{% 
    \glslonguserfont{\Glsaccesslongpl{##1}\ifglsxtrinsertinside##2\fi}%
    \ifglsxtrinsertinside\else##2\fi
    \glsxtruserparen{\glsfirstabbrvuserfont{\glsaccessshortpl{##1}}}{##1}%
  }%
}
\setabbreviationstyle[acronym]{modified-long-short-user}

or I can temporarily change \glsfirstlonguserfont to \glslonguserfont within \glsxtrabbreviationfont if it’s not the first use:

\renewcommand{\glsxtrabbreviationfont}[1]{\textcolor{teal}{\glsxtrifwasfirstuse{}{\let\glsfirstlonguserfont\glslonguserfont}#1}}

Download: PDF (56.65K), source code (2.67K).

Emphasize Long Form

example image

This method uses the long-em-short-em style provided by glossaries-extra, with a minor modification:

\setabbreviationstyle[acronym]{long-em-short-em}

\renewcommand*{\glsabbrvemfont}[1]{#1}

This simply redefines \glsabbrvemfont to just do its argument (removing the default emphasis on the short form). Note that here the long form is not only emphasized on first use, but also in the explicit full form produced with \glsxtrfull and in the glossary.

Download: PDF (56.09K), source code (1.12K).

Emphasize First-Use Long Form (long-em-short-em)

example image

If you are using the long-em-short-em style and you don’t want the emphasis to also appear in the glossary, this can achieved with a minor modification to the previous example:

\renewcommand*{\glsfirstlongemfont}[1]{\emph{#1}}%
\renewcommand*{\glslongemfont}[1]{#1}

This only uses \emph in the first use long form command \glsfirstlongemfont, which is used for the first use of the \gls-like commands and also by \glsxtrfull but not in the glossary (where the description is encapsulated with \glslongemfont instead).

Download: PDF (56.48K), source code (1.21K).