[Tex/LaTex] Remove keywords from code ‘listings’

colorlistings

I tried to customize a code listing to support some pseudo-code format that suited me best. It was easy to add keywords using otherkeywords or morekeywords, but the opposite, the deletekeywords did not work as expected. If the stem of a word was defined as a keyword, the word would be highlighted. For example, if create is in morekeywords, then created is also highlighted except the d in the end. Adding created to the deletekeywords list is not helping.

Please suggest how I could solve this problem.

\documentclass[12pt]{article}
\usepackage{fancyhdr}
\usepackage[ansinew]{inputenc} %Windows-1252 encoding
\usepackage{listings}
\usepackage[english]{babel}

\usepackage[usenames,dvipsnames]{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{lightgray}{rgb}{0.9,0.9,0.9}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{numbers=left,
    numbersep=5pt,
    basicstyle=\sffamily\tiny,
    numberstyle=\tiny\color{gray},
    keywordstyle=\color{blue},
    commentstyle=\color{dkgreen},
    stringstyle=\color{mauve},
    backgroundcolor=\color{lightgray},
    frame=single,
    columns=fullflexible
    }

\begin{document}

\title{}
\author{}

\maketitle

\section{somesection}

\lstset{language=Python, caption=somecaption, otherkeywords={smooth}, deletekeywords={smoothed, is, not}}

\begin{figure}[h]
\begin{lstlisting}
    smooth is welcome
    smoothed is not
\end{lstlisting}
\end{figure}

\end{document}

Best Answer

Rather use keywords={smooth} instead of otherkeywords. According to the listings documentation, the latter

defines keywords that contain other characters, or start with digits.

Here's an updated MWE showing the desired output:

enter image description here

\documentclass[12pt]{article}
\usepackage{listings}% http://ctan.org/pkg/listings

\usepackage[usenames,dvipsnames]{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{lightgray}{rgb}{0.9,0.9,0.9}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{numbers=left,
    numbersep=5pt,
    basicstyle=\sffamily\tiny,
    numberstyle=\tiny\color{gray},
    keywordstyle=\color{blue},
    commentstyle=\color{dkgreen},
    stringstyle=\color{mauve},
    backgroundcolor=\color{lightgray},
    frame=single,
    columns=fullflexible
    }

\begin{document}

\title{}
\author{}

\maketitle

\section{somesection}

\lstset{language=Python, caption=somecaption, keywords={smooth}}

\begin{figure}[h]
\begin{lstlisting}
    smooth is welcome
    smoothed is not
\end{lstlisting}
\end{figure}

\end{document}​

The above MWE specifies the keywords, but subsequently also overwrites the existing keywords supported by the language setting. To maintain the existing set and add more/remove some, use morekeywords={smooth}, deletekeywords={is,not}.