About
Shop
LaTeX
Free Software
Books
News
Contact
Blog
News: Tune in to Radio Norfolk's Matthew Gudgin's Teatime Show between 6 and 7pm on Wednesday, July 12th for Keith Skipper's mardle with Nicola Cawley (aka Nicola Talbot).

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


Last modified: 2017-07-13.

The free resources on this site are funded by book sales, not by adverts. If you would like to help keep this site free of annoying third-party ads, please consider buying a book.

© 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 Site Map FAQs