For a package I'm writing, I would like to provide a customised way to create a new command. The idea is that the user will write something like
\DeclareFancyThing{\myfancything}{parameter1}{parameter2}
and then this will create a command \myfancything, which itself takes an argument or two.
I was pleased and surprised to find that the following MWE does actually work, and produces the words "fancy foo!" as expected:
\documentclass{article}
\newcommand{\mynewcommand}[2]{%
\newcommand{#1}{fancy #2!}%
}
\mynewcommand{\foo}{foo}
\begin{document}
\foo
\end{document}
However, I can't work out how to make it so that the generated command \foo itself takes an argument. Is there a way to do that?
The following might be expected to produce the words "fancy foo (bar)!", but it in fact gives the error "TeX capacity exceeded, sorry [input stack size=10000]."
\documentclass{article}
\newcommand{\mynewcommand}[2]{%
\newcommand{#1}[1]{fancy #2 (#1)!}%
}
\mynewcommand{\foo}{foo}
\begin{document}
\foo{bar}
\end{document}
I guess the issue is that the #1 refers to the first argument of \mynewcommand instead of the argument of \foo, but I don't know how to change that.
Edit: The question is rightly closed, so I can't post an answer - but in order to help future visitors, in case the comments get deleted, the following works fine and produces "fancy foo (bar)!".
\documentclass{article}
\newcommand{\mynewcommand}[2]{%
\newcommand{#1}[1]{fancy #2 (##1)!}%
}
\mynewcommand{\foo}{foo}
\begin{document}
\foo{bar}
\end{document}
#, i.e.,##1. For example\newcommand{\mynewcommand}[2]{ \newcommand{#1}[1]{fancy #2! ##1!} }works. But why would you want to do that when\newcommandalready exists? – Marijn Aug 12 '22 at 06:39\DeclareFancyThing{\foo}{1}{7}{14.2}{8}{hello}and then after that just write\foo{42}, instead of having to write\fancyThing{42}{1}{7}{14.2}{8}{hello}every time they use the command. Of course the user can write\newcommand{\foo}[1]{\fancyThing{#1}{1}{7}{14.2}{8}{hello}}themself, but since every document will contain several such lines I wanted to automate it. – N. Virgo Aug 12 '22 at 06:43###1,####1etc. for deeper nesting). – Marijn Aug 12 '22 at 06:53##you are missing%at ends of lines, your definition is creating multiple space tokens each time it is used – David Carlisle Aug 12 '22 at 08:02###1is nonsense. We have##1for first level of nesting,####1for second etc. We must to double all#in each next level. – wipet Aug 12 '22 at 08:16