Smile for the Camera: a new cybercrime short story ebook.

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:


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:

 {\glsxtrifwasfirstuse{ (\glsentryuseri{\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:


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):

 (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:


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


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

 (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

\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

% KOMA class alterations (the default sans-serif clashes
% with the smallcaps font in the headings examples)


% Need a wider column for the location list:

% Adjust the description column width if necessary:

% Default font for name field in the glossary. This may be
% overridden by the category attributes:


% Define some semantic commands:

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


% 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.


% Modify the font used for the description in the glossary:

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


% 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).

  description={a waterbird with webbed feet}}

  description={mainly tropical bird with bright plumage}}

  description={vegetable with thick green or purple leaves}}

  description={vegetable with heads of small green or purple flower

 description={hard mineral consisting of silica}}

 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":

description=unit of electrical resistance}

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):

% 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:
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):

 {\glsxtrifwasfirstuse{ (\glsentryuseri{\glslabel},

% Insert the "user1" text after the description in the glossary
% but only for the "constant" category:

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

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

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

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

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

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

% The optional argument can be used to set a different category:

% An alternative approach that uses \bacteriafont in the document
% and in the glossary is to provide a custom style:
%  \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)

 {cbot}% label
 {C.~botulinum}% short
 {Clostridium botulinum}% long

 {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.


% 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:
 (Translation: \glsentryuseri{\glscurrententrylabel})}

% Now we have an awkward one that ends with a period:

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


% But not on first use (the user1 text doesn't end with a
% period):

 [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:
 (Translation: \glsentryuseri{\glscurrententrylabel})}

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


\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: 
 {% is an abbreviation
   {% is regular

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

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

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


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

\section{User Field}

Using \verb|\glsuseri|:
 \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|.

% the constant entry "pi" can't be used in the PDF bookmarks,
% so use the user1 text for the bookmark

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


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).