Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
2  
An interesting problem would be to only color digits in the source, and not in strings or comments. –  Clément Nov 15 '11 at 7:21
1  

1 Answer 1

\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

share|improve this answer
    
Great ! Thanks! –  Nanoc Jul 20 '11 at 21:25
    
+1 Is it possible to change the hard-coded list of {*}{{{\color{red}*}}}1 into a looping form? –  xport Jul 20 '11 at 21:26
1  
This also colors numbers inside comments, though. –  Clément Nov 15 '11 at 7:17
7  
@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 Jan 31 '12 at 15:57
1  
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. –  Jubobs Feb 17 at 12:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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