Latest news 2024-10-15: New blog post: Tales for Our Times Book Launch.

Gallery: Mixing Styles

image of 3-column glossary with name (in various colours) optionally followed by symbol in first column
This example uses the glossaries-extra package, which extends the glossaries package and provides better abbreviation handling. You’ll need at least version 1.04 of glossaries-extra.sty (you’ll also need a fairly up-to-date version of glossaries.sty as well).

This example is quite complicated as there are a lot of different styles in use. Each entry is assigned a category. The label may be any label you choose, but take care that it doesn’t contain any special characters. Be careful not to confuse the category key with the parent or type key (see Logical Glossary Divisions (type vs group vs parent)). The parent key allows you to have a hierarchical structure in your glossary and the type key allows you to have multiple glossaries.

A single glossary may contain entries with different categories and entries with the same category can be spread across different glossaries. The categories and their attributes allow fine tuning of the way entries are displayed both in the document text and in the glossary.

Semantic commands, such as \animalfont, are defined for each category. This allows the control sequence name (the command name without the leading backslash) to be used as a value for the (custom) font attribute and (supported) glossnamefont attribute. The \glsxtrregularfont command that encapsulates regular entries is modified to pick up the value of the custom font attribute.

The abbreviation and bacteria categories aren’t considered regular entries. (They use one of the abbreviation styles that sets the regular attribute to false.) This means that they won’t pick up the font attribute. Instead the font change must be handled by the abbreviation style or by redefining \glsxtrabbreviationfont. In this case, the abbreviation style is handling it. The glossdescfont attribute can be set to apply a font change to the description in the glossary.

Another attribute that’s used is glossname, which automates case-changing for the name field when it’s displayed in the glossary (where supported by the glossary style).

The “post-link” hook, which occurs after the link-text produced by commands like \gls is altered for the entries with the unit category:

\newcommand*{\glsxtrpostlinkunit}{%
 \ifdefempty\glscustomtext{\glsxtrpostlinkAddSymbolOnFirstUse}{}}

The entries with the category set to constant also have their post-link hook adjusted so that the description and user1 values are inserted after commands like \gls but only on first use:

\newcommand*{\glsxtrpostlinkconstant}{%
 \ifdefempty\glscustomtext
 {\glsxtrifwasfirstuse{ (\glsentryuseri{\glslabel},
  \glsentrydesc{\glslabel})}{}}%
 {}}

This category also adjusts the post-description hook so that the value of the user1 is also automatically inserted after the description in the glossary:

\newcommand*{\glsxtrpostdescconstant}{\space
 (\glsentryuseri{\glscurrententrylabel})}

As with Abbreviations With Translations, this example also has abbreviations with translations where again the translation is stored in the user1 field. The short-long-user abbreviation style can be used to automatically insert the translation in the parenthetical material on first use of commands like \gls. The post-description hook is adjusted to automatically insert the translation after the description in the glossary (for the abbreviations with the category set to foreign):

\newcommand*{\glsxtrpostdescforeign}{\space
 (Translation: \glsentryuseri{\glscurrententrylabel})}

There’s one awkward case where the short and long forms end with a full stop (period). This has the category set to foreignenddot so that it can be dealt with separately. If the abbreviation is referenced at the end of the sentence, this can cause a duplicate full stop, so the end of sentence period has to be discarded:

\glssetcategoryattribute{foreignenddot}{discardperiod}{true}

but not on first use (because the user1 text doesn’t end with a period):

\glssetcategoryattribute{foreignenddot}{retainfirstuseperiod}{true}

This foreignenddot category also needs to have the post-description hook set to match the foreign post-description hook:

\newcommand*{\glsxtrpostdescforeignenddot}{\space
 (Translation: \glsentryuseri{\glscurrententrylabel})}

The page list is prefixed with “Page: ” or “Pages: ” using:

\GlsXtrEnablePreLocationTag{Page: }{Pages: }

The initial comment lines below are arara directives. You can remove them if you don’t use arara.

% arara: pdflatex
% arara: makeglossaries
% arara: pdflatex
% arara: pdflatex
\documentclass[fontsize=10pt,open=any]{scrbook}

\usepackage[T1]{fontenc}
\usepackage{tgtermes}
\usepackage{xcolor}
\usepackage{siunitx}
\usepackage[hidelinks]{hyperref}
\usepackage[nopostdot=false,% don't suppress the post-description dot
 stylemods=longragged,% load glossary-longragged.sty and glossaries-extra-stylemods.sty
 style=longragged3col,% set the glossary style
 nogroupskip% don't have a gap between groups
]{glossaries-extra}

% KOMA class alterations (the default sans-serif clashes
% with the smallcaps font in the headings examples)
\setkomafont{disposition}{\normalcolor\bfseries}

\pagestyle{headings}

% Need a wider column for the location list:
\setlength{\glspagelistwidth}{0.12\textwidth}

% Adjust the description column width if necessary:
%\setlength{\glsdescwidth}{0.5\textwidth}

% Default font for name field in the glossary. This may be
% overridden by the category attributes:
\renewcommand*{\glsnamefont}[1]{\textbf{#1}}

\makeglossaries

% Define some semantic commands:
\newcommand*{\animalfont}[1]{\textcolor{brown}{#1}}
\newcommand*{\vegetablefont}[1]{\textcolor{teal}{#1}}
\newcommand*{\mineralfont}[1]{\textcolor{violet}{#1}}
\newcommand*{\myacronymfont}[1]{\textcolor{orange}{#1}}
\newcommand*{\bacteriafont}[1]{\textcolor{green}{#1}}
\newcommand*{\unitfont}[1]{\textcolor{blue}{#1}}
\newcommand*{\constantfont}[1]{\textcolor{magenta}{#1}}

% Make the first letter of the name field uppercase in the glossary
% for the given categories:

\glssetcategoryattribute{animal}{glossname}{firstuc}
\glssetcategoryattribute{mineral}{glossname}{firstuc}
\glssetcategoryattribute{vegetable}{glossname}{firstuc}

% Make the name field in the glossary use a font changing command
% (overrides \glsnamefont). In this example, I'm using the semantic
% commands I defined above. The backslash is omitted.

\glssetcategoryattribute{animal}{glossnamefont}{animalfont}
\glssetcategoryattribute{mineral}{glossnamefont}{mineralfont}
\glssetcategoryattribute{vegetable}{glossnamefont}{vegetablefont}
\glssetcategoryattribute{acronym}{glossnamefont}{myacronymfont}
\glssetcategoryattribute{bacteria}{glossnamefont}{bacteriafont}
\glssetcategoryattribute{unit}{glossnamefont}{unitfont}
\glssetcategoryattribute{constant}{glossnamefont}{constantfont}

% Modify the font used for the description in the glossary:
%\glssetcategoryattribute{bacteria}{glossdescfont}{textsf}

% Modify the way commands like \gls display regular terms according
% to attribute.

\glssetcategoryattribute{general}{font}{textsf}
\glssetcategoryattribute{mineral}{font}{mineralfont}
\glssetcategoryattribute{animal}{font}{animalfont}
\glssetcategoryattribute{vegetable}{font}{vegetablefont}
\glssetcategoryattribute{acronym}{font}{myacronymfont}
\glssetcategoryattribute{unit}{font}{unitfont}
\glssetcategoryattribute{constant}{font}{constantfont}

% I can't add one of the above for the "abbreviation" or "bacteria"
% category as they aren't considered regular entries. (They use
% one of the abbreviation styles that sets the "regular" attribute
% to "false".)
\glssetcategoryattribute{bacteria}{font}{bacteriafont}% ignored!
% If the abbreviation style for "bacteria" is changed to, say,
% "short-em", the attribute won't be ignored.

% Adjust \glsxtrregularfont so that it picks up the appropriate
% command from the "font" attribute (set above).
\renewcommand*{\glsxtrregularfont}[1]{%
  \glshasattribute{\glslabel}{font}%
  {\csuse{\glsgetattribute{\glslabel}{font}}{#1}}%
  {#1}%
}

\newglossaryentry{duck}{name=duck,
  category={animal},
  description={a waterbird with webbed feet}}

\newglossaryentry{parrot}{name=parrot,
  category={animal},
  description={mainly tropical bird with bright plumage}}

\newglossaryentry{cabbage}{name=cabbage,
  category={vegetable},
  description={vegetable with thick green or purple leaves}}

\newglossaryentry{brocolli}{name=brocolli,
  category={vegetable},
  description={vegetable with heads of small green or purple flower
buds}}

\newglossaryentry{quartz}{name=quartz,
 category=mineral,
 description={hard mineral consisting of silica}}

\newglossaryentry{corundum}{name=corundum,
 category=mineral,
 description={crystalline form of aluminium oxide}}

% The following doesn't have the category explicitly set, so it
% defaults to "general":

\newglossaryentry{loremipsum}{name={lorem ipsum},
 description={dummy text}}

% The following set the category to "unit":

\newglossaryentry{ohm}{name=ohm,symbol={\si{\ohm}},
category=unit,
description=unit of electrical resistance}

\newglossaryentry{angstrom}{name={\aa}ngstr\"om,
category=unit,
symbol={\si{\angstrom}},sort=angstrom,
description={non-SI unit of length}}

% The selected style doesn't display the symbol
% so add it to the post-name hook:
\newcommand*{\glsxtrpostnameunit}{ (\glsentrysymbol{\glscurrententrylabel})}

% Insert the symbol after commands like \gls but only on first use
% (and only for the "unit" category):

\newcommand*{\glsxtrpostlinkunit}{%
 \ifdefempty\glscustomtext{\glsxtrpostlinkAddSymbolOnFirstUse}{}}
% The above "post-link" hook occurs after the link-text produced by 
% \gls so it's place outside the \unitfont argument.

% Mathematic constants need the "sort" key set because the "name"
% field contains commands:
\newglossaryentry{pi}{name={\ensuremath{\pi}},
sort=pi,
category=constant,
user1=pi,
description={ratio of a circle's circumference to its diameter}}

% Insert the description and "user1" text after commands like \gls but only on first use
% (and only for the "constant" category):

\newcommand*{\glsxtrpostlinkconstant}{%
 \ifdefempty\glscustomtext
 {\glsxtrifwasfirstuse{ (\glsentryuseri{\glslabel},
  \glsentrydesc{\glslabel})}{}}%
 {}}

% Insert the "user1" text after the description in the glossary
% but only for the "constant" category:
\newcommand*{\glsxtrpostdescconstant}{\space
 (\glsentryuseri{\glscurrententrylabel})}

% \newacronym sets the category to "acronym" by default. The
% "short-em" style makes it a regular entry.
\setabbreviationstyle[acronym]{short-em}

\newacronym{laser}{laser}{light amplification by
stimulated emission of radiation}

\newacronym{scuba}{scuba}{self-contained underwater breathing
apparatus}

% \newabbreviation sets the category to "abbreviation" by default.
% The "long-short-sc" style sets the regular attribute to false.
\setabbreviationstyle{long-short-sc}

\newabbreviation{html}{html}{hypertext markup language}

\newabbreviation{svg}{svg}{scalable vector graphics}

% The optional argument can be used to set a different category:
\setabbreviationstyle[bacteria]{long-em-short-em}
%\setabbreviationstyle[bacteria]{short-em}

% An alternative approach that uses \bacteriafont in the document
% and in the glossary is to provide a custom style:
%\newabbreviationstyle{bacteria}%
%{%
%  \GlsXtrUseAbbrStyleSetup{long-short}%
%}%
%{%
%  \GlsXtrUseAbbrStyleFmts{long-short}%
%  \renewcommand*{\glsabbrvfont}[1]{\bacteriafont{##1}}%
%  \renewcommand*{\glsfirstlongfont}[1]{\bacteriafont{##1}}%
%  \renewcommand*{\glslongfont}[1]{\bacteriafont{##1}}%
%}
% (the definition of \bacteriafont would need to be adjusted to
% include \emph)
%\setabbreviationstyle[bacteria]{bacteria}

\newabbreviation[category=bacteria]%
 {cbot}% label
 {C.~botulinum}% short
 {Clostridium botulinum}% long

\newabbreviation[category=bacteria]%
 {putida}% label
 {P.~putida}% short
 {Pseudomonas putida}% long

% Abbreviations that have translations. These put the translated
% text in the "user1" field. The "-user" abbreviation styles
% insert this field (if present) into the parenthetical material
% when used with \gls.

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

% The fonts used by this style can be adjusted:
\renewcommand*{\glsabbrvuserfont}[1]{\textsf{\color{red}#1}}% short-form font
\renewcommand*{\glslonguserfont}[1]{\textsf{#1}}% long-form font

\newabbreviation[category=foreign,user1={French \TeX\ user group}]
 {gutenberg}{GUTenburg}{Groupe francophone des utilisateurs de \TeX}

% Insert the translation (user1) text after the description 
% in the glossary but only for the "foreign" category:
\newcommand*{\glsxtrpostdescforeign}{\space
 (Translation: \glsentryuseri{\glscurrententrylabel})}

% Now we have an awkward one that ends with a period:
\setabbreviationstyle[foreignenddot]{short-long-user}

% Need to discard the end of sentence period for the
% entries in this category.

\glssetcategoryattribute{foreignenddot}{discardperiod}{true}

% But not on first use (the user1 text doesn't end with a
% period):
\glssetcategoryattribute{foreignenddot}{retainfirstuseperiod}{true}

\newabbreviation
 [user1={German Speaking \TeX\ User Group},category=foreignenddot]
 {dante}{DANTE e.V.}{Deutschsprachige Anwendervereinigung \TeX\ e.V.}

% Insert the translation (user1) text after the description 
% in the glossary but only for the "foreignenddot" category:
\newcommand*{\glsxtrpostdescforeignenddot}{\space
 (Translation: \glsentryuseri{\glscurrententrylabel})}

% Insert page/pages tag before location list:
\GlsXtrEnablePreLocationTag{Page: }{Pages: }

\begin{document}
\tableofcontents

\chapter{Samples in the Main Document Text}

\section{All Entries}

First use (\verb|\gls|): \forglsentries{\thislabel}{\gls{\thislabel}. }

Next use (\verb|\gls|): \forglsentries{\thislabel}{\gls{\thislabel}. }

Using \verb|\glsfirst|: \forglsentries{\thislabel}{\glsfirst{\thislabel}. }

Using \verb|\glstext|: \forglsentries{\thislabel}{\glstext{\thislabel}. }

\section{Abbreviations only}
Abbreviations that have the \verb|regular| attribute set are
hybrids and can have odd results. In this example, the hybrid
entries are: 
\forglsentries{\thislabel}{%
 \ifglshasshort{\thislabel}%
 {% is an abbreviation
   \glsifregular{\thislabel}%
   {% is regular
     \texttt{\thislabel}.
   }%
   {}%
 }
 {}%
}

Using \verb|\glsxtrfull|: 
\forglsentries{\thislabel}{%
 \ifglshasshort{\thislabel}{\glsxtrfull{\thislabel}.  }{}}

Using \verb|\glsxtrshort|: 
\forglsentries{\thislabel}{%
 \ifglshasshort{\thislabel}{\glsxtrshort{\thislabel}.  }{}}

Using \verb|\glsxtrlong|: 
\forglsentries{\thislabel}{%
 \ifglshaslong{\thislabel}{\glsxtrlong{\thislabel}.  }{}}

\section{Symbols}

Using \verb|\glssymbol|:
\forglsentries{\thislabel}{%
 \ifglshassymbol{\thislabel}{\glssymbol{\thislabel}.  }{}}

\section{User Field}

Using \verb|\glsuseri|:
\forglsentries{\thislabel}{%
 \ifglshasfield{useri}{\thislabel}{\glsuseri{\thislabel}[.]  }{}}

(The full stop in the above is placed inside square brackets
to prevent the \verb|discardperiod| attribute from discarding it
since the \verb|useri| field doesn't end with a dot.)

\chapter{Math Mode}
Inline: $\gls{pi} = \frac{C}{d}$, display:

\[
 \gls{pi} = \frac{C}{d}
\]

\chapter{Samples in Headings}

Don't use commands like \verb|\gls| in section headings, but you can
use commands like \verb|\glsfmttext| or \verb|\glsfmtshort|.

\forglsentries{\thislabel}{%
 \ifglshasshort{\thislabel}%
 {\section{\glsfmtshort{\thislabel}}}%
 {%
% the constant entry "pi" can't be used in the PDF bookmarks,
% so use the user1 text for the bookmark
   \glsifcategory{\thislabel}{constant}
   {%
     \section{\texorpdfstring{\glsfmttext{\thislabel}}{\glsentryuseri{\thislabel}}}%
   }%
   {%
     \section{\glsfmttext{\thislabel}}%
   }%
 }%
 \Glsdesc[noindex]{\thislabel}.
}


\renewcommand{\glossarypreamble}{\emph{This glossary uses the style 
`\csuse{@glossary@default@style}' with modifications made
through categories and their attributes.}\par\medskip}
\printglossaries

\end{document}

If you don’t use arara, you need to run the following commands:

pdflatex sample-name-font
makeglossaries sample-name-font
pdflatex sample-name-font

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

Download: PDF (197.37K), source code (11.32K).