I've created a package which I would like to take in options and turn these into new macros (one new macro for each option). I also want these new macros to produce new macros in turn. For example, calling my package thus:
\usepackage[foo]{mypackage}
Should effectively run the following:
\newcommand{\foo}[1]{%
\def\fooMagic{some handling of #1}
}
My attempt thus far is below - in an example form.
\ProvidesPackage{mypackage}
\RequirePackage{xkeyval}
% Create a macro to create the macro that will create the macros:
\newcommand{\@mypackage@addnewmacro}[1]{%
\expandafter\newcommand\expandafter{\csname #1\endcsname}[1]{%
% Point A is here (for explanatory reasons that will become apparent)
\expandafter\def\csname #1Magic\endcsname{some handling of ##1}
}
}
% Interpret (unrecognised) options supplied to the package as new macro names:
\DeclareOptionX*{%
\@mypackage@addnewmacro{\CurrentOption}
}
% Process the options:
\ProcessOptionsX*
Running this:
\documentclass{report}
\usepackage[foo]{mypackage}
\foo{bar}
\begin{document}
\fooMagic
\end{document}
Should have printed:
some handling of bar
But instead it results in an error (in the log):
Undefined control sequence.\fooMagic
If the following is inserted at Point A:
\PackageWarning{mypackage}{'#1','##1'}
The log shows that #1 is empty, printing:
'','bar'
I'm not really sure why this is happening. My guess is that it might be something to do with defining all the options and then processing them afterwards, at which point the original #1 values have been dropped. Although regardless of the cause, any help to get around/fix this would be much appreciated.