This is kind of a double question but closely connected in my case. Inspired by https://tex.stackexchange.com/a/55769/14159 I tried to make an improved version which allows additional parameters for the defined symbols.
This is my current code:
\documentclass{article}
\usepackage{xparse}
\usepackage{nomencl}
\usepackage{xcolor}
% colorize to distinguish original and renewed command
\NewDocumentCommand{\defsym}{mmmmm} {%
\NewDocumentCommand{#1}{#2}{%
\RenewDocumentCommand{#1}{#2}{\textcolor{blue}{#4}}
\nomenclature{$#1$}{#5}
\textcolor{red}{#3}
}
}
\defsym\Uk{O{k}}{U_{#1}}{U_{##1}}{Some variable}
\makenomenclature
\begin{document}
\noindent$\Uk[j]$,$\Uk,$ $\Uk[p], \Uk[q]$ and $\Uk[r]$
\printnomenclature
\end{document}
Now to the two problems I have are
- The double definition
#3
and#4
is very ugly, is there some macro which increases the nesting level, i.e. which adds one # to each parameter? So that I could write\incnest{#3}
instead of#4
- The
\RenewDocumentCommand
definition is not global so all U's except the\Uk[q]
are red. Is there some equivalent to\global
or\gdef
in xparse. I found something similar for\renewcommand
in \global\renewcommand equivalent of \global\def but it's a bit hacky.
Any tricks or workarounds to get it working are welcome.