18

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}
5
  • Welcome to TeX.SE. It is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. Mar 6, 2012 at 18:24
  • You are right. It is my mistake. I have added code. Mar 6, 2012 at 19:29
  • 1
    If you use keywords={smooth} instead, everything works fine. Moreover, otherkeywords is defined for keywords containing non-letter elements. Is there any reason why you're using otherkeywords?
    – Werner
    Mar 6, 2012 at 19:52
  • 1
    Very silly of me! I did not realize I could use keywords. I thought otherkeywords or morekeywords is the one I should use to add more keywords to the set. Thank you very much! Mar 6, 2012 at 20:09
  • I looked into it a bit, and keywords is not what I want, because it resets the list of keywords and the ones listed inside keywords are the only keywords that get highlighted. Mar 7, 2012 at 9:46

1 Answer 1

13

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}.

3
  • 1
    I thought there was a problem with using keywords as this resest the existing keywords and hence the reason for otherkeywords? Mar 6, 2012 at 23:45
  • That is exactly what I was about to comment on. Using only keywords seems to remove all Python keywords, which I do not want. That was the reason I used otherkeywords to begin with. Mar 7, 2012 at 9:45
  • 3
    @SubhamoySengupta: I've updated my answer with the use of morekeywords={smooth},deletekeywords={is,not}, which seems to be what you're after.
    – Werner
    Mar 8, 2012 at 6:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .