Listings package: coloring integers

I am using the Latex Listings package to include code in a document. I need to implemented syntax highlighting similar to what you see in a standard IDE.

I've been able to assign colors to comments, keywords, and strings, but I am having some trouble figuring out how to color the numbers properly.

Here's an example of how it should look:

As you can see, anytime a word after the equals sign starts with a digit, all following characters until (but not including) the next semicolon or space are colored. When a word starts with a digit, all characters in the word should be colored, even if they are not digits. Otherwise, none of the digits are colored.

As of right now, I cannot think of a case where a word before the equals sign would start with a digit, so it will probably be acceptable to find a solution that colors all words starting with a digit.

In this case, digits are: 0-9.

Here's an MWE in Latex, but as you can see it doesn't have any coloring for the numbers.

\documentclass[pdftex]{report}
\usepackage{listings}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\definecolor{Code}{rgb}{0,0,0}
\definecolor{Keywords}{rgb}{255,0,0}
\definecolor{Strings}{rgb}{255,0,255}
\definecolor{Numbers}{rgb}{255,128,0}

\lstdefinestyle{mycode} {
language=C,
% Strings
stringstyle=\color{Strings},
% keywords
keywordstyle={\color{Keywords}\bfseries}
}

\begin{document}

\begin{lstlisting}[style=mycode]
int main ()
{
//this is an example
a1 = 0;
a2 = a1;
a3 = 16hFF;
return 0;
}
\end{lstlisting}

\end{document}

-
– Peter Grill Nov 6 '12 at 18:21
Thanks for linking those threads. However, neither addresses the issue of coloring numbers only after the equals sign. I've tried the solutions in both threads, but they still color numbers that occur to left of the = sign, which is not what I want. – Lyn Nov 6 '12 at 21:19
So just to clarify: you want the entire text of anything that starts with a digit following an equal sign (with possible a leading space) to be colored up until the closing semi-colon. No other digits are to be colored. So, can it be assumed that the "digits" that are to be colored restricted to [a-fA-F0-9h]? Are there other characters that may show up here? – Peter Grill Nov 6 '12 at 21:35
Thanks for clarifying. Reading your comment got me thinking more about the requirements and I've modified the question slightly. Any WORD after the equals sign that starts with a digit (0 - 9, a-f, A-F) should be colored. Coloring ends at the next semicolon OR SPACE. There's another example in my code that illustrates this as well. – Lyn Nov 6 '12 at 21:51
You shoudl also adjust the text: "if the first digit after the equals sign is a number, all following digits until (but not including) the next semicolon are colored". Also can you provide a list of all the characters that are digits. You example include h. Any other characters? – Peter Grill Nov 6 '12 at 21:52
show 1 more comment

Here is an adaptation of Coloring digits with the listings package:

Further Enhancements:

• This does not work if there is a non-digit within a number. So 16hxFF will not be properly displayed, but 16x (with trailing space) is fine.

Code:

\documentclass[pdftex]{report}
\usepackage{listings}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\definecolor{Code}{rgb}{0,0,0}
\definecolor{Keywords}{rgb}{255,0,0}
\definecolor{Strings}{rgb}{255,0,255}
\definecolor{Numbers}{rgb}{255,128,0}

\usepackage{etoolbox}
\newtoggle{AfterEqualSign}% Are we supposed to be coloring digits or not
\togglefalse{AfterEqualSign}% Wait until first equal sign
\togglefalse{AlreadyColoring}% Start off with not coloring.

\newcommand*{\ColorIfDigitsAfterEqualSign}[1]{%
\iftoggle{AfterEqualSign}{\color{orange}#1}{#1}%
}%
\color{orange}#1%
}{%
\global\togglefalse{AfterEqualSign}%
#1%
}%
}%
\newcommand*{\DisableColoring}[1]{%
#1%
\global\togglefalse{AfterEqualSign}%
}%
% Allows for a leading space after the =, but before the number
#1%
\global\togglefalse{AfterEqualSign}%
}{}%
}%
\lstset{literate=%
{=}{{{\global\toggletrue{AfterEqualSign}{=}}}}1% Encountered equal sign
{0}{{{\ColorIfDigitsAfterEqualSign{0}}}}1
{1}{{{\ColorIfDigitsAfterEqualSign{1}}}}1
{2}{{{\ColorIfDigitsAfterEqualSign{2}}}}1
{3}{{{\ColorIfDigitsAfterEqualSign{3}}}}1
{4}{{{\ColorIfDigitsAfterEqualSign{4}}}}1
{5}{{{\ColorIfDigitsAfterEqualSign{5}}}}1
{6}{{{\ColorIfDigitsAfterEqualSign{6}}}}1
{7}{{{\ColorIfDigitsAfterEqualSign{7}}}}1
{8}{{{\ColorIfDigitsAfterEqualSign{8}}}}1
{9}{{{\ColorIfDigitsAfterEqualSign{9}}}}1
{\ }{{{\DisableColoringIfAlreadyColoring{\ }}}}1% Disable coloring at trailing space
{;}{{{\DisableColoring{;}}}}1% Disable coloring at semi colon
}

\lstdefinestyle{mycode} {
language=C,
% Strings
stringstyle=\color{Strings},
% keywords
keywordstyle={\color{Keywords}\bfseries}
}

\begin{document}

\begin{lstlisting}[style=mycode]
int main ()
{
//this is an example
a1 = 0;
a2 = a1;
a3 = 16hFF;
a4 = 16 + a1;
return 0;
}
\end{lstlisting}

\end{document}

-
 Thank you. This answer works well for my code! – Lyn Nov 8 '12 at 7:30