6

I would like to automatically generate commands like

\newcommand{\Air}{\gls{air}}
\newcommand{\Earth}{\gls{earth}}
...

to use with glossaries package, without using \newcommand for every entry of the glossary. Is this possible and how?

Stating this differently, is it possible to automatically create commands of the type \Abbr where Abbr is the uppercase form of the abbr argument in

\newglossaryentry{abbr}{name=...}
2
  • You could simply wrap the newglossaryentry macro in something that defines two new macros (upper and lowercase) and then does the original newglossaryentry? Nov 2, 2013 at 20:03
  • I have no idea how to do this.
    – Stefano
    Nov 2, 2013 at 20:20

3 Answers 3

7

Assuming that you want \Abbr to print the uppercased contents of the name field of the abbr glossary entry, this is a way.

I've used the trick described by egreg in this answer to define \Abbr.

\documentclass{article}

\usepackage{glossaries}

\let\oldglossaryentry\newglossaryentry
\renewcommand{\newglossaryentry}[2]{%
\def\tempname##1##2\relax{%
  \uppercase{\expandafter\gdef\csname ##1}##2\endcsname{\MakeUppercase{\glsentrytext{##1##2}}}%
}%
\tempname#1\relax%
\oldglossaryentry{#1}{#2}}

\newglossaryentry{abbr}
{
  name={abbreviation},
  description={description of abbreviation},
}

\makeglossaries

\begin{document}

\Abbr

\printglossaries
\end{document} 
6
  • Aah, the uppercase group can end halfway through the csname, that's good to know. Makes sense, actually. Thanks :) Nov 3, 2013 at 9:53
  • Out of curiosity, is there any reason you are going through an extra layer of macro? That is, it would work just as wel if you left out tempname and used \tempnameaux{#1} right before adding the old glossary entry, wouldn't it? Nov 3, 2013 at 10:10
  • @RoelofSpijker You are right. Answer edited. Thanks for pointing it out :-) Nov 3, 2013 at 10:43
  • I don't need to \MakeUppercase it, but this answer is what I was looking for. I just need the uppercase form in order to prevent clash with other commands that may be already defined. The text will be in smallcaps so \gls{##1##2} will do. I wanted to emulate the leipzig` package behaviour in all that. Thanks.
    – Stefano
    Nov 3, 2013 at 11:49
  • @Stefano Happy that helped. Adapt it to your needs, anyway I would suggest \textsc{\glsentrytext{##1##2}}. Nov 3, 2013 at 12:04
7

As extension of Roelof's answer:

One can use the LaTeX helper macros \@car and \@cdr to extract the 1st letter of a given name, for example:

\documentclass[12pt]{article}

\makeatletter
\providecommand*\newfloat@capitalize[2]{%
  \edef\newfloat@tempa{\gdef\noexpand#1{\@car#2\@nil}}%
  \uppercase\expandafter{\newfloat@tempa}%
  \edef\newfloat@tempa{%
    \noexpand\g@addto@macro\noexpand#1{\@cdr#2\@nil}}%
  \newfloat@tempa}
\newcommand*\mynewglos[2]{%
  \newglossaryentry{#1}{#2}%
  \@namedef{#1}{\gls{#1}}%
  \newfloat@capitalize\@tempa{#1}%
  \@namedef{\@tempa}{\Gls{#1}}%
}
\makeatother

\usepackage{glossaries}
\makeglossaries{}
\begin{document}
\mynewglos{electrolyte}{name=electrolyte,description={solution able to conduct electric current}}
\gls{electrolyte} or \electrolyte{} or \Gls{electrolyte} or \Electrolyte
\printglossaries
\end{document}

Some notes:

  • \@car and \@cdr and their usage are documented in the LaTeX source.
  • \newfloat@capitalize is taken from my newfloat package and defines the 1st argument (=command) to the 2nd argument (=name) but with 1st letter capitalized. (It is defined with \providecommand so using the newfloat package in the document does not make any harm. As alternative one could give that macro a new name, or integrate it into the definition of \mynewglos which would eliminate the need of a helper macro.)
  • The (helper) macro, in this case \@tempa will be defined globally. This is caused by the usage of the LaTeX helper macro \g@addto@macro which has no companion macro \l@addto@macro which does the same thing but locally. (But \l@addto@macro is offered by many LaTeX packages, e.g. the KOMA-Script document classes.) As an alternative one could use the eTeX primitive \unexpanded for doing the same thing without helper macro, see the definition of \l@addto@macro provided by KOMA-Script as an example.
6

Creating a macro from a parameter where the first letter has to be capitalized turns out to be extremely difficult (or I am doing something wrong). It comes down to the fact that \uppercase is not expandable. And \uppercase{\csname #1\endcsname} would capitalize the entire word...

The easiest thing that I can think of is to give the uppercase word as a parameter as well. That is, you could use the following:

\documentclass[12pt]{article}
\def\mynewglos#1#2#3{%
  \newglossaryentry{#1}{#3}%
  \expandafter\def\csname #1\endcsname{\gls{#1}}%
  \expandafter\def\csname #2\endcsname{\Gls{#1}}%
}
\usepackage{glossaries}
\makeglossaries{}
\begin{document}
\mynewglos{electrolyte}{Electrolyte}{name=electrolyte,description={solution able to conduct electric current}}
\gls{electrolyte} or \electrolyte{} or \Gls{electrolyte} or \Electrolyte
\printglossaries
\end{document}

Note that this defines \Abbr as the capitalized version \Gls instead of the regular version. You can simply change that in the definition containing #2. If it is not required, you can also remove the lowercase definitions by removing the definition containing #1.

If someone has a good way to define \Abbr from a macro where only abbr is passed as an argument, I would very much like to see it :)

You must log in to answer this question.

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