[Tex/LaTex] Strip one level of braces for using keyval-value within nested keyval

bracesxkeyval

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):

\makeatletter
  \define@key[foo]{bar}{options}{\def\FooBarOptions{#1}}
  \define@key[foo]{bar}{label}{\def\FooBarLabel{#1}}
  \define@key[foo]{bar}{caption}{\def\FooBarCaption{#1}}
\makeatother

%\MySpecialFigure[keyvals]{filename}
\newcommand{\MySpecialFigure}[2][]{%
  \begingroup
    \setkeys[foo]{bar}{options={}, label={}, caption={}}
    \setkeys[foo]{bar}{#1}

    \begin{figure}
      \includegraphics[\FooBarOptions]{#2}
      \caption{\FooBarCaption}
      \label{\FooBarLabel}
    \end{figure}
  \endgroup
}

I want to use it like:

\MySpecialFigure[options={width=0.5\textwidth, draft}, 
                 label={fig:label},
                 caption={My Caption}]
{my-pic.png}

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

  \define@key[foo]{bar}{options}{\edef\FooBarOptions{#1}}

but that wouldn' strip the braces either.

How can I solve this?

Best Answer

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.

Turning

\includegraphics[\FooBarOptions]{#2}

into

\expandafter\includegraphics\expandafter[\FooBarOptions]{#2}

should fix it.

Related Question