TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer
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
Thank you, everything is working now as I wanted. – B3ret Apr 14 '11 at 11:13

Your Answer


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.