[Tex/LaTex] LaTeX code formatting using lstlistings: custom keywords not recognized

keywordslistings

I'm trying to add some keywords to an existing language using the morekeywords setting:

\documentclass[12pt]{article}

\usepackage{color}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{listings}

\definecolor{lightgray}{rgb}{0.98,0.98,0.98}
\renewcommand{\ttdefault}{pcr}
\lstset {
  language=xml,
  basicstyle={\footnotesize\ttfamily},
  numbers=none,
  backgroundcolor=\color{lightgray},
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  keywordstyle={\bfseries\color{Blue}},
  commentstyle={\color{Red}\textit},
  stringstyle=\color{Magenta},
  frame=single,
  breaklines=true,
  breakatwhitespace=true,
  tabsize=4,
  morekeywords={rdf,rdfs,owl}  % <-- adding custom keywords
}

\begin{document}
\begin{lstlisting}
  <owl:Class rdf:ID="Band">
  <owl :Class rdf :ID="Band">
\end{lstlisting}

\end{document}

gives me

enter image description here

When there is trailing colon, the keywords owl and rdf are not recognized. However, there is no problem with built-in keywords (C example):

enter image description here

Any solutions?

Best Answer

Colon is the namespace indicator in XML so this behavior is not really wrong! If you open this xml in gedit you will see that GTK syntax highlighter does exactly the same!:

enter image description here

But you can do a little hack! If you add : as delimiter, you can achieve what you want. Please note that I'm neither a listings nor xml expert so I'm not sure this is the best solution:

\documentclass[12pt]{article}

\usepackage{color}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{listings}

\definecolor{lightgray}{rgb}{0.98,0.98,0.98}
\renewcommand{\ttdefault}{pcr}
\lstset {
  language=xml,
  basicstyle={\footnotesize\ttfamily},
  numbers=none,
  backgroundcolor=\color{lightgray},
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  keywordstyle={\bfseries\color{Blue}},
  commentstyle={\color{Red}\textit},
  stringstyle=\color{Magenta},
  frame=single,
  breaklines=true,
  breakatwhitespace=true,
  tabsize=4,
  morekeywords={rdf,rdfs,owl},
  moredelim=*[s][\ttfamily]{:}{:} %Newly added line
}

\begin{document}
\begin{lstlisting}
  <owl:Class rdf:ID="Band">
  <owl :Class rdf :ID="Band">
\end{lstlisting}

\end{document}

Which generates this:

enter image description here