3

I'm trying to do color code listing for java. I use this definition:

\usepackage{listings}
\usepackage{color}
\definecolor{codeviolet}{rgb}{0.65,0.11,0.36}
\definecolor{codeblue}{rgb}{0.1,0.21,0.57}
\definecolor{codelightblue}{rgb}{0,0.53,0.70}
\definecolor{codegray}{rgb}{0.5, 0.5, 0.5}
\definecolor{backcolour}{rgb}{0.96,0.96,0.96}
\lstset{
  language=Java,
  %alsoletter=0123456789,
  backgroundcolor=\color{backcolour},
  commentstyle=\color{codegray},
  moredelim=[s][\color{codegray}]{@}{\ },
  keywordstyle=\color{codeviolet}\textbf,
  numberstyle=\tiny\color{codegray},
  stringstyle=\color{codeblue},
  basicstyle=\footnotesize,
  %basicstyle=\ttfamily\small,
  breakatwhitespace=false,         
  breaklines=true,                 
  captionpos=b,                    
  keepspaces=true,                 
  numbers=left,                    
  numbersep=5pt,                  
  showspaces=false,                
  showstringspaces=false,
  showtabs=false,
  texcl=true,
  %frame=single,
  %morekeywords={.,=,!,+,-,:,\%,<,>,<=,=>,==},
  literate=
    {0}{{{\color{codelightblue}0}}}1
    {1}{{{\color{codelightblue}1}}}1
    {2}{{{\color{codelightblue}2}}}1
    {3}{{{\color{codelightblue}3}}}1
    {4}{{{\color{codelightblue}4}}}1
    {5}{{{\color{codelightblue}5}}}1
    {6}{{{\color{codelightblue}6}}}1
    {7}{{{\color{codelightblue}7}}}1
    {8}{{{\color{codelightblue}8}}}1
    {9}{{{\color{codelightblue}9}}}1
    {.}{{{\color{codeviolet}.}}}1
    {=}{{{\color{codeviolet}=}}}1
    {!}{{{\color{codeviolet}!}}}1
    {+}{{{\color{codeviolet}+}}}1
    {-}{{{\color{codeviolet}-}}}1
    {:}{{{\color{codeviolet}:}}}1
    %{/}{{{\color{codeviolet}/}}}1
    {\%}{{{\color{codeviolet}\%}}}1
}

Problem is that literate color definitions of operators and number is not override color definition of string. I expect that string coloring is superior literate style. Thanks for advices.

4

You should call literate with an asterisk, like this:

literate=*
   {0}{{{\color{codelightblue}0}}}1
   {1}{{{\color{codelightblue}1}}}1
   {2}{{{\color{codelightblue}2}}}1
   % and so on

This tells listings that literate should not override other syntax highlighting, including strings and comments.

When I made that change, this is the output I got:

enter image description here

Reference: the listings documentation, p. 50.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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