I have defined my own figure command using xkeyval and I want to pass arguments for the nested \includegraphics as one of its keyvalues (maybe I stripped too much code, but I hope you get the idea):


    \setkeys[foo]{bar}{options={}, label={}, caption={}}


I want to use it like:

\MySpecialFigure[options={width=0.5\textwidth, draft}, 
                 caption={My Caption}]

Because of the braces around the options, the whole block gets parsed as one value at \includegraphics and behaves like:

\includegraphics[{width=0.5\textwidth, draft}]{my-pic.png}

I get error:

Package xkeyval Error:'width=0.5\textwidth, draft' undefined in families 'Gin'

I tried


but that wouldn' strip the braces either.

How can I solve this?


You need to expand the \FooBarOptions before you feed it to \includegraphics, otherwise it is taken as one key. This has nothing to do with an extra layer of braces.





should fix it.

  • It works like that. Why do I need expandafter twice? I tried both \expandafter\includegraphics[\FooBarOptions]{#2} and \includegraphics\expandafter[\FooBarOptions]{#2} – B3ret Apr 14 '11 at 10:39
  • @B3ret: An \expandafter "jumps" over the next token (e.g. macro), so the first one jumps over \includegraphics and would expand [ if the second one is not there. This one then jumps over the [ and expands \FooBarOptions. One way to avoid this is to define the [ ] as part of \FooBarOptions: \def\FooBarOptions{[#1]}. Then you can use \expandafter\includegraphics\FooBarOptions{#2}. However, \includegraphics\expandafter[\FooBarOptions]{#2} would make \includegrpahics be expanded first and then take \expandafter` as first mandatory argument, which causes of course trouble. – Martin Scharrer Apr 14 '11 at 10:42

Your Answer

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

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