[Tex/LaTex] listings with R: keywords in variable names are highlighted when using underscores

listingsr

When using language=R as a lstlistings style, if a variable name contains a keyword surrounded by underscores it will be highlighted. See the image and code example below. What can I do to prevent this from happening?

Other languages like Java seem to work fine, as do variable names without underscores. I tried alsoletter=_ but that doesn't change anything.

enter image description here

\documentclass{article}
\usepackage{listings}
\usepackage{color}

\lstdefinestyle{generalStyle} {
    frame=single,
    keywordstyle=\color{blue}\bfseries,
    stringstyle=\color{magenta}
}

\lstdefinestyle{JavaStyle} {
    language=Java, 
    style=generalStyle
}

\lstdefinestyle{RStyle} {
    language=R,
    style=generalStyle  
}

\begin{document}

    Java
    \begin{lstlisting}[style=JavaStyle]
        some_String_int_thing = "Somestring"
    \end{lstlisting}

    R
    \begin{lstlisting}[style=RStyle]
        some_data_csv_thing = 'Somestring'
    \end{lstlisting}

\end{document}

Best Answer

R syntax highlighting is defined in lstlang3.sty. The definition for R in that file contains the following:

 otherkeywords={!,!=,~,$,*,\&,\%/\%,\%*\%,\%\%,<-,<<-,_,/},
 alsoother={._$},

If you want to remove the special treatment of _ then add something like this to your own definition of RStyle

 otherkeywords={!,!=,~,$,*,\&,\%/\%,\%*\%,\%\%,<-,<<-,/},
 alsoother={.$},

(that is, remove the underscores). Besure to get the commas right at the end of the lines.