LaTeX lstlisting - “\keywordsprefix” 关键字在字符串和注释中也得到强调

LaTeX lstlisting - “\keywordsprefix” 关键字在字符串和注释中也得到强调

我正在尝试在 LaTeX 文档中显示一些 SAS 代码。为此,我想使用自定义的 lst 语言。我已经设法实现了除以“&”符号开头的用户定义变量之外的所有内容。每个以“&”开头的单词都应以粗体和蓝色打印,除非它出现在字符串或注释中。在我的示例中,它将始终以粗体和蓝色显示。其他关键字不会在字符串或注释中突出显示。您知道为什么它在我的示例中表现不正确吗?有什么想法可以修复我的代码吗?

提前致谢!

\usepackage{listings}
\usepackage{color}
\usepackage[svgnames]{xcolor}
\definecolor{spYellow}{HTML}{ADAD00}

% ---------------------------------------------------------------------
% SAS language definition

\lstdefinelanguage{SAS}{
  sensitive=false,
  alsoletter={\%\&},
  %
  % User variables
  keywordsprefix={\&},
  %
  % Comments
  morecomment=[f][\color{Green}\slshape][0]*,
  morecomment=[s]{/*}{*/},
  %
  % Strings
  morestring=[b]",
  morestring=[d]',
  %
  % Datalines and cards
  morecomment=[s][\itshape\color{spYellow}]{datalines;}{;},
  morecomment=[s][\itshape\color{spYellow}]{cards;}{;},
  %
  % Distinct highlight for proc <proc>, data, run, quit
  morecomment=[s][\bfseries\color{DarkBlue}]{proc\ }{\ },
  morekeywords={keyword1},
  morekeywords=[2]{
    data ,proc ,run ,quit
  },
  %
  % Macros
  morekeywords=[3]{
    \%include ,\%let
  },
  %
  % Statements
  morekeywords=[4]{libname}
}

% ---------------------------------------------------------------------
% SAS enhanced editor style

\providecommand{\textcolordummy}[2]{#2}
\lstalias{sas}{SAS}
\lstdefinestyle{sas-editor}{
  language          = SAS,
  showstringspaces  = false,   % Don't underline spaces in strings
  showspaces        = false,   % Don't underline spaces
  breaklines        = true,    % automatic line b\color{Blue}\color{Blue}reaking
  breakatwhitespace = true,    % breaks only at white space.
    basicstyle        = \footnotesize\ttfamily,
    tabsize           = 4,      % Tab size
    lineskip          = 1.5pt,  % Sparing between lines of code
    numbers                     = left,
    commentstyle      = \color{black!50}\itshape \let\textcolor\textcolordummy,
  %
  % User variables
  keywordstyle = {\bfseries\color{NavyBlue}},
  %
  % Distinct highlight for proc <proc>, data, run, quit
  keywordstyle = [2]{\bfseries\color{DarkBlue}},
  %
  % Built-in macro functions
  keywordstyle = [3]{\color{Blue}},
  %
  % Statements
  keywordstyle = [4]{\color{Blue}},
  %
  % Strings and comments
  stringstyle  = \color{Purple},
  commentstyle = \color{Green}\slshape
}

\lstset{style=sas-editor}

\begin{document}

Text before the code.

\renewcommand{\ttdefault}{pcr}
\begin{lstlisting}%[language=SAS]
/******************Macros************************/
/* Compile all macros   */
%include "&incodes.keyword1.makro1.sas";
%include '&incodes.keyword1.makro2.sas';
/*%include "&incodes.keyword1.makro3.sas";*/
/*%include &incodes.keyword1.makro4.sas;*/
%include &incodes.keyword1.makro5.sas;
\end{lstlisting}

Some text after the code.

\end{document}

相关内容