# How to create a command that creates commands?

I recently learned how to make key=value commands with the pgfkeys package. I'm currently trying to define a command, let's call it \commander, with key=value options that can define new commands also with key=value functions. (Something like supercommand or metacommand.) The syntax of \commander is roughly the following:

\commander[color=red,pos=up,invert=false]{\thenewcommand}


\thenewcommand[color=blue]

is just the same as calling:

\thenewcommand[color=blue,pos=up,invert=false]

I have made some steps towards defining \commander. Here are its pgfkeys.

\newif\ifcommanderinvert
\pgfkeys{
/commander/.is family, /commander,
% "commander" options
color/.estore in = \commanderColor,
pos/.estore in = \commanderPos,
invert/.is if=commanderinvert,
% Ignore this: It is just for showing that I'm using the .unknown feature
.unknown/.code={\edef\commanderUnk{\commanderUnk,\pgfkeyscurrentname=#1}},%
}

And here is an incomplete definition of \commander:

% (Optional) argument #1 is for the options
% (Non-optional) argument #2 is the name of the new command
%                                    (e.g.: \thenewcommand)
\newcommand{\commander}[2][]{%
\pgfkeys{/commander,%
color=black,%
pos=up,%
invert=false,#1}%
%
% Here goes some code
Code...
%
% Here I define the pgfkeys for \thenewcommand.
% Every appearance of the string "thenewcommand" has to be replaced
% by the actual name of our new command or an abbreviation.
% Else, we risk making conflicting names each time we call \commander.
\newif\ifthenewcommandinvert
\pgfkeys{
% "thenewcommand" options
% Notice I'm not using the "pos" option
color/.estore in = \thenewcommandColor,
invert/.is if=thenewcommandinvert,
}
% Defining \thenewcommand: it only has optional arguments
% Notice that the name of the new command is #2 of \commander
\newcommand{#2}[1][]{%
\pgfkeys{/thenewcommand,%
color=\commanderColor,%
%invert=?,
% How do I pass here the value of /commander invert/?
#1}%
% Here goes some code
\ifthenewcommandinvert%
Code...%
\else%
Code...\fi%
% (By the way, how can I distinctly refer to
% argument #1 of \commander and to argument #1 of \thenewcommand?)
}

With a definition along these lines, if we call \commander as follows:

\commander[color=blue]{\bluecommand}

then calling:

\bluecommand[invert=true]

must be the same as calling:

\bluecommand[color=blue,pos=up,invert=true]

In order to complete this function I have to overcome two challenges:

1. What is the best strategy for recursively creating the pgfkeys for each new command created with \commander?
2. How do I the pass value of the invert key of \commander as the default value of the invert key of the new command.

I hope someone can give me some hand here. Thanks in advance.

PS: For this command I'm using/requiring the packages graphicx, pgfkeys and tikz and, optionally, the class beamer.

• Potential duplicate ? tex.stackexchange.com/a/65781/141947 – BambOo Sep 4 '19 at 11:36
• Thanks, @BambOo. I don't think it's a duplicate. The use of key=value options takes this into another level. It is still useful, though. – lfba Sep 4 '19 at 11:38
• You can do this with \csname <command name>\endcsname but given that you are using pgf keys, why not just using <my new command>/.code={<code>}. This would also allow you to store the other keys on a directory of that name. (graphicx gets loaded by tikz.) – user194703 Sep 4 '19 at 14:46
• What I meant is that, as explained in section 87.1.2 Quick Guide to Using the Key Mechanism of pgfmanual v3.14b. you can also use pgf keys to "execute" some code. I do not understand the purpose of "invert" and other things, otherwise I could post an example. – user194703 Sep 6 '19 at 3:03
• The thing is that internally pgf keys work by using \csname <command name>\endcsname to convert keys to macros, which they internally use. You in a way seem to aim at "inverting" this procedure. Which is why I would suggest just to work with pgf keys, and /.code. – user194703 Sep 6 '19 at 3:20