1

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?

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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