I'm creating a lot of commands dynamically in a very big LaTeX project, and i want to ensure these commands are only called once in the entire project. I can already create single-use commands thanks to this code (which i included in the MWE) : https://tex.stackexchange.com/a/410816/235171.
How would one exploit it to create single-use commands which names are created via a parameter within another command ? I also want my commands to be with a leading backslash.
MWE :
\documentclass{article}
% Preprocessor code
\usepackage{xparse}
\ExplSyntaxOn
\cs_set_protected:Nn \my_optional_processor:n
{
\str_if_eq:nnTF { #1 } { \use:c { c_novalue_tl } }
{ \def \ProcessedArgument {} }
{ \def \ProcessedArgument { [#1] } }
}
\ExplSyntaxOff
\newcommand\MyStarredProcessor[1]{%
\IfBooleanTF{#1}
{\def\ProcessedArgument{}}
{\def\ProcessedArgument{}}}
\ExplSyntaxOn
\NewDocumentCommand{\newonetimecommand}
{ t!
>{\MyStarredProcessor}s
m
>{\my_optional_processor:n}O{ \use:c { c_novalue_tl } }
>{\my_optional_processor:n}+O{ \use:c { c_novalue_tl } }
+m }{%
\newcommand#2#3#4#5{%
#6%
\IfBooleanTF{#1}{\gdef}{\def}#3{%
\GenericError{}
{Error:~\string#3~used~twice!}
{Command~was~setup~to~be~usable~only~once}{}%
}%
}}
\ExplSyntaxOff
% Failed attempt
\newcommand{\createCommand}[1]{%
\expandafter\newonetimecommand!*\csname\string#1\endcsname{
I am writing #1 !
}
}
\begin{document}
\createCommand{foo} % Creates \foo
\foo % Can be called once
\foo % Should produce an error thanks to the use of \newonetimecommand!*
\end{document}
Produced error :
Command \csname already defined. Or name \end... illegal
\expandafter\newonetimecommand!expands!which is not expandable,\expandafter\newonetimecommand\expandafter!\expandafter*\csnameexpands\csname– David Carlisle Apr 19 '23 at 23:50