Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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. –  Peter Grill Mar 6 '12 at 18:24
    
You are right. It is my mistake. I have added code. –  Subhamoy Sengupta Mar 6 '12 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 '12 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! –  Subhamoy Sengupta Mar 6 '12 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. –  Subhamoy Sengupta Mar 7 '12 at 9:46
add comment

1 Answer

up vote 6 down vote accepted

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

share|improve this answer
1  
I thought there was a problem with using keywords as this resest the existing keywords and hence the reason for otherkeywords? –  Peter Grill Mar 6 '12 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. –  Subhamoy Sengupta Mar 7 '12 at 9:45
2  
@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 '12 at 6:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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