# listings: Changing the meaning of underscore for the language R (customizing a language)

I really like the listings package but often it drives me crazy :). I want to have listings of the programming language R (statistic stuff). Here's a minimal example:

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}
\lstset{
language=R,
basicstyle=\ttfamily,
keywordstyle=\color{red}\bfseries,
}

\begin{document}

\begin{lstlisting}
my_Variable <- qnorm(0.05/2, mean=0, sd=1)
>> 1.959964
\end{lstlisting}

\end{document}


Here's the result:

Here's the problem:

1. Even in the comment the dash is highlighted.
2. The underscore in the Variable name is highlighted too.

I have tried to use the deletekeywords={_,/} option - but without success. I also tried alsoletter. Maybe someone could point out my mistake.

EDIT 1: If it's possible I do not want to change the actual code (listing) as in the first answer. Because then the code won't be suitable for my programming language (in my case R) any more.

EDIT 2: Any help is appreciated. How can I make it easier to help?

-
Have you considered knitr or sweave? With either of these the source document contains both the runnable R code and the LaTeX text. –  Ethan Bolker Oct 20 '12 at 13:34
@Ethan: Hello Ethan, thanks for the hint. I will consider this in the next project - right now I want to meet the deadline :). I just want to include R code as it is and it should look pretty in the listing. –  Manuel Kuehner Oct 21 '12 at 9:44

The R language highlighting is defined in lstdrvrs.dtx. If you look there, you will find

otherkeywords={!,!=,~,$,*,\&,\%/\%,\%*\%,\%\%,<-,<<-,_,/}  So you just need to add that to your \lstset to overwrite the defaults, but with the underscore and slash removed: otherkeywords={!,!=,~,$,*,\&,\%/\%,\%*\%,\%\%,<-,<<-}


Doing this will prevent the slash and underscore from ever being highlighted, though, so this won't work if you need them to be highlighted in other contexts. Just to make things uniform between division and multiplication, you may also wish to remove the asterisk * from the list. (You might need to remove a few other things as well to keep math in general uniform; I don't know R.)

Unfortunately, Pygments doesn't seem to offer as much as listings in terms of keyword highlighting for R, so if that's what you need, Pygments-based highlighting packages like minted and pythontex may not be an alternative (at least, not without writing a custom lexer).

-
Thanks. This is very useful. Is it normal that listings highlights in comments? –  Manuel Kuehner Oct 22 '12 at 19:42
Regular keywords (such as defined by morekeywords) don't highlight in comments. But it seems that anything in otherkeywords will be highlighted anywhere. This is apparently a bug in listings (here's another reference to it). –  G. Poore Oct 22 '12 at 22:13
@Pooer: Ok - thanks. Do you think a bug report has a chance to get noticed? –  Manuel Kuehner Oct 23 '12 at 19:01
You might file a bug report, but listings hasn't had a new release since 2007. That's part of the reason I added Pygments-based highlighting to my pythontex package; I want a powerful highlighting solution that's under active development. –  G. Poore Oct 23 '12 at 22:25
Very cool. I haven't heared about it yet. I neet to install additional software as I understand it. I'll try it. Thanks. –  Manuel Kuehner Oct 24 '12 at 17:34

You can seek shelter inside escapeinside option as below:

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}
\lstset{
language=R,
basicstyle=\ttfamily,
keywordstyle=\color{red}\bfseries,
escapeinside=||
}

\begin{document}

\begin{lstlisting}
my|\_|Variable <- qnorm(0.05/2, mean=0, sd=1)
# even inside comments |/| doesn't highlight.
>> 1.959964
\end{lstlisting}

\end{document}


-
That's a very comfortable way -- thank you very much! I didn't use escapeinside yet. Cool! But this is a kind of work around? This solution changes into LaTeX mode - right? I'll wait a bit - maybe there is another way without changing the listing. Because the listing is not suitable to be used in R anymore. –  Manuel Kuehner Oct 20 '12 at 12:40