With the MWE below, I find the following trouble: within the figure* environment, I have three different \lstset instructions. The first and 3rd (commented out) work, the 2nd (as is) presents with error message " language [ANSI]C undefined". It looks like in the error message the language name is perfect. If I uses the \edef trick, the error goes away. I wonder why in line


I do need \edef, and why 3 lines later

> \ME@decorations % Decorating comments

fails if I handle it in the same way as \ME@language in this line


\definecolor{ForestGreen}{rgb}{0.0, 0.4, 0.0}
\definecolor{burntorange}{rgb}{0.8, 0.33, 0.0}
\newlength{\mywidth} % Used in defining listing width
\definecolor{ivory}{rgb}{1.0, 1.0, 0.94}


%%Usage \MESourceFile[keys]{source file}{caption}{label}
%   \lstset{language={[ANSI]C}}
 % \begingroup\edef\x{\endgroup\noexpand\lstset{language={\ME@language}}}\x
        \lstinputlisting[label=#4, name=#4]{#2}%
        \ME@decorations % Decorating comments
%% Prepare for some decorations on the listing files

    \advance\c@lstnumber by \@ne



    \advance\@tempcnta by \m@ne


%% Put a balloon around some lines in a source
% Usage: \MEHighlightLines{BallonName}{SourceName}{FirstLine}{LastLine}
            \xdef\b@lines{({pic cs:line-#2-\pgf@temp-start} -| {pic cs:line-#2-#3-first})}%
                \xdef\b@lines{({pic cs:line-#2-\pgf@temp-start} -| {pic cs:line-#2-#3-start})}%
                \xdef\b@lines{(pic cs:line-#2-\pgf@temp-start)}
    \foreach \k in {#3,...,#4} {%
            \xdef\b@lines{\b@lines (pic cs:line-#2-\k-first) }
            \xdef\b@lines{\b@lines (pic cs:line-#2-\k-end) }
        \noexpand\tikz[remember picture,overlay]
        fit={\b@lines}, color=ForestGreen,yshift=-2pt,
        draw, fill=green!30, opacity=0.4,  inner sep=1pt, rounded corners=5pt
        ] (#1) {};


    ]{HelloWorld.c}{Hello world}{lst:HelloWorld.c}


#include <stdio.h>

int main()
     printf("hello, world\n");
  • I think you truncated your question
    – percusse
    May 11, 2015 at 20:37
  • The output seems good, but that's because output to the .log may necessarily be expanded. The \edef\x-trick expands it so that it works as expected.
    – Werner
    May 11, 2015 at 20:44

1 Answer 1


When you say


TeX doesn't expand any macro in <whatever> and listings compares what it got with the known list of languages. In the case <whatever> is \ME@language, it is not in the list.

However, if you do


TeX will eventually do


and listings will be happy.

The mysterious error message saying that an unknown language was detected is deceiving, because listings passes \ME@language to the error showing command, which will expand it when the message is issued.

  • Hmm. I used to design my systems in a way that the error cause and the message informing about that is the same. It is pretty misleading. Thanks for the explanation.
    – katang
    May 12, 2015 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.