# LaTeX code formatting using lstlistings: custom keywords not recognized

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

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

Any solutions?

• I don't think while is a built-in keyword for the XML language. – egreg May 21 '13 at 15:44
• @egreg The second example is from C for illustration. – Yang May 21 '13 at 16:53
• There is a big difference. In the XML language, listings declares : as a letter, which it doesn't for the C language. – egreg May 21 '13 at 16:55

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!:

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}},
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:

• Thanks for your help, but it fails to recognize rdf. – Yang May 21 '13 at 19:13
• Yes. I didn't pay attention. I will try to modify it... – Pouya May 21 '13 at 19:25
• @Yang I modified the answer. Now rdf is also detected. As you can see ID is highlighted but as I said I'm not xml expert so I don't know if this behavior is fine or not. – Pouya May 21 '13 at 19:33
• Yes, ID is not highlighted on my machine. Thanks! – Yang May 21 '13 at 19:36