I'm trying to use xparse and generic TeX/LaTeX commands to write a macro (that takes no arguments) to define another macro that does take arguments:
\documentclass{minimal}
\RequirePackage{xparse,fmtcount}
\newcounter{mycounter}
\NewDocumentCommand{\mycounterHex}{O{1}}{}
\providecommand{\enableCounterCommands}{
\RenewDocumentCommand{\mycounterHex}{O{1}}{\padzeroes[#1]{\hexadecimal{mycounter}}}
}
\begin{document}
\end{document}
When I compile this document with pdflatex --interaction=nonstopmode, I get the following error message:
! Illegal parameter number in definition of \enableCounterCommands.
<to be read again>
1
l.11 }
I believe that the problem is that TeX thinks I'm trying to refer to the (nonexistent) first argument of \enableCounterCommands, but I'm actually trying to reference the optional-with-default-value first argument of \mycounterHex. How can I indicate to TeX that I am referencing the argument of \mycounterHex?

#?...\padzeroes[##1]...– gernot Jun 30 '23 at 16:17#seems to work well. Do you know ifxparse's\RenewDocumentCommandcan be used to make a global redefinition from inside of another macro definition? – Ben Zelnick Jun 30 '23 at 16:44