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

Gallery: Units (glossaries.sty)

image of sample document with section referencing entries and a 4-column glossary showing the symbol, description, name and page
This example is a modification of the Units (glossaries-extra.sty) example. In this case the symbolic representation is stored in the name field, the unit name is stored in the symbol field and the first field is used to make the first use show the symbol field followed by the symbolic representation in parentheses. To prevent problems caused by expansions issues, \glsnoexpandfields is used to switch off the automatic expansion.

A user command (\newunit) is provided to make it easier to define entries and to ensure consistency:

\newcommand*{\newunit}[4][]{%
  \newglossaryentry{#2}{name={#3},sort={#2},
  first={\glsentrysymbol{#2} (#3)},
  description={#4},symbol={#2},#1}%
}

Since makeindex can’t interpret commands like \si{\ohm} (instead treating it as a string starting with a literal backslash) and xindy discards commands and braces (which would leave an empty sort value), this \newunit custom user command sets the sort key to the label.

For convenience, the label is assumed to be the same as the unit name (which is stored in the symbol field) by default. This which works fine for the “ohm” entry (where both the label and unit name can be “ohm”) but not for the “Ångström” entry as entry labels can’t contain special or non-ASCII characters (unless XeLaTeX or LuaLaTeX are used), which means the label must be Angstrom (or angstrom) and not Ångström. In this case, the symbol field needs to be set in the option argument of \newunit. For example:

\newunit[symbol={\AA ngstr\"om}]{angstrom}{\si{\angstrom}}{unit of length}

This version may be used with the extension package glossaries-extra or with just the base glossaries package. I’ve used a different glossary style long4col for variation.

An alternative approach is to use \newacronym (or \newabbreviation provided by glossaries-extra.sty) with a custom glossary style.

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[style=long4col]{glossaries}

\makeglossaries

\glsnoexpandfields

\newcommand*{\newunit}[4][]{%
  \newglossaryentry{#2}{name={#3},sort={#2},
  first={\glsentrysymbol{#2} (#3)},
  description={#4},symbol={#2},#1}%
}

\newunit{ohm}{\si{\ohm}}{unit of electrical resistance}

\newunit[symbol={\AA ngstr\"om}]{angstrom}{\si{\angstrom}}{unit of length}

\begin{document}
The \gls{ohm} is a unit of resistance and the
\gls{angstrom} is a unit of length.

Let's mention \gls{ohm} and \gls{angstrom} again.

\printglossaries
\end{document}

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

pdflatex sample-units2
makeglossaries sample-units2
pdflatex sample-units2

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

Download: PDF (23.20K), source code (651B).