# Building keyval arguments using a macro.

I try to create the arguments for a package which uses keyval, with macro, but this does not work:

Here is the demo code:

\documentclass{minimal}
\usepackage{listings}
\lstset{language=Pascal}
\def\flags{language=Pascal}
\lstset{\flags}
\begin{document}
\end{document}


The first \lstset works. The second fails with

! Package keyval Error: language=Pascal undefined.


Any ideas on how I can do this?

-

The keyval approaches all avoid expanding input, so in your \lstset{\flags} example the code sees \flags and not the content of the macro. The error message here is a bit unhelpful as it is expanded by TeX, so it looks like what you expect! You need to expand the input before applying the keyval macro

\documentclass{minimal}
\usepackage{listings}
\lstset{language=Pascal}
\def\flags{language=Pascal}
\expandafter\lstset\expandafter{\flags}
\begin{document}
\end{document}


(I assume the real problem has this wrapped up inside a macro or similar, where you can do the appropriate expansion.)

-
Brrrr.... I tried one \expandafter, did not think of using two. The error message really confused me. – Yossi Gil Mar 15 '11 at 21:07
@Yossi: As I said, the error is misleading (it would be better to put the argument into a toks and then use that for the output, or to use e-TeX's \unexpanded). – Joseph Wright Mar 15 '11 at 21:11
@Yossi: Remember that { is a token. So \expandafter\lstset{\flags} will stop after expanding {. You need to jump over \expandafter and {; hence, two \expandafters. – Matthew Leingang Mar 15 '11 at 22:22
@Matthew: Ah, a nice place to sell expl3. Our equivalent is \keys_set:nV { package } \variable, and sorts out the expansion for you! – Joseph Wright Mar 15 '11 at 22:43