Martin Scharrer posted a very elegant answer for the question "Creating a zebra effect using listings". I want to extend that code to highlight only lines given in a comma separated list. The motivations is that I want to highlight certain lines of fully functional C code inputed from a file without using any escape chars
That means that I need a command
\ifinrange{<num>}{<range>}{<TRUE>}{<FALSE>}% num>=0
where range {-2,4,8-10, 15-} is {0,1,2,4,8,9,10,15,16,...}
\ifinrange{ 0}{-2,4,8-10, 15-}{TRUE}{FALSE} -> TRUE
\ifinrange{ 3}{-2,4,8-10, 15-}{TRUE}{FALSE} -> FALSE
\ifinrange{ 4}{-2,4,8-10, 15-}{TRUE}{FALSE} -> TRUE
\ifinrange{ 9}{-2,4,8-10, 15-}{TRUE}{FALSE} -> TRUE %% EDIT: changed 7 to 9
\ifinrange{11}{-2,4,8-10, 15-}{TRUE}{FALSE} -> FALSE
\ifinrange{20}{-2,4,8-10, 15-}{TRUE}{FALSE} -> TRUE
The following code
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{beramono}
\usepackage{listings}
\usepackage{xcolor}
\makeatletter
\newcommand\realnumberstyle[1]{}
\newcommand{\HighLight}[3]{%
{\realnumberstyle{#3}}%
\begingroup
\lst@basicstyle
\ifinrange{\value{lstnumber}}{#2}{%
\color{#1}%
\rlap{\hspace*{\lst@numbersep}%
\color@block{\linewidth}{\ht\strutbox}{\dp\strutbox}}}{}%
\endgroup}
\newcommand{\ifinrange}[4]{}%%<<<<<<<<<<<<<<<<<
\makeatother
\begin{document}
\begin{lstlisting}[language=C,
basicstyle=\ttfamily,
numberstyle=\HighLight{green!35}{-3,6},
numbers=left]
/**
* Prints Hello World.
**/
#include <stdio.h>
int main(void) {
printf("Hello World!");
return 0;
}
\end{lstlisting}
\end{document}
must produce


inlinemacro required. The exact application is only secondary here. – Martin Scharrer May 25 '11 at 06:47\ifinrange{ 7}{-2,4,8-10, 15-}{TRUE}{FALSE} -> TRUEshould be FALSE instead. – Martin Scharrer May 25 '11 at 08:06