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.

How to format all numbers in a different color has been discussed in this post.

However, this also colors numbers that are part of a variable name. Is there an option to avoid that?

\documentclass[fleqn, a4paper]{article}
\usepackage{listings} 
\usepackage{color}

\lstdefinelanguage{mylang}{%
    basicstyle=\ttfamily,%
    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}}}{1}% Following is to ensure that only periods
         {.1}{{\textcolor{blue}{.1}}}{1}% followed by a digit are changed.
         {.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}%
         {\ }{{ }}{1}% handle the space
         ,%
    }
\begin{document}
\begin{lstlisting}[language=mylang]
    {
      _SetCalibTableName("lorem",100);
      g_Met_strReferenceName1=Met_strReferenceName;
    }
\end{lstlisting}
\end{document}
share|improve this question

1 Answer 1

This happens because your language definition is too "poor" at the moment; you haven't declared strings, keywords, comments... Depending on the specificity of your language, there are several options: you could use the starred version of literate, so the replacements you declared won't be made on strings and comments, and define some appropriate strings. In the following example, everything between _ and ( and between _ and = will be treated as a string:

\documentclass[fleqn, a4paper]{article}
\usepackage{listings} 
\usepackage{color}

\lstdefinelanguage{mylang}{%
    basicstyle=\ttfamily,%
    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}}}{1}% Following is to ensure that only periods
         {.1}{{\textcolor{blue}{.1}}}{1}% followed by a digit are changed.
         {.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}%
         {\ }{{ }}{1}% handle the space
         ,%
    string=[s]{_}{=},
    string=[s]{_}{(},
    stringstyle=\ttfamily,
    showstringspaces=false
    }
\begin{document}
\begin{lstlisting}[language=mylang]
    {
      _SetCalibTableName("lorem",100);
      g_Met_strReferenceName1=Met_strReferenceName;
    }
\end{lstlisting}
\end{document}

enter image description here

But this could not be the best approach, depending on the language you are trying to define (perhaps this approach would involve considering so many cases that it's not viable, or some objects that shouldn't be treated as strings will be considered erroneously as such).

Another option would be to escape those numbers for which you don't want the replacement to be done; for example, assuming that ! won't be used with any specific meaning in your language, you could say:

\documentclass[fleqn, a4paper]{article}
\usepackage{listings} 
\usepackage{color}

\lstdefinelanguage{mylang}{%
    basicstyle=\ttfamily,%
    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}}}{1}% Following is to ensure that only periods
         {.1}{{\textcolor{blue}{.1}}}{1}% followed by a digit are changed.
         {.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}%
         {\ }{{ }}{1}% handle the space
         ,%
    escapeinside={!!}
    }
\begin{document}
\begin{lstlisting}[language=mylang]
    {
      _SetCalibTableName("lorem",100);
      g_Met_strReferenceName!1!=Met_strReferenceName;
    }
\end{lstlisting}
\end{document}

enter image description here

Another option (similar in spirit to the last one) would be to set mathescape=true and surround the numbers in the variable names with $...$:

\documentclass[fleqn, a4paper]{article}
\usepackage{listings} 
\usepackage{color}

\lstdefinelanguage{mylang}{%
    basicstyle=\ttfamily,%
    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}}}{1}% Following is to ensure that only periods
         {.1}{{\textcolor{blue}{.1}}}{1}% followed by a digit are changed.
         {.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}%
         {\ }{{ }}{1}% handle the space
         ,%
    mathescape=true
    }
\begin{document}
\begin{lstlisting}[language=mylang]
    {
      _SetCalibTableName("lorem",100);
      g_Met_strReferenceName$1$=Met_strReferenceName;
    }
\end{lstlisting}
\end{document}
share|improve this answer
1  
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

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.