如何正确定义列表中字符串的多个语法

如何正确定义列表中字符串的多个语法

我想为列表定义自己的语法突出显示。我希望关键字为红色,注释为绿色,纯文本为黄色,字符串为蓝色。问题是我想要有多个字符串定义前缀。${..}、@{..} 和 &{..} 应该定义一个字符串。我遇到的问题在 MWE 和下图中可见。如果我使用 &{} 或 @{},它们不会被突出显示。如果我在一个列表中使用不同类型的字符串,第一个 ${} 和最后一个 @{} 或 &{} 之间的所有内容都将突出显示为蓝色。有没有办法解决这个问题,或者也许我可以尝试一种解决方法。:)

梅威瑟:

\documentclass[a4paper,titlepage,12pt,oneside]{article}
\usepackage{listings}
\usepackage{color}
\definecolor{rideRed}{RGB}{153,0.0,0.0}
\definecolor{rideBlue}{RGB}{0,128,128}
\definecolor{rideGreen}{RGB}{63,127,95}
\definecolor{rideYellow}{RGB}{187,136,68} 
\lstdefinelanguage{Ride}
{
  % list of keywords
  morekeywords={keyword},
  sensitive=false, % keywords are not case-sensitive
  morecomment=[l]{\#}, % l is for line comment
  morestring=[s]{\$\{}{\}}
  morestring=[s]{\&\{}{\}}
  morestring=[s]{\@\{}{\}}
}
% Set Language
\lstset{
  language={Ride},
  basicstyle=\small\ttfamily\color{rideYellow}, % Global Code Style
  captionpos=b, % Position of the Caption (t for top, b for bottom)
  extendedchars=true, % Allows 256 instead of 128 ASCII characters
  tabsize=4, % number of spaces indented when discovering a tab 
  columns=fixed, % make all characters equal width
  keepspaces=true, % does not ignore spaces to fit width, convert tabs to spaces
  showstringspaces=false, % lets spaces in strings appear as real spaces
  breaklines=true, % wrap lines if they don't fit
  frame=trbl, % draw a frame at the top, right, left and bottom of the listing
  frameround=tttt, % make the frame round at all four corners
  framesep=4pt, % quarter circle size of the round corners
  numbers=left, % show line numbers at the left
  numberstyle=\small\ttfamily, % style of the line numbers
  commentstyle=\color{rideGreen}, % style of comments
  keywordstyle=\color{rideRed}, % style of keywords
  stringstyle=\color{rideBlue}, % style of strings
}
\begin{document}
\lstinline|${String}| \\
\lstinline|@{AnotherString}| \\
\lstinline|&{AndAnotherString}|

\begin{lstlisting}
Keyword
#Comment
Normal Text
${String}   Keyword
@{String}   Keyword
&{String}   Keyword
#Comment
Normal Text
${Keyword}  Keyword
@{Keyword}  Keyword
&{Keyword}  Keyword
\end{lstlisting}

\end{document}

其结果如下: 在此处输入图片描述

只有 ${..}、@{..} 和 &{..} 应该以蓝色突出显示。

相关内容