6

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.

4
  • I don't understand what you mean by "writes the 1 at the end". The end of what? (The document?) What does the 1 represent? Do you mean that you want a command that automatically does both \gls{BetragVektor} and \index{$|\overline{u_2}|$}? Feb 24, 2015 at 11:36
  • No - sorry for the cryptic text. I want to define one variable like \newglossaryentry{c} {name=\ensuremath{\vec{c}},description={speed}} However I have \gls{c} three times with the indizes 1...3 in my document. The command \gls{c}{_1} writes the _1 at the end of c - but not under the arrow. The same problem is with: \newglossaryentry{cc} {name=\ensuremath{|c|},description={magnitude of c}} The command \gls{cc}{_1} writes the 1 at the end (|c|1) and not |c_1|. Do you know what I mean?
    – Franz2304
    Feb 24, 2015 at 11:59
  • Okay, I understand what you mean now. Feb 24, 2015 at 12:05
  • Thank you for your help. At the moment I have defined c and write everytime $\vec{\gls{c}_1}$. That's ok but not nice and it is not really more simple. Thank you for your trouble.
    – Franz2304
    Feb 24, 2015 at 12:22

1 Answer 1

6

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

3
  • All isn't perfect :-) If you call \gls{label} a second time without an optional argument, you don't get the default index. Another mistake is, if the first call is with an argument latex writes the default index. $\gls{BetragVektor}$ $\gls{BetragVektor}$ or $\gls{BetragVektor}[1]$ $\gls{BetragVektor}$
    – Franz2304
    Feb 24, 2015 at 16:12
  • @Franz2304 I've updated my answer. Feb 24, 2015 at 18:06
  • I have no clue how it works - but it works fine. Thank you very much.
    – Franz2304
    Feb 26, 2015 at 7:50

You must log in to answer this question.

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