Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 8 down vote accepted

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

share|improve this answer
    
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
1  
@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
2  
@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
1  
@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
add comment

Your Answer

 
discard

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.