2

I'm defining my own listings and have run into an issue whereby LaTeX highlights words within words - e.g. if OR is a keyword, it highlights the OR in ANIMATOR - even if ANIMATOR itself is a keyword (it overrides the ANIMATOR highlighting).

MWE:

\documentclass{article}
\usepackage{listings, xcolor, courier}

\lstdefinelanguage{defFile}{
    morekeywords={FOOBAR, FOOBAE},
    otherkeywords={-,->,> ,BAR},
    morekeywords=[2]{-,->,>,;},
    morekeywords=[3]{BAR}
}

\lstdefinestyle{defFile}
{   
    \lstset{
            language = {defFile},
            basicstyle = \small\ttfamily,
            keywordstyle = \bfseries\color{purple},
            keywordstyle = [2]{\color{green}},
            keywordstyle = [3]{\color{blue}}
            }
}
\begin{document}

\begin{lstlisting}[style={defFile}]
FOOBAR -> FOO, BAR
FOOBAE -> FOO, BAE
\end{lstlisting}


\end{document}

The result of this is:

enter image description here

Note how due to subsequently defining BAR as a keyword, FOOBAR is no longer highlighted.

0
1

Your example doesn't compile. You can't use \lstset inside a style definition. Beside this: Don't repeat your keywords in otherkeywords:

\documentclass{article}
\usepackage{listings, xcolor, courier}

\lstdefinelanguage{defFile}{
    morekeywords={FOOBAR, FOOBAE},
    otherkeywords={-,->,>},
    morekeywords=[2]{-,->,>,;},
    morekeywords=[3]{BAR}
}

\lstdefinestyle{defFile}
{
            language = {defFile},
            basicstyle = \small\ttfamily,
            keywordstyle = \bfseries\color{purple},
            keywordstyle = [2]{\color{green}},
            keywordstyle = [3]{\color{blue}}
}
\begin{document}

\begin{lstlisting}[style={defFile}]
FOOBAR -> FOO, BAR
FOOBAE -> FOO, BAE
\end{lstlisting}


\end{document}

enter image description here

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.