With glossaries-extra, I would like to generate macros for each glossary term to improve readability of the latex sources. I find it difficult to read text that looks like this (especially if it is a longer text):
\Glspl{pig} or \gls{pig}? \Gls{cat} or \glspl{cat}?
What's distracting me is that capitalization and number are in the command, not in the term. Ideally, the line would read,
\Pigs or \pig? \Cat or \cats?
However, that does not work because it causes trouble with the next whitespace. To deal with that problem my preferred choice is \Pigs/.
Generating macros of that form is possible using \edef in the <code> part of \glsforeachincategory{<category-list>}{<category>}{<label>}{<code>}. Making macros for plural and singular is easily achieved by appending an s to the label.
Two problems remain:
Depending on the terms, there may be clashes with existing command names. The solution should have minimal impact both visually and in terms of character count. My current solution is to prepend
xto the label, yielding\xPigs/, but I am not happy because there may still be clashes and readability could be better. Prepending_(\_Pigs/) or.(\.Pigs/), which would be better readable, do not work. Any suggestions?The capitalization of the term is what I cannot get to work at all. My current solution is to capitalize the prepended
x, i.e.\Xpigs/. So the question is: How to generate a macro named\xPigs/through\defGlspl#1(see below) when the argument expands topig?
More general remarks or alternative approaches are welcome, too!
\documentclass{article}
\usepackage[automake]{glossaries-extra}
% two glossary entries, pig and cat, for our example.
\newglossaryentry{pig}
{
name={pig},
description={A pig is any of the animals in the genus Sus.},
category={animals}
}
\newglossaryentry{cat}
{
name= {cat},
description= {The cat (Felis catus) is a small carnivorous mammal.},
category={animals},
}
\makeglossaries
% macros defining macros that
% * expand to a glossary command (e.g. \gls)
% * do not inhibit readability (regarding capitalization and singular/plural))
\def\defgls#1{\expandafter\edef\csname x#1\endcsname/{\gls{#1}}}
\def\defGls#1{\expandafter\edef\csname X#1\endcsname/{\Gls{#1}}}
\def\defglspl#1{\expandafter\edef\csname x#1s\endcsname/{\glspl{#1}}}
\def\defGlspl#1{\expandafter\edef\csname X#1s\endcsname/{\Glspl{#1}}}
% apply the macros for all glossary terms in a category
\glsforeachincategory{animals}{\cat}{\lab}{%
\defgls\lab%
\defGls\lab%
\defglspl\lab%
\defGlspl\lab%
}
%%% My question %%%
% how to change \defGls#1 and \defGlspl#1 above
% to generate the macros below that, for the demo,
% I wrote by hand (for the pig and cat entry)
% (the difference is the capitalization of the macro name)
\def\xPigs/{\Glspl{pig}}
\def\xPig/{\Gls{pig}}
\def\xCats/{\Glspl{cat}}
\def\xCat/{\Gls{cat}}
\begin{document}
% demo
% I find this hard to read and scan for errors
\Glspl{pig} or \gls{pig}? \Gls{cat} or \glspl{cat}?
% Better:
\Xpigs/ or \xpig/? \Xcat/ or \xcats/?
% what I would like:
%% note the difference
%% above:\Xpigs/ below: \xPigs/
%% above:\Xcat/, below:\xCat/)
\xPigs/ or \xpig/? \xCat/ or \xcats/?
\def\Pigs/{Pigs}
\def\pig/{pig}
\def\Cat/{Cat}
\def\cats/{cats}
%ideal, but unrealistic because of possible name-clashes:
\Pigs/ or \pig/? \Cat/ or \cats/?
\printglossaries
\end{document}
H2Oorkm/h? What about math? Won't the use of / as command terminator be confusing there? – Ulrike Fischer Jun 14 '19 at 12:35\xPigs/solution so much because a) it may lead to clashes, as you point out, and b) it isn't great for readability.However, it still works because in case of a clash you can change the label of your term. Also, you can choose which category of terms to apply it to, so if you have a category with a lot of clashes, use the normal approach.
In many cases, the normal approach is ok in terms of readability.
– fkleedorfer Jun 14 '19 at 12:59H2Oandkm/hare such cases, and I would probably not apply the approach to them.