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 want to highlight all the numbers appearing in an input code of a certain program by, for example, coloring them. By numbers I mean integer, rational and floating point numbers. I am trying with the listings page but with no success. I have noticed that specific numbers can be formatted with the morekeywords and alsoletter options on defining the language but I want to acomplish this task for any number.

For example, I would like the code

vector([3/5,4,0.4566])

to appear with the three numbers with color green (or whatever). Is it possible to do this in an automatic way?

share|improve this question
2  
Welcome to TeX.SE. Even though it may seem trivial, it is always best to compose a MWE that illustrates the problem including the \documentclass so that those trying to help don't have to recreate it. –  Peter Grill Oct 20 '11 at 16:44
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 1 at 18:16
add comment

1 Answer

Similar to solutions in Visualization in LaTeX of hamming distance, and Problem with the alignment of characters, you could use the literate command to define a style that is to be applied to each digit. Below I included a color for the . but commented out the color for the ,.

enter image description here

As there might be a period used outside of a number context, I have defined different styles based on the assumption that any period used in a number will have a digit following it.

\documentclass[border=2pt]{standalone}
\usepackage{listings}
\usepackage{xcolor}

\lstdefinestyle{FormattedNumber}{%
    literate={0}{\textcolor{blue}{0}}{1}%
             {1}{\textcolor{blue}{1}}{1}%
             {2}{\textcolor{blue}{2}}{1}%
             {3}{\textcolor{blue}{3}}{1}%
             {4}{\textcolor{blue}{4}}{1}%
             {5}{\textcolor{blue}{5}}{1}%
             {6}{\textcolor{blue}{6}}{1}%
             {7}{\textcolor{blue}{7}}{1}%
             {8}{\textcolor{blue}{8}}{1}%
             {9}{\textcolor{blue}{9}}{1}%
             {.0}{\textcolor{blue}{.0}}{2}% Following is to ensure that only periods
             {.1}{\textcolor{blue}{.1}}{2}% followed by a digit are changed.
             {.2}{\textcolor{blue}{.2}}{2}%
             {.3}{\textcolor{blue}{.3}}{2}%
             {.4}{\textcolor{blue}{.4}}{2}%
             {.5}{\textcolor{blue}{.5}}{2}%
             {.6}{\textcolor{blue}{.6}}{2}%
             {.7}{\textcolor{blue}{.7}}{2}%
             {.8}{\textcolor{blue}{.8}}{2}%
             {.9}{\textcolor{blue}{.9}}{2}%
             %{,}{\textcolor{blue}{,}}{1}% depends if you want the "," in color
             {\ }{ }{1}% handle the space
             ,
   basicstyle=\ttfamily,%  Optional to use this
}
\newcommand{\FormattedNumber}[1]{%
    \lstinline[style=FormattedNumber]{#1}%
}

\begin{document}
\FormattedNumber{a.vector([3/5,4,0.4566])}
\end{document}
share|improve this answer
    
One caveat: This would format \FormattedNumber{a.vector(<stuff>)} by also making the period blue. It would be possible, but somewhat round-about to get that fixed using mathescape=true and then using \FormattedNumber{a$.$vector(<stuff>)}. –  Werner Oct 20 '11 at 17:07
    
@Werner: Excellent point. Have updated the solution to handle that case. –  Peter Grill Oct 20 '11 at 17:19
    
That's perfect!!! You saved me a lot of time. Thank you!!! –  Rafa Gallego Oct 20 '11 at 18:22
    
All replacements that include the comma should have 2 instead of 1 at the end. this defines the number of output characters and is important for the complicated spacing of listings. –  stefanct Mar 29 at 0:45
    
@stefanct: Thanks. Have corrected it. –  Peter Grill Mar 29 at 2:17
add comment

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.