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:


Our \thenewcommand is expected to have some (but not all) of the same key=value options of \commander; furthermore, their default values have to be the values called by \commander (or the default values of \commander if they aren't explicitly called). For example, if \thenewcommand is defined as above, then calling:


is just the same as calling:


I have made some steps towards defining \commander. Here are its 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

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)
% Loading \commander's pgfkeys
% Here goes some 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.
    /thenewcommand/.is family, /thenewcommand,
    % "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
    % How do I pass here the value of /commander invert/?
% Here goes some code
% (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:


then calling:


must be the same as calling:


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
  • 1
    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
  • 1
    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
  • 1
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.