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.


1 Answer 1


This is a test
123 + 456 = 789


enter image description here

  • +1 Is it possible to change the hard-coded list of {*}{{{\color{red}*}}}1 into a looping form? Commented Jul 20, 2011 at 21:26
  • 1
    This also colors numbers inside comments, though.
    – Clément
    Commented Nov 15, 2011 at 7:17
  • 12
    @Clément: Add a star before the zero, i.e. {0}{{{\color{red}0}}}1 -> *{0}{{{\color{red}0}}}1 to only colour outside of strings and comments. You only need to do this once, so not for the rest of the numerals.
    – qubyte
    Commented Jan 31, 2012 at 15:57
  • 3
    However, be aware that, with those literate replacements of digits, if you define identifiers containing digits, listings won't be able to recognise those identifiers as such. For instance, if you have alsoletter={1},morekeywords={test1},keywordstyle=\color{blue}, then Herbert's literate replacements will prevent test8 from being coloured in blue.
    – jub0bs
    Commented Feb 17, 2014 at 12:48
  • @xport I know your question is old, but, if you're still interested, look at egreg's answer to tex.stackexchange.com/questions/164442/…
    – jub0bs
    Commented Aug 16, 2014 at 12:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .