Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

Thanks in advance!

share|improve this question

3 Answers 3

up vote 3 down vote accepted

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!

share|improve this answer
    
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.

(Plug: While this article is now freely available since it was published over 12 months ago, TUG membership gives you hard copy and immediate electronic access to such articles; I highly recommend supporting them!)

share|improve this answer
    
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

If I were you, I'd just pass the options to lua and parse the text into a table. lualibs package (string module) have function to parse options and key/value, I'd try that and save myself the headache (and insanity) of various tex based key/value parsing packages.

share|improve this answer
    
Hi Khaled, that is what we are currently doing. But with the TikZ way of passing arguments, it would look much nicer with optional arguments and not something like `\pgfgdsetalgorithm{algo-name}{options as list} –  Matten Nov 30 '10 at 23:20
1  
@Matten, I don't know how to define commands with optional arguments, sorry, but once you know that you can still pass it to lua :) –  Khaled Hosny Nov 30 '10 at 23:41
    
sure I will do this. For further improvements, there might be some cases when somebody needs to modify the interface and get some options right in TeX out of the keys, but for us it's mostly copying the options to Lua and processing them with the string module. Thanks :) –  Matten Dec 1 '10 at 10:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.