A possible manual approach is with the use of moredelim=**[is][\bfseries]{@}{@},
Can I also suggest a style? Below I have defined \lstdefinelanguage
customRthree
in conjunction with package courier
. You can also try beramono
. See for instance, Latex listings R: need mono-spaced font with bold keywords and correct caret (^).
\lstdefinelanguage{customRthree}{
keywords={t},
otherkeywords={<<-,\%*\%}, %% cannot add <-
keywordstyle=\bfseries,
moredelim=**[is][\bfseries]{@}{@}, % every character inside @ @ will be in bold face
basicstyle=\ttfamily,
commentstyle=\textsl,
}
Code
\documentclass[preview,border=5,convert]{standalone}
\usepackage{courier}
\usepackage{listings}
\lstdefinelanguage{customR}{ % defined a new R entry
keywords={t},
otherkeywords={<<-,\%*\%}, %% cannot add <-
keywordstyle=\bfseries,
moredelim=**[is][\bfseries]{@}{@},
}
\lstdefinelanguage{customRtwo}{
otherkeywords={<-,<<-,\%*\%}, %% cannot add <-
keywordstyle=\bfseries,
moredelim=**[is][\bfseries]{@}{@},
}
\lstdefinelanguage{customRthree}{
keywords={t,R},
otherkeywords={<<-,\%*\%}, %% cannot add <-
keywordstyle=\bfseries,
moredelim=**[is][\bfseries]{@}{@},
basicstyle=\ttfamily,
commentstyle=\textsl,
}
\begin{document}
\lstset{language=R,caption={Standard R}}
\begin{lstlisting}[frame=tb]
S <- H %*% t(pprd) %*% t(H) + R
xest <<- xprd + kalmangain %*% (z - H %*% xprd)
}
\end{lstlisting}
\lstset{language=customR,caption={Custom R}}
\begin{lstlisting}[frame=tb]
S @<-@ H %*% t(pprd) %*% t(H) + R
xest @<<-@ xprd + kalmangain %*% (z - H %*% xprd)
}
\end{lstlisting}
\lstset{language=customRtwo,caption={Custom R (alt.)}}
\begin{lstlisting}[frame=tb]
S <- H %*% t(pprd) %*% t(H) + R
xest @<@<- xprd + kalmangain %*% (z - H %*% xprd)
}
\end{lstlisting}
\begin{lstlisting}[frame=tb,language=customRthree,caption={Custom R 3}]
S @<-@ H %*% t(pprd) %*% t(H) + R
xest @<<-@ xprd + kalmangain %*% (z - H %*% xprd)
}
\end{lstlisting}
\end{document}
Output
Best Answer
R syntax highlighting is defined in
lstlang3.sty
. The definition for R in that file contains the following:If you want to remove the special treatment of
_
then add something like this to your own definition ofRStyle
(that is, remove the underscores). Besure to get the commas right at the end of the lines.