# How can I highlight keywords with special characters or numbers in them?

In the following MWE the declared keywords te-st and 999 are not highlighted as expected:

\documentclass{scrartcl}
\usepackage{listings,xcolor}
\lstdefinelanguage{mylang}{keywords={test,te-st,999,xyz},keywordstyle=\color{red}}
\begin{document}
\begin{lstlisting}[language=mylang,basicstyle=\ttfamily]
abc test
def te-st
ghi 999
jkl xyz
\end{lstlisting}
\end{document}


How can I define keywords like 999 or te-st?

• You need to make - and 9 behave as letters: alsoletter={-9} – egreg May 27 '14 at 12:20

For listings, keywords like all identifiers start with letters and continue with letters or digits. If you need other characters in your identifiers, you must make them letters or digits. Alternatively, use the otherkeywords option for keywords consisting of characters you don't wish to redefine:

\documentclass{scrartcl}
\usepackage{listings,xcolor}
\lstdefinelanguage{mylang}{%
alsodigit={-},    % also: alsoletter
otherkeywords={999},
keywords={test,te-st,xyz},keywordstyle=\color{red}}
\begin{document}
\begin{lstlisting}[language=mylang,basicstyle=\ttfamily]
abc test
def te-st
ghi 999
jkl xyz
\end{lstlisting}
\end{document}


See the Reference part of listings' documentation for details.