如何突出显示包含破折号或斜线的关键字?

如何突出显示包含破折号或斜线的关键字?

我有一个 Clojure 列表,我想为某些关键字着色,而我只能对不包含任何破折号 ('-') 或斜线 ('/') 的字符串进行此操作。但我还需要考虑由破折号等组成的关键字。

我有以下清单:

\documentclass[english,a4paper,12pt]{report}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{listings}
\usepackage{xcolor}

\input{listingsClojure}

\begin{document}
\begin{lstlisting}[
        basicstyle={\fontsize{9}{10}\ttfamily},
        mathescape=true,showstringspaces=false,
        flexiblecolumns=true,
        tabsize=2,
        numbersep=1pt,
        numbers=left,
        xleftmargin=0.2cm,
        framerule=0pt,language=Clojure
        ]
(defn NOAsp [?n1 ?n2]
  "the numbers"
  (l/all
      (w/asp ?n1)
      (equals ?n2 (.getSourceLocation ?n1))))
\end{lstlisting}


\end{document}

还有我的 lstdefinelanguage Clojure 文件:

\usepackage{soul}
\usepackage{xcolor}

\lstdefinelanguage{Clojure} {
    morekeywords={defn, equals,l,all,getSourceLocation,'w/asp'},
    morestring=[b]",
    morestring=[b]', 
}


\lstdefinelanguage{none} {
  keywords={},
  alsoletter={+},
  morekeywords={}
}

\newcommand{\fsize}{\fontsize{8pt}{0pt}\selectfont}
\newcommand{\fsizesmall}{\fontsize{7.5pt}{0pt}\selectfont\ttfamily}




\lstset{
  basicstyle = \fsize,
  identifierstyle = \fsize,
  keywordstyle = \fsize\color[HTML]{71295b},
  keywordstyle = [2]\fsize\bfseries \color[HTML]{859900},
  commentstyle = \fsize\color[HTML]{839496},
  stringstyle = \fsize\color[HTML]{2429c9},
  lineskip={-2pt}
}

我想看看 '全部' 和 '黄蜂' 和 '.获取源位置' 一起上色。现在我只看到以下关键字是彩色的:“l”、“all”、“defn”和“getSourceLocation”。更清楚的是,红色块必须一起上色:

在此处输入图片描述

答案1

默认情况下,listings不允许/使用标识符。您必须明确指定listings将该字符视为“字母”:

alsoletter=/,

'此外,如果您打算在同一语言中使用该字符作为字符串分隔符,请不要使用内部关键字listings。您无法同时实现这两种方式。

在此处输入图片描述

\documentclass[english,a4paper,12pt]{report}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{listings}
\usepackage{xcolor}

\usepackage{soul}
\usepackage{xcolor}

\lstdefinelanguage{Clojure} {
    alsoletter=/, % <--- this
    morekeywords={defn, equals,l/all,getSourceLocation,w/asp},
    morestring=[b]",
    morestring=[b]', 
}


\lstdefinelanguage{none} {
  keywords={},
  alsoletter={+},
  morekeywords={}
}

\newcommand{\fsize}{\fontsize{8pt}{0pt}\selectfont}
\newcommand{\fsizesmall}{\fontsize{7.5pt}{0pt}\selectfont\ttfamily}




\lstset{
  basicstyle = \fsize,
  identifierstyle = \fsize,
  keywordstyle = \fsize\color[HTML]{71295b},
  keywordstyle = [2]\fsize\bfseries \color[HTML]{859900},
  commentstyle = \fsize\color[HTML]{839496},
  stringstyle = \fsize\color[HTML]{2429c9},
  lineskip={-2pt}
}

\begin{document}
\begin{lstlisting}[
        basicstyle={\fontsize{9}{10}\ttfamily},
        mathescape=true,showstringspaces=false,
        flexiblecolumns=true,
        tabsize=2,
        numbersep=1pt,
        numbers=left,
        xleftmargin=0.2cm,
        framerule=0pt,language=Clojure
        ]
(defn NOAsp [?n1 ?n2]
  "the numbers"
  (l/all
      (w/asp ?n1)
      (equals ?n2 (.getSourceLocation ?n1))))
\end{lstlisting}

\end{document}

相关内容