# creating new command including siunitx - error due to spacing (?)

I have created a new command for the glossarie-package. Especcially for symbols...

\newcommand*{\nsymbol}[4]{\newglossaryentry{#1}{
type=symbols,
name={#2},
description={\nopostdesc},
symbol={{#3}},
user1={#4},
sort={#1}}}


The main issue here (i guess) the following line works fine, but the second one gives me an 'Undefiened Control Sequence' and I can't determine whats the problem.

\nsymbol{abc}{alphabet}{$A_.{bc}$}{$\si{cm}$}
\nsymbol{abc}{alphabet}{$A_.{bc}$}{$\si{\metre}$}


I can only guess, but it seems the newcommand doesnt work quite well with siunitx, since the error says something like:

! Undefined control sequence. \@glo@useri ->$\si {\metre }$ l.38 ...alphabet}{$A_.{bc}$}{$\si{\metre}$}

Where there is a space between '\si' and '{\metre}', maybe this is the problem... It doesn't make any difference in, whether its mathmode or not. Thanks in advance, its probably stupid, but I would appreciate any helpful answer.

EDIT:that's kind of a minimal example, not quite sure, what's the problem here, so I copied a lot.

\documentclass{scrreprt}

\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage[locale=DE]{siunitx}
\sisetup{detect-mode = false,
detect-family=true,
mode=math,
%               output-decimal-marker={,},
binary-units=true,
group-separator={\,},group-minimum-digits=3%
}

% upright indices-shortcut
\mathcode\.="8000
\begingroup\lccode~=.
\lowercase{\endgroup\def~}#1{\mathrm{#1}}

\usepackage{hyperref}

\usepackage[acronym, symbols, nomain, toc]{glossaries}
\usepackage{booktabs}
\usepackage{tabu}

%%% new glossary styl
\newglossarystyle{symblongtabu}{
\renewenvironment{theglossary}{
}{
\end{longtabu}}
\renewcommand*{\glsgroupskip}{}
\toprule
\bfseries Formelzeichen & \bfseries Bezeichnung &
\bfseries Einheit & \bfseries Seite(n)
\bottomrule\endfoot}
\renewcommand*{\glossentry}[2]{
\glsentryitem{##1}
% Entry number if required
\glstarget{##1}{\glossentrysymbol{##1}} &
\glossentryname{##1} &
\glsentryuseri{##1} &
##2\tabularnewline}}

\makenoidxglossaries

%%% newsymb macro
\newcommand*{\nsymbol}[4]{\newglossaryentry{#1}{
type=symbols,
name={#2},
description={\nopostdesc},
symbol={{#3}},
user1={#4},
sort={#1}}}
\defglsentryfmt[symbols]{\ifmmode
\glssymbol{\glslabel}
\else
\glsgenentryfmt~\glsentrysymbol{\glslabel}
\fi}
%%% entries
\nsymbol{abc}{alphabet}{$A_.{bc}$}{$\si{cm}$}
\nsymbol{abc1}{alphabet1}{$A_.{bc1}$}{$\si{\metre}$}

\begin{document}

\printnoidxglossary[type = symbols, style = symblongtabu]

\gls{abc} \\
\gls{abc1}

\end{document}

• Welcome to TeX.SX! Please help us (and also you) and add a minimal working example (MWE), that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass and ending with \end{document}. Try \SI{}{\metre} Sep 7, 2017 at 16:19
• Done, hope it is good enough. To make it work zou need to comment both second lines... Sep 7, 2017 at 16:36
• Nearly missed your other advice...\SI{}{} doesnt make any difference...white space again before the curly bracket Sep 7, 2017 at 16:41

Just \protect it, as in \nsymbol{abc1}{alphabet1}{$A_.{bc1}$}{$\si{\protect\metre}$}

UPDATE: Nicola just provided the Rosetta Stone...adding \glsnoexpandfields to the preamble alleviates the need to protect the arguments. It works to "prevent expansion occurring when the entry's internal commands are set."

\documentclass{scrreprt}

\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage[locale=DE]{siunitx}
\sisetup{detect-mode = false,
detect-family=true,
mode=math,
%               output-decimal-marker={,},
binary-units=true,
group-separator={\,},group-minimum-digits=3%
}

% upright indices-shortcut
\mathcode\.="8000
\begingroup\lccode~=.
\lowercase{\endgroup\def~}#1{\mathrm{#1}}

\usepackage{hyperref}

\usepackage[acronym, symbols, nomain, toc]{glossaries}
\usepackage{booktabs}
\usepackage{tabu}

%%% new glossary styl
\newglossarystyle{symblongtabu}{
\renewenvironment{theglossary}{
}{
\end{longtabu}}
\renewcommand*{\glsgroupskip}{}
\toprule
\bfseries Formelzeichen & \bfseries Bezeichnung &
\bfseries Einheit & \bfseries Seite(n)
\bottomrule\endfoot}
\renewcommand*{\glossentry}[2]{
\glsentryitem{##1}
% Entry number if required
\glstarget{##1}{\glossentrysymbol{##1}} &
\glossentryname{##1} &
\glsentryuseri{##1} &
##2\tabularnewline}}

\makenoidxglossaries

%%% newsymb macro
\newcommand*{\nsymbol}[4]{\newglossaryentry{#1}{
type=symbols,
name={#2},
description={\nopostdesc},
symbol={{#3}},
user1={#4},
sort={#1}}}
\defglsentryfmt[symbols]{\ifmmode
\glssymbol{\glslabel}
\else
\glsgenentryfmt~\glsentrysymbol{\glslabel}
\fi}
%%% entries
\nsymbol{abc}{alphabet}{$A_.{bc}$}{$\si{cm}$}
\nsymbol{abc1}{alphabet1}{$A_.{bc1}$}{$\si{\protect\metre}$}

\begin{document}

\printnoidxglossary[type = symbols, style = symblongtabu]

\gls{abc} \\
\gls{abc1}

\end{document}


• well, that did the trick. Thanks a lot. Any suggestions on how to solve the issue if I define the units in the newcommand with user1={$\si{#4}$} and only write \nsymbol{abc}{alphabet}{$A_.{bc}$}{\metre} ? Reason here would be the time to add new ones... Sep 7, 2017 at 18:21
• @Djiwahwah I am sorry, but I have no direct workaround for the requirement to \protect the argument of \si. Sep 7, 2017 at 18:29
• @Djiwahwah except that neither \p1 nor \p2 are valid macro names since 1 and 2 are not an alphabetic catcode. I think this might be a case of "the cure is worse than the ailment" Sep 7, 2017 at 19:14
• It's simpler to just add \glsnoexpandfields to prevent expansion occurring when the entry's internal commands are set then you don't need \protect. Sep 7, 2017 at 19:14
• One last thing, this solution does make it possible to use only the unit-token within \nsymb such as \ampere if you set user1={$\si{#4}$} - might be helpful to somebody Sep 7, 2017 at 19:34