# Managing keyword colors with listings

I am working in a document that presents R code with the listings package, and I am having trouble with the font color with some characters and words that apparently are taken as first keywords.

In this example

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{
language=R,
keywordstyle=\color{blue},
keywordstyle=[2]{\color{red}},
backgroundcolor=\color{gray!25},
morekeywords=[2]{arg,pos},
moredelim=[is][\color{violet}]{''}{''}
}
\begin{document}
\begin{lstlisting}
# define one / two / three characteristics
install(''"tree/site"'', arg = 1, pos = "t")
\end{lstlisting}
\end{document}


that produces:

Here the slashes are printed in blue both in the comments and inside the quotes, and they do not respect the colors defined in lstset.

Besides pos that has explicitly defined as a keywordstyle [2] in red appears in blue.

I appreciate your help or suggestions.

• I just have tried your example with package minted instead of listings. It did mark the comment in one color. Personally I think, the spacing between the glyphens is prettier, than with listings.
– Jan
Feb 7 '17 at 14:14
• Thanks @Jan for the comment, and I'll check out the minted package. However, listings has ways to have prettier spacing like e.g. basicstyle=\fontfamily{zi4}\selectfont in the lstset definition.
– Toño
Feb 8 '17 at 8:39
• A common setting would be basicstyle = \ttfamily, columns = fullflexible for monospaced font Feb 8 '17 at 9:04
• Yes, but I have already defined the basicstyle with zi4 as in my above comment to Jan
– Toño
Feb 8 '17 at 9:09

pos already is defined as keyword. You can remove before defining it new:

deletekeywords = {pos}


/ also is defined as keyword but with otherkeywords. Unfortunately there is no deleteotherkeywords but you can set the list anew leaving / out:

otherkeywords = {!,!=,~,$,*,\&,\%/\%,\%*\%,\%\%,<-,<<-,_}  The original definition of R where I found the above can be found in the file lstlang3.sty. Full example: \documentclass{article} \usepackage{xcolor} \usepackage{listings} \lstset{ language=R, deletekeywords={pos}, otherkeywords={!,!=,~,$,*,\&,\%/\%,\%*\%,\%\%,<-,<<-,_},
keywordstyle=\color{blue},
keywordstyle=[2]{\color{red}},
backgroundcolor=\color{gray!25},
morekeywords=[2]{arg,pos},
moredelim=[is][\color{violet}]{''}{''}
}
\begin{document}
\begin{lstlisting}
# define one / two / three characteristics
install(''"tree/site"'', arg = 1, pos = "t")
\end{lstlisting}
\end{document}


• It works like a charm and thanks a lot. I just was wondering why some otherkeywords work with literate within lstsetlike literate= {<-}{{\ttfamily <-\;}}1 but for example literate= {_}{{\ttfamily _}}1 gives a error. But the reference you gave is very helpful.
– Toño
Feb 8 '17 at 8:46