0

My glossary is full of mathematical and technical things that have parameters, indices etc. A similar case was wonderfully solved at 1.

However as the number and type of my indices varies from term to term i would like to have multiple args \glsargA, \glsargB,.. which could be fetched from \glsinsert and to define the defaults specifically for each gls-entry.

I tried to merge in code from 2, but since I don't understand the mechanics of TeX I had no luck.

Non-working example:

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

\newglossaryentry{Upara}{
 name=\ensuremath{u_{\glsargA}^{\glsargB}},
 defaultargA=one,
 defaultargB=two,
 description={u}
 }
\newglossaryentry{Xpara}{
 name=\ensuremath{x_{\glsargB}^{\glsargA}},
 defaultargA=I,
 defaultargB=II,
 description={x}
 }

% magic might go here...

\defglsentryfmt{%
...
}

\begin{document}

$\gls{Upara}$

$\gls{Xpara}$

$\gls{Upara}[{aha}{b}]$

$\gls{Xpara}[{aha}{b}]$

$\gls{Upara}$

$\gls{Xpara}$


\printglossaries
\end{document} 
Fabian
  • 33

1 Answers1

2

This is an extension of Nicola Talbot's code.

Option 1

This uses something closer to the requested syntax but does not do everything you want. However, it does do some of it. It allows you to set up 'filler' place-holders separately for one and two-place entries. Although I've used i for the one-place and i and ii for the two-place, you could use i for the former and kangaroos and elephants for the latter, if you wished. However, you cannot specify these on a per-entry basis.

The required syntax is \gls{<entry>}[<single argument>] or \gls{<entry>}[<first argument>,<second argument>].

\documentclass{scrreprt}
\usepackage{glossaries}
\makeglossaries
\glsnoexpandfields
\newcommand*{\glsarg}{i}
\newcommand*\glsargA{i}
\newcommand*\glsargB{ii}

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

\newglossaryentry{Upara}{
 name=\ensuremath{u_{i}^{ii}},
 text=\ensuremath{u_{\glsargA}^{\glsargB}},
 description={u}
 }
\newglossaryentry{Xpara}{
 name=\ensuremath{x_{ii}^{i}},
 text=\ensuremath{x_{\glsargB}^{\glsargA}},
 description={x}
 }

% modify the entry's format
\makeatletter
\def\before@comma#1,#2\@nil{#1}
\def\after@comma#1,#2\@nil{#2}
\defglsentryfmt{% based on Nicola Talbot's code at https://tex.stackexchange.com/a/229879/
  \let\orgglsargA\glsargA
  \let\orgglsargB\glsargB
  \let\orgglsarg\glsarg
  \ifdefempty\glsinsert
  {}%
  {%
    \edef\tempa{\expandafter\before@comma\glsinsert,\@nil}%
    \edef\tempb{\glsinsert}%
    \ifx\tempa\tempb
      \let\glsarg\glsinsert
    \else
      \def\glsargA{\expandafter\before@comma\tempb\@nil}%
      \def\glsargB{\expandafter\after@comma\tempb\@nil}%
    \fi
    \let\glsinsert\relax
  }%
  \glsgenentryfmt
  \let\glsargA\orgglsargA
  \let\glsargB\orgglsargB
  \let\glsarg\orgglsarg
}
\makeatother

\begin{document}

$\gls{BetragVektor}[1]$

$\gls{BetragVektor}$

$\gls{BetragVektor}[1]$

$\gls{BetragVektor}[2]$

$\gls{BetragVektor}[]$

$\gls{Upara}$

$\gls{Xpara}$

$\gls{Upara}[aha,b]$

$\gls{Xpara}[aha,b]$

$\gls{Upara}$

\printglossaries
\end{document}

one- and two-place glossary entries

Option 2

This moves further from the requested syntax by defining a new command \mygls{}[]. The syntax is otherwise the same as in the solution above, but the advantage is of a new command is that you can get different default values for different two-place or different one-place entries.

This solution uses the package xparse for more fluent handling of the optional argument.

\documentclass{scrreprt}
\usepackage{glossaries,xparse}
\makeglossaries
\glsnoexpandfields
\newcommand*{\glsarg}{i}
\newcommand*\glsargA{i}
\newcommand*\glsargB{ii}

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

\newglossaryentry{Upara}{
 name=\ensuremath{u_{one}^{two}},
 text=\ensuremath{u_{\glsargA}^{\glsargB}},
 description={u}
 }
\newglossaryentry{Xpara}{
 name=\ensuremath{x_{II}^{I}},
 text=\ensuremath{x_{\glsargB}^{\glsargA}},
 description={x}
 }

% modify the entry's format
\makeatletter
\def\before@comma#1,#2\@nil{#1}
\def\after@comma#1,#2\@nil{#2}
\defglsentryfmt{% based on Nicola Talbot's code at https://tex.stackexchange.com/a/229879/
  \let\orgglsargA\glsargA
  \let\orgglsargB\glsargB
  \let\orgglsarg\glsarg
  \ifdefempty\glsinsert
  {}%
  {%
    \edef\tempa{\expandafter\before@comma\glsinsert,\@nil}%
    \edef\tempb{\glsinsert}%
    \ifx\tempa\tempb
      \let\glsarg\glsinsert
    \else
      \def\glsargA{\expandafter\before@comma\tempb\@nil}%
      \def\glsargB{\expandafter\after@comma\tempb\@nil}%
    \fi
    \let\glsinsert\relax
  }%
  \glsgenentryfmt
  \let\glsargA\orgglsargA
  \let\glsargB\orgglsargB
  \let\glsarg\orgglsarg
}
\NewDocumentCommand\mygls { m o }{%
  \IfNoValueTF {#2}{%
    \glossentryname{#1}%
  }{%
    \gls{#1}[#2]%
  }%
}
\makeatother

\begin{document}

$\mygls{BetragVektor}[1]$

$\mygls{BetragVektor}$

$\mygls{BetragVektor}[1]$

$\mygls{BetragVektor}[2]$

$\mygls{BetragVektor}[]$

$\mygls{Upara}$

$\mygls{Xpara}$

$\mygls{Upara}[aha,b]$

$\mygls{Xpara}[aha,b]$

$\mygls{Upara}$

\printglossaries
\end{document}

new command for greater flexibility

cfr
  • 198,882