# Passing options to a macro

Background

We're developing an automated graph drawing framework for TikZ with LuaTeX. The TeX interface on the pgf layer is nearly done, the next step will be the TikZ Layer. The maker of TikZ, Till Tantau, gives us a lot of help, but he has very little time.

The most work is done, but some little sugar is missing. The algorithms work, passing the nodes to lua and shipping them out back later works, with edges too. As I'm proceeding with the pgf layer, I got a problem with passing arguments to macros.

Problem

Some graph drawing algorithms need options like the root node of a tree or in which style the nodes should be arranged. Saving the options after \node[options] is no problem, but how to implement such an option handling by myself? I've read the particular pgfkeys section of the TikZ manual and done a lot of googeling, but not found yet what I need.

The idea is something like this (it's very simplified for showing what I want!)

We have some interface functions on the TeX level to select algorithms and so on.

\def\pgfgdsetalgorithm...{..call some lua functions here..}


Some algorithms, say the tree drawing algorithm, needs an explicit root node. The call could now be something like this

\pgfgdsetalgorithm[rootnode=nodexy]{treedrawing}


After the examples I've read I now know it's very comfortable for users to use the options, but very hard to implement them.

Does anyone got a simple example or maybe a good tutorial so I can proceed?

-

To expand a bit on Will's answer, an initial staring point would be something like

\pgfqkeys{setalgorithm}{%
rootnode .code = \pgfkeys{/node = #1}
}
\def\pgfgdsetalgorithm{%
\pgfutil@ifnextchar[%]
{\pgfgdsetalgorithm@}
{\pgfgdsetalgorithm@[]}%
}
\def\pgfgdsetalgorithm@[#1]#2{%
\pgfqkeys{setalgorithm}{#1}%
% Stuff with #2
}


Here, I've used the pgf version of \@ifnextchar to set up the optional argument in a format-independent fashion. I'm not quite clear on what exactly you need to do with your keys, so for the moment I've simply assumed that you would translate the rootnode key into a plain pgf key called node: I expect there is more to this than that!

-
This sounds like a very interesting approach and should fit my problem. I appreciate your input and will try to get along with it this evening! –  Matten Dec 1 '10 at 8:55
Thank you. The solution is clearly not very difficult, but I had thought in many different directions :) –  Matten Dec 1 '10 at 11:32

It's not clear to me exactly what you're stuck on, but you might find Joseph Wright's article on keyval processing in LaTeX to be helpful:

http://www.tug.org/TUGboat/tb30-1/tb94wright-keyval.pdf

The section on pgfkeys is near the end.

I already read this article (more than once) and it's a very good starting point. My question is more like 'I want pgfkeys and I have an idea - a macro like \pgfgdsetalgorithm[]{} - how to obtain the optional [] part and where to set the keys' –  Matten Dec 1 '10 at 8:42