3

I am using glossaries for the list of symbols in my document and I added some keys for units. I want to use the unit(s) defined in the glossary in a siunitx range. This works fine when my defined unit is only one term but fails for more complex ones. Is there some way to achieve this?

Here's my MWE:

\documentclass{article}
\usepackage{siunitx}
\sisetup{per-mode=symbol}
\usepackage[symbols,xindy]{glossaries-extra}

% add unit
\glsaddkey
{unit}
{}
{\glsentryunit}
{\Glsentryunit}
{\glsunit}
{\Glsunit}
{\GLSunit}

\glssetnoexpandfield{unit}

% add unit
\glsaddkey
{unit2}
{}
{\glsentryunit2}
{\Glsentryunit2}
{\glsunit2}
{\Glsunit2}
{\GLSunit2}

\glssetnoexpandfield{unit2}

\makeglossaries

\glsxtrnewsymbol[%
description=power,%
unit=\si{\W},%
unit2=\si{\J\per\s}
]{P}{P}

\begin{document}

\glsunit{P}, \glsunit2{P}\\

\SIrange{0.5}{2}{\glsunit{P}}\\

\SIrange{0.5}{2}{\glsunit2{P}}

\end{document}

As you can see in the 3rd line the units are not shown.

1
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format.
    – Seamus
    Commented Jul 26, 2016 at 11:12

1 Answer 1

3

There seem to be two problems here. The first is that you can't have command names that include digits, so \glsentryunit2 actually means \glsentryunit followed by 2. (This is why there are commands, such as \glsentryuseri, that contain Roman numerals instead of digits.)

Your first \glsaddkey command defines \glsentryunit, and your second \glsaddkey attempts to define \glsentryunit but that command has already been defined, so you get an error. To get around this, you need to find another name, for example \glsentryunitII:

\documentclass{article}
\usepackage{siunitx}
\sisetup{per-mode=symbol}
\usepackage[symbols,xindy]{glossaries-extra}

% add unit
\glsaddkey
{unit}
{}
{\glsentryunit}
{\Glsentryunit}
{\glsunit}
{\Glsunit}
{\GLSunit}

\glssetnoexpandfield{unit}

% add unit
\glsaddkey
{unit2}
{}
{\glsentryunitII}
{\GlsentryunitII}
{\glsunitII}
{\GlsunitII}
{\GLSunitII}

\glssetnoexpandfield{unit2}

\makeglossaries

\glsxtrnewsymbol[%
description=power,%
unit=\si{\W},%
unit2=\si{\J\per\s}
]{P}{P}

\begin{document}

\glsunit{P}, \glsunitII{P}\\

\SIrange{0.5}{2}{\glsunit{P}}\\

\SIrange{0.5}{2}{\glsunitII{P}}

\end{document}

The second problem is that

\SIrange{0.5}{2}{\glsunitII{P}}

doesn't work, but neither does

\SIrange{0.5}{2}{\si{\J\per\s}}

It needs to be

\SIrange{0.5}{2}{\J\per\s}

I recommend removing the \si part from the definition and providing convenient commands that add it where needed. For example:

\documentclass{article}
\usepackage{siunitx}
\sisetup{per-mode=symbol}
\usepackage[symbols,xindy]{glossaries-extra}

% add unit
\glsaddkey
{unit}
{}
{\glsentryunit}
{\Glsentryunit}
{\glsunit}
{\Glsunit}
{\GLSunit}

\glssetnoexpandfield{unit}

% add unit
\glsaddkey
{unit2}
{}
{\glsentryunitII}
{\GlsentryunitII}
{\glsunitII}
{\GlsunitII}
{\GLSunitII}

\glssetnoexpandfield{unit2}

\makeglossaries

\glsxtrnewsymbol[%
description=power,%
unit=\W,%
unit2=\J\per\s
]{P}{P}

\newcommand{\sigls}[2][]{\glsdisp[#1]{#2}{\si{\glsentryunit{#2}}}}

\newcommand{\siglsII}[2][]{\glsdisp[#1]{#2}{\si{\glsentryunitII{#2}}}}

\begin{document}

\sigls{P}, \siglsII{P}

\SIrange{0.5}{2}{\glsentryunit{P}}

\SIrange{0.5}{2}{\glsentryunitII{P}}

\end{document}

image of result

Edit:

In the above, the range doesn't index the entry. If you want it indexed, you can add \glsadd. Convenient commands:

\newcommand*{\siglsrange}[4][]{\SIrange[#1]{#2}{#3}{\glsentryunit{#4}}\glsadd{#4}}
\newcommand*{\siglsrangeII}[4][]{\SIrange[#1]{#2}{#3}{\glsentryunitII{#4}}\glsadd{#4}}

Usage:

\siglsrange{0.5}{2}{P}
\siglsrangeII{0.5}{2}{P}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .