Latest news 2024-09-23: new blog post "Tales for Our Times: Raising Funds and Awareness for St Martins Housing Trust".

Gallery: Units (glossaries-extra.sty)

image of sample document referencing units followed by a glossary
This example is copied from my article Glossaries and lists. It uses the glossaries-extra package to take advantage of the post-link hook.

The entry representing a unit has the unit name (ohm) in the name field and the symbolic representation (\si{\ohm}) in the symbol field. (The command \si is provided by the siunitx package, which needs to be loaded.)

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

The category unit is used to identify the entry as representing a unit. (The category field defaults to general if not set in \newglossaryentry.) This allows customisation of the link text (the text produced by commands like \gls) so that the unit (provided in the symbol field) can be automatically appended in parentheses on first use.

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

The symbol on its own can be referenced with \glssymbol (which internally sets \glscustomtext).

The default glossary style is list which doesn’t display the symbol field, so I’ve used the index style instead.

An alternative version that only uses the base glossaries package can be found in Units (glossaries.sty).

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

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

\usepackage{siunitx}
\usepackage{glossaries-extra}

\makeglossaries

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

\newglossaryentry{resistor}{name={resistor},
 description={component that implements electrical resistance}}

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

\begin{document}
A \gls{resistor} is an electrical component.
The \gls{ohm} is a unit of resistance.

Symbol: \glssymbol{ohm} and let's mention \gls{ohm} again.

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

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

pdflatex sample-units
makeglossaries sample-units
pdflatex sample-units

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

Download: PDF (33.78K), source code (700B).