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?
Best Answer
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: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.)This produces:
Method 3: Specific Category (
glossaries-extra
)If you want to use the extension package
glossaries-extra
, this is a better method: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: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:
Then the entries can be defined as:
So now
\gls{BetragVektor}
uses the value of thefirst
/text
field (as normal, which here is obtained from thename
field) but\glsxtrfmt{BetragVektor}{1}
is essentially like\glslink{BetragVektor}{\vectorlength{1}}
.¹Modified MWE:
There are some more examples in Section 3.1 "Functions" of
glossaries-extra
andbib2gls
: An Introductory Guide (that section is general toglossaries-extra
regardless of whether or not you're usingbib2gls
).¹ 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}}
.