# Symbols with optional parameter in glossaries with \newglossary

I am trying to use the approach given in the solution here Glossary entry with extra parameter (which looks really neat) for an application where i am having more than one glossary; in the original i have only a symbolslist and abbreviations, but here's an mwe adapting the symbolslist to the main list provided in the solution of the link above

\documentclass{scrbook}
%
\usepackage[acronym,toc]{glossaries}
%this line is new
\newglossary[slg]{symbolslist}{syi}{syg}{List of Symbols}
\makeglossaries
\glsnoexpandfields

\newcommand*{\glsarg}{i}

\newglossaryentry{BetragVektor}{
name=\ensuremath{|\overline{u_i}|},
text=|\overline{u_{\glsarg}}|,
description={ABC}}
\newglossaryentry{s:tP}{
name=\ensuremath{T_p\mathcal M},
text=T_\glsarg\mathcal M,
description={},
type=symbolslist %in own list
}
% modify the entry's format

\defglsentryfmt{%
\let\orgglsarg\glsarg
\ifdefempty\glsinsert
{}%
{%
\let\glsarg\glsinsert
\let\glsinsert\relax
}%
\glsgenentryfmt
\let\glsarg\orgglsarg
}

\begin{document}

$\gls{BetragVektor}[1]$

$\gls{BetragVektor}$

$\gls{BetragVektor}[1]$

$\gls{BetragVektor}[2]$

$\gls{BetragVektor}[]$

$\gls{s:tP}[1]$

$\gls{s:tP}$

$\gls{s:tP}[1]$

$\gls{s:tP}[2]$

$\gls{s:tP}[]$

\printglossaries
\end{document}


As mentioned, the only thing new is that I introduce a second list. The solution still seems to work for the main list as the result shows.

So somehow the new argument gets added in the and and \glsarg stays the same. How can I solve this, such that my new glossary also can have optional parameters on their entries?

## Method 1: Specific Glossary

\defglsentryfmt has an optional argument that indicates which glossary this format should govern. If omitted, main is assumed, so you also need to do the same for the new glossary:

\defglsentryfmt[symbolslist]{%
\let\orgglsarg\glsarg
\ifdefempty\glsinsert
{}%
{%
\let\glsarg\glsinsert
\let\glsinsert\relax
}%
\glsgenentryfmt
\let\glsarg\orgglsarg
}


## Method 2: All Glossaries

Alternatively, you can just redefine \glsentryfmt to apply to all glossaries. (Individual glossaries may be overridden using \defglsentryfmt, which \newacronym does automatically.)

\documentclass{scrbook}
%
\usepackage[acronym,toc]{glossaries}
%this line is new
\newglossary[slg]{symbolslist}{syi}{syg}{List of Symbols}
\makeglossaries
\glsnoexpandfields

\newcommand*{\glsarg}{i}

\newglossaryentry{BetragVektor}{
name=\ensuremath{|\overline{u_i}|},
text=|\overline{u_{\glsarg}}|,
description={ABC}}
\newglossaryentry{s:tP}{
name=\ensuremath{T_p\mathcal M},
text=T_\glsarg\mathcal M,
description={},
type=symbolslist %in own list
}
% modify the entry's format

\renewcommand*{\glsentryfmt}{%
\let\orgglsarg\glsarg
\ifdefempty\glsinsert
{}%
{%
\let\glsarg\glsinsert
\let\glsinsert\relax
}%
\glsgenentryfmt
\let\glsarg\orgglsarg
}

\begin{document}

$\gls{BetragVektor}[1]$

$\gls{BetragVektor}$

$\gls{BetragVektor}[1]$

$\gls{BetragVektor}[2]$

$\gls{BetragVektor}[]$

$\gls{s:tP}[1]$

$\gls{s:tP}$

$\gls{s:tP}[1]$

$\gls{s:tP}[2]$

$\gls{s:tP}[]$

\printglossaries
\end{document}


This produces:

## Method 3: Specific Category (glossaries-extra)

If you want to use the extension package glossaries-extra, this is a better method:

\documentclass{scrbook}
%
\usepackage[abbreviations]{glossaries-extra}

\newglossary[slg]{symbolslist}{syi}{syg}{List of Symbols}
\makeglossaries
\glsnoexpandfields

\newcommand*{\glsdefaultarg}{i}
\newcommand*{\glsarg}{\glsdefaultarg}

\newglossaryentry{BetragVektor}{
category=arg,% requires an argument
name=\ensuremath{|\overline{u_i}|},
text=|\overline{u_{\glsarg}}|,
description={ABC}}
\newglossaryentry{s:tP}{
category=arg,% requires an argument
name=\ensuremath{T_p\mathcal M},
text=T_\glsarg\mathcal M,
description={},
type=symbolslist %in own list
}
% modify the entry's format

\preto\glsentryfmt{%
\glsifcategory{\glslabel}{arg}% if category set to "arg"
{%
\ifdefempty\glsinsert
{\let\glsarg\glsdefaultarg}%
{%
\let\glsarg\glsinsert
\let\glsinsert\empty
}%
}%
{}%
}

\begin{document}

$\gls{BetragVektor}[1]$

$\gls{BetragVektor}$

$\gls{BetragVektor}[1]$

$\gls{BetragVektor}[2]$

$\gls{BetragVektor}[]$

$\gls{s:tP}[1]$

$\gls{s:tP}$

$\gls{s:tP}[1]$

$\gls{s:tP}[2]$

$\gls{s:tP}[]$

\printglossaries
\end{document}


Now you just need to tag which entries require an argument by using category=arg in the entry definition. You can also store the default argument, if it should be different for each entry. For example:

\documentclass{scrbook}
%
\usepackage[abbreviations]{glossaries-extra}

\newglossary[slg]{symbolslist}{syi}{syg}{List of Symbols}
\makeglossaries
\glsnoexpandfields

\newcommand*{\glsarg}{}

\newglossaryentry{BetragVektor}{
category=arg,% requires an argument
user1={i},
name=\ensuremath{|\overline{u_i}|},
text=|\overline{u_{\glsarg}}|,
description={ABC}}
\newglossaryentry{s:tP}{
category=arg,% requires an argument
user1={p},
name=\ensuremath{T_p\mathcal M},
text=T_\glsarg\mathcal M,
description={},
type=symbolslist %in own list
}
% modify the entry's format

\preto\glsentryfmt{%
\glsifcategory{\glslabel}{arg}% if category set to "arg"
{%
\ifdefempty\glsinsert
{\glsfieldfetch{\glslabel}{useri}{\glsarg}}%
{%
\let\glsarg\glsinsert
\let\glsinsert\empty
}%
}%
{}%
}

\begin{document}

$\gls{BetragVektor}[1]$

$\gls{BetragVektor}$

$\gls{BetragVektor}[1]$

$\gls{BetragVektor}[2]$

$\gls{BetragVektor}[]$

$\gls{s:tP}[1]$

$\gls{s:tP}$

$\gls{s:tP}[1]$

$\gls{s:tP}[2]$

$\gls{s:tP}[]$

\printglossaries
\end{document}


## Method 4: Specific Entry (glossaries-extra)

Here's another method: use \glsxtrfmt for the cases where a parameter is required. This deviates from the concept of an optional parameter, but it's worth considering as it doesn't alter the normal display format.

A designated field (by default user1, but may be changed by redefining \GlsXtrFmtField) stores the name (without the leading backslash) of a command that takes a single mandatory argument that's associated with the formatting of the given entry. Then \glsxtrfmt[options]{label}{parameter} applies that command to parameter (encapsulated by \glslink to index and, if enabled, hyperlink to the glossary).

For example, define:

\newcommand{\vectorlength}[1]{|\overline{u_{#1}}|}
\newcommand{\tP}[1]{T_#1\mathcal{M}}


Then the entries can be defined as:

\newglossaryentry{BetragVektor}{
user1={vectorlength},
name=\ensuremath{\vectorlength{i}},
description={ABC}}

\newglossaryentry{s:tP}{
user1={tP},
name=\ensuremath{\tP{p}},
description={},
type=symbolslist
}


So now \gls{BetragVektor} uses the value of the first/text field (as normal, which here is obtained from the name field) but \glsxtrfmt{BetragVektor}{1} is essentially like \glslink{BetragVektor}{\vectorlength{1}}

Modified MWE:

\documentclass{scrbook}

\usepackage[automake]{glossaries-extra}

\newglossary[slg]{symbolslist}{syi}{syg}{List of Symbols}

\makeglossaries
\glsnoexpandfields

\newcommand{\BetragVektor}[1]{|\overline{u_{#1}}|}
\newcommand{\tP}[1]{T_#1\mathcal{M}}

\newglossaryentry{BetragVektor}{
user1={BetragVektor},
name=\ensuremath{\BetragVektor{i}},
description={ABC}}

\newglossaryentry{s:tP}{
user1={tP},
name=\ensuremath{\tP{p}},
description={},
type=symbolslist %in own list
}

\begin{document}

$\glsxtrfmt{BetragVektor}{1}$

$\gls{BetragVektor}$

$\glsxtrfmt{BetragVektor}{1}$

$\glsxtrfmt{BetragVektor}{2}$

$\gls{BetragVektor}$

$\glsxtrfmt{s:tP}{1}$

$\gls{s:tP}$

$\glsxtrfmt{s:tP}{1}$

$\glsxtrfmt{s:tP}{2}$

$\gls{s:tP}$

\printglossaries

\end{document}


There are some more examples in Section 3.1 "Functions" of glossaries-extra and bib2gls: An Introductory Guide (that section is general to glossaries-extra regardless of whether or not you're using bib2gls).

¹ It's a little more complicated: \glsxtrfmt[options]{label}{text} effectively does \glslink[default options,options]{label}{\glsxtrfmtdisplay{csname}{text}{}} where csname is the control sequence name obtained from the designated field. (If the field is unset csname defaults to @firstofone.)

The starred version \glsxtrfmt*[options]{label}{text}[insert] is effectively \glslink[default options,options]{label}{\glsxtrfmtdisplay{csname}{text}{insert}}.

• Oh, that was an easy fix - actually it's enough to have it for just my symbolslist, so having the one list entry fixed already solves the problem. Thanks for the fast (and so simple) answer :) – Ronny Jun 27 '16 at 10:09
• @Ronny I added glossaries-extra alternative for completeness. – Nicola Talbot Jun 27 '16 at 10:17
• Thanks, looks like a safer version, because only some (all of one category) are extended by an optional argument and not all symbols. – Ronny Jun 27 '16 at 13:46

I have chosen the third option as mentioned in Nicola's answer and it works like charm!

However there are a few remarks I would like to make regarding the syntax which weren't apparent to me and I had to experiment a little to make it work as expected. So:

1. You can build other glossary entries on top of more primitive ones simply by calling \gls{<primitive entry name>} inside the respective field (see example; i guess that works for the whole glossaries package).
2. You can pass the argument from the more complicated glossary entry to the more primitive one (category=arg has to be set in the complicated entry as well for you to be able to pass the argument).
• the argument is passed implicitly (see example)
3. And a few other things mentioned in the comments of the example

# Example

\documentclass{article}

\usepackage[automake]{glossaries-extra}

\makeglossaries
\glsnoexpandfields

\newcommand*{\glsarg}{}

\newglossaryentry{simple}{
category=arg,% requires an argument
user1={a},% default argument for \glsarg
name={$$A_{i}$$},
text={A_{\glsarg}},
description={simpler}}
\newglossaryentry{simple2}{
category=arg,% requires an argument
user1={b},
name={$$B_{\glsarg}$$},% \glsarg has no effect here nor does user1=b, it has to be B_{b} to appear that way in the glossary, this will evaluate to just $$B$$
text={B_{\glsarg}},% \glsarg works as usual here
description={simpler}}
\newglossaryentry{complex}{
category=arg,% required to require an argument
name={$$C_{\gls{simple}[k]}$$},% will evaluate as $$C_{A_k}$$ in the glossary
text={C_{\gls{simple}}},% \glsarg will be passed to \gls{simple} without mentioning it here
description={more complex. You can use $$\gls{simple2}$$ here as well}}

\preto\glsentryfmt{%
\glsifcategory{\glslabel}{arg}%
{%
\ifdefempty\glsinsert
{\glsfieldfetch{\glslabel}{useri}{\glsarg}}%
{%
\let\glsarg\glsinsert
\let\glsinsert\empty
}%
}%
{}%
}

\begin{document}

$\gls{simple}$% shows as A_a
$\gls{simple}[i]$% shows as A_i
$\gls{simple2}[j]$% shows as B_j
$\gls{complex}[k]$% shows as C_{A_k}

\printglossaries
\end{document}

• Actually, you shouldn't nest \gls inside of descriptions of other entries. – user31729 Nov 13 '17 at 15:17
• As a supplement to @ChristianHupfer's comment, see the section Nested Links in the glossaries-extra documentation. – Nicola Talbot Nov 13 '17 at 17:37
• @VaNa As long as you group it and have hyper=false (e.g. {\gls*{simple}[k]}) and don't use it for abbreviations (or something that has different first and text values) and you don't need the corresponding \glsentry... to be expandable (which it won't be anyway, since the values are in math-mode) and it doesn't interfere with the case-changing commands like \Gls (which aren't applicable in this case) then you should be okay. Suppressing the indexing isn't quite such a problem (unless you feel it inappropriate). The other issues are the primary concerns. – Nicola Talbot Nov 13 '17 at 18:51
• It might be easier to have a wrapper command, for example \newcommand{\ngls}[2]{{\gls[hyper=false]{#1}[#2]}} and use that (e.g. name={$$C_{\ngls{simple}{k}}$$}, text={C_{\ngls{simple}{}}}). – Nicola Talbot Nov 13 '17 at 18:54
• @cfr Just to confirm: it's okay to have \gls in the description field if you don't use \glsdesc and if you don't try using \glsentrydesc in the final optional argument of arguments like \gls (or insert the description in any other way into text produced by commands like \gls, for example in \glsentryfmt). When I read Christian's initial comment, I interpreted "descriptions" as meaning field values in general (such as the text field, which will cause a problem) rather than specifically the value of the description field. My concern was focused on text={C_{\gls{simple}}}. – Nicola Talbot Dec 20 '17 at 11:09