[Tex/LaTex] Building keyval arguments using a macro.

key-valuelistings

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?

Best Answer

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