Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question already has an answer here:

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

share|improve this question

marked as duplicate by Jubobs, Claudio Fiandrino, Jesse, Heiko Oberdiek, ChrisS May 23 at 11:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
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. –  Jubobs May 23 at 8:10
    
@Jubobs now I think it is more clear –  Hakan May 23 at 8: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). –  Jubobs May 23 at 9:30

1 Answer 1

up vote 2 down vote accepted

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}
share|improve this answer
    
Thank you so much @Jubobs !! –  Hakan May 23 at 9:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.