# New macro and glossary entry in a command

I want to unify the creation of a macro and the addition of a new entry to the glossary in a single command using the glossaries package. A call to the command would ideally look like

\mycommand{powerset}{\operatorname{Pow}(#1)}{A}{Power set of A}


The first argument specifies the name of the new macro: \powerset, the second is the definition of this macro. In this case, \powerset expects one argument and produces as result \operatorname{Pow}(#1).

The last two arguments are related with the glossary entry. The first argument is the default parameter for the newly created macro. The name of the glossary entry should be the macro called with the default parameter - in math mode. In this case, \powerset{A}. Finally, the last argument is the description of the glossary entry. In this example, the glossary entry should look like

\newglossaryentry{powerset}
{
name=\ensuremath{\operatorname{Pow}(A)},
description=Power set of A
}


Any ideas? Also, how hard is to extend this process to include a specified amount of parameters like

\mycommand{complement}{2}{#1 \setminus #2}{A}{B}{The complement of B in A}?


The motivation under the question is to provide the ability to define macros that will be added to the glossary after first use with the proper link to the page where they have been first used.

• The first part of the question is possible of course, the second is very complex ('arbitrarily many parameters'), but I don't think you will gain much by such an approach. You define \powerset for which purpose? – user31729 Aug 28 '17 at 22:59
• Your last request is possible, but I wouldn't recommend it, and am not sure how to implement it, atm. – Skillmon Aug 28 '17 at 23:00
• @Skillmon We can focus on solving the original question for now. The arbitrarily many parameters one is just an extra. – Martin Azpillaga Aug 28 '17 at 23:05

This might do (I never used the glossaries package before, so don't know what to test):

\documentclass[]{article}

\usepackage{glossaries}
\usepackage{xparse}

\NewDocumentCommand{\mycommand}{m m m m}{%
\expandafter\newcommand\expandafter{\csname #1\endcsname}[1]{#2}%
\newglossaryentry{#1}{%
name=\ensuremath{\csname #1\endcsname{#3}},%
description=#4%
}%
}

\makeglossaries

\mycommand{powerset}{\operatorname{Pow}(#1)}{A}{Power set of A}

\begin{document}
\printglossary[style=altlist,title=Glossar]
\gls{powerset}
$\powerset{c}$
\end{document}


The following provides the possibility of optionally adding two more arguments (but you might encounter problems with grouping after that command; also the code might not be pretty)

\NewDocumentCommand{\mycommand}{m m m m g g}{%
\IfNoValueTF{#6}{%
\IfNoValueTF{#5}{%
\expandafter\newcommand\expandafter{\csname #1\endcsname}[1]{#2}%
\newglossaryentry{#1}{%
name=\ensuremath{\csname #1\endcsname{#3}},%
description=#4%
}%
}%
{%
\expandafter\newcommand\expandafter{\csname #1\endcsname}[2]{#2}%
\newglossaryentry{#1}{%
name=\ensuremath{\csname #1\endcsname{#3}{#4}},%
description=#5%
}%
}%
}%
{%
\expandafter\newcommand\expandafter{\csname #1\endcsname}[3]{#2}%
\newglossaryentry{#1}{%
name=\ensuremath{\csname #1\endcsname{#3}{#4}{#5}},%
description=#6%
}%
}%
}

• \makeglossaries is missing before \mycommand usage – user31729 Aug 28 '17 at 23:12
• @ChristianHupfer as I said, I have no idea of how to use that package :) – Skillmon Aug 28 '17 at 23:13
• Does 'xparse' provide any features that can not be achieved with regular LaTeX code in this case? – Martin Azpillaga Aug 28 '17 at 23:14
• @MartinAzpillaga: I think in this case you could use \newcommand as well instead of the \NewDocumentCommand – user31729 Aug 28 '17 at 23:15
• @MartinAzpillaga I've added one more optional argument delimited by {}. And here one can see one of the additional features of xparse... Note that the g type argument is not recommended to use! – Skillmon Aug 28 '17 at 23:22