About 🛈
Shop 🛒Shop 🛒
LaTeX 🖹
Software 💾
Books 📚
Gallery 🖺
News 📢
Contact 📧
Blog 🖋

Gallery (glossaries-extra): Setting Styles According to Category

Image of
mixed styles sample
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).

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

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

% 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
These commands can be run from a terminal or command prompt or you might be able to run them by clicking on a button in your text editor.

Download: PDF, document source.

© 2016 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs