如何不通过 lstlisting 中的文字覆盖字符串样式?

如何不通过 lstlisting 中的文字覆盖字符串样式?

我正在尝试为 Java 制作颜色代码列表。我使用以下定义:

\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
}

问题是运算符和数字的文字颜色定义不会覆盖字符串的颜色定义。我希望字符串着色是优越的文字风格。谢谢您的建议。

答案1

您应该literate使用星号来调用,如下所示:

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

这说明listings不应literate覆盖其他语法突出显示,包括字符串和注释。

当我做出这一改变时,我得到的输出如下:

在此处输入图片描述

参考:文档listings,第 50 页。

相关内容