I wanted to define a macro which makes a program listing, and finds out a program language from an optional argument. The output I receive from the MWE below is
Language set = 'Verilog'
Passed argument is 'Verilog'
So, is looks like that in both calls to '\lstset' the same argument is passed. However, if the argument is produced from optional, rather than a mandatory argument, I am presented with the error message: "language Verilog undefined". What do I wrong?
\documentclass{article}
\usepackage{listingsutf8}
\usepackage{xkeyval}
\makeatletter
\define@key{MESourceFile}{language}{\def\pb@language{#1}}
\setkeys{MESourceFile}{language={[ANSI]C}}%
\newcommand\MESourceFile[4][]{
\begingroup%
\setkeys{MESourceFile}{#1}
Passed argument is '\pb@language'\\
\lstset{language={\pb@language}}
\endgroup
}
\makeatother
\newcommand\MESetListingFormat[2][]
{
Language set = '#2'
\lstset{language={#2}} e
}
\begin{document}
\MESetListingFormat{Verilog}
\MESourceFile[language=Verilog]{lst/TestHello.c}{A hello világ program}{lst:TestHello.c}
\end{document}

\lstset{language={\pb@language}}, the code\begingroup\edef\x{\endgroup\noexpand\lstset{language=\pb@language}}\x– egreg Aug 20 '14 at 15:45\begingroup\edef\xtrick is very common; you need nothing special if the argument is empty, as\setkeyshappily accepts an empty argument. – egreg Aug 20 '14 at 17:40