[Tex/LaTex] How to highlight keywords with special characters or numbers in them

highlightingkeywordslistings

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}

enter image description here

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

Best Answer

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.