[Tex/LaTex] Glossary entry with extra parameter

glossariessymbols

I want to create a list of symbols for my work, where the symbols are often provided with an index. Now I do not want to create a new entry for each symbol and index. Is it possible with another parameter in the call \ gls {\label, {#1}} the index (#1) put to the desired location. If I have expressed myself too cryptic, may help the minimal example:

\documentclass{scrreprt}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[ansinew]{inputenc}
\usepackage{glossaries}
\makeglossaries

\newglossaryentry{BetragVektor1}{name=\ensuremath{|\overline{u_1}|},description={Länge des Vektors}}
\newglossaryentry{BetragVektor2}{name=\ensuremath{|\overline{u_2}|},description={Länge des Vektors}}
\newglossaryentry{BetragVektor}{name=\ensuremath{|\overline{u_2}|},description={Länge des Vektors}}


\begin{document}

target:\\
The vector $|\overline{u_1}|$ is longer than the vector $|\overline{u_2}|$.

option1:\\
The vector \gls{BetragVektor1} is longer than the vector \gls{BetragVektor2}.

option2(gloosarie-documentation) :\\
\gls{BetragVektor}{1} 
This option writes the 1 at the end - not behind u.

\end{document} 

Thanks for all the advice and I hope, there is a solution.

Best Answer

Here's one possible solution that uses the final optional argument of commands like \gls:

\documentclass{scrreprt}
\usepackage{glossaries}
\makeglossaries

\glssetnoexpandfield{text}% don't expand text field when defining an entry

\newcommand{\symbolidx}{i}% default index

\newglossaryentry{BetragVektor}{
 name=\ensuremath{|\overline{u_\symbolidx}|},
 text=|\overline{u_\symbolidx}|,
 description={}}

% modify the entry's format

\defglsentryfmt{%
 \let\symbolidx\glsinsert
 \def\glsinsert{}%
 \glsgenentryfmt
}

\begin{document}

$\gls{BetragVektor}$

$\gls{BetragVektor}[1]$

$\gls{BetragVektor}[2]$

\printglossaries
\end{document} 

This produces:

Image of result

However, you won't be able to use this optional argument for any of your other glossary entries.

Edit:

Sorry, I forget that you also need to prevent the expansion of the first key. Here's an updated version where I've switched off expansion using \glsnoexpandfields:

\documentclass{scrreprt}
\usepackage{glossaries}
\makeglossaries

\glsnoexpandfields

\newcommand*{\glsarg}{i}

\newglossaryentry{BetragVektor}{
 name=\ensuremath{|\overline{u_i}|},
 text=|\overline{u_\glsarg}|,
 description={}}

% 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}[]$

\printglossaries
\end{document} 

Image of document

Related Question