5

I have a Clojure listing and I want to colour certain keywords whereas I can only do for strings not including any dash ('-') or slash ('/). But I also need to consider keywords consisting of with dash and etc as well.

I have the following listing:

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

And my lstdefinelanguage Clojure file:

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

I would like to see 'l/all' and 'w/asp' and '.getSourceLocation' together in color. Now I only see the following keywords are coloured: 'l', 'all', 'defn' and 'getSourceLocation'. More clearly, the red blocks must be coloured together:

enter image description here

jub0bs
  • 58,916
Hakan
  • 546
  • Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – jub0bs May 23 '14 at 08:10
  • @Jubobs now I think it is more clear – Hakan May 23 '14 at 08:50
  • Your question is somewhat clearer now, but people still have to piece your code together in order to run it. What's good practice, on this site, is to make your code self-contained by providing a minimal working example (MWE). – jub0bs May 23 '14 at 09:30

1 Answers1

9

By default, listings doesn't allow / in identifiers. You have to explicitly tell listings to treat that character as a "letter":

alsoletter=/,

Also, don't use ' inside keywords if you intend to use that character as a string delimiter within the same listings language. You can't have it both ways.

enter image description here

\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}
jub0bs
  • 58,916