[Tex/LaTex] How to change the color of digits when using the listings package

colorlistings

I'm using the listings package and was already able to modify the color of the keywords and almost all elements I was interested in. The only thing that remains is the color of the digits. I'd like to have all numbers within my source codes to be coloured in red. Do you know any smart way of doing this ?

I was able to change the colour of single digits but not of whole numbers. Any help would be greatly appreciated.

Best Answer

\documentclass[12pt]{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{literate=%
    {0}{{{\color{red}0}}}1
    {1}{{{\color{red}1}}}1
    {2}{{{\color{red}2}}}1
    {3}{{{\color{red}3}}}1
    {4}{{{\color{red}4}}}1
    {5}{{{\color{red}5}}}1
    {6}{{{\color{red}6}}}1
    {7}{{{\color{red}7}}}1
    {8}{{{\color{red}8}}}1
    {9}{{{\color{red}9}}}1
}
\begin{document}

\begin{lstlisting}
This is a test
123 + 456 = 789
\end{lstlisting}

\end{document}

enter image description here

Related Question