# Inconsistent output for R language operators when using listings package

A referee noticed that the <<- (assignment to a global) and %*% (matrix mult.) operators in a code listing I used were wrong.

I am using the standard listings package (version 1.4 dated 2007/02/22, from texlive 20120611 on Ubuntu 12.10). An example is below. In both cases the second use of < or % seems to use the correct font, the first one does not.

These operators are defined (in file lstlang3.sty) as

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


In this short example, I even define a simple custom language definition. By defining %*% as an otherkeyword I have half the problem solved -- but I cannot fix <<- and <- at the same time: once <- is added it match inside of <<-. This seems like a bug. Any TeXwizards who can chase it?

Code:

\documentclass[12pt]{article}
\usepackage{listings}
\lstdefinelanguage{customR}{    % defined a new R entry
keywords={t},
keywordstyle=\bfseries
}
\lstdefinelanguage{customRtwo}{
keywordstyle=\bfseries
}

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

\end{document}


Output (rendered to png here)

In summary:

• In the default, both <<- and %*% are wrong
• I managed to fix <<- and %*% but ...
• ... I cannot seem to fix <<- and <-

Any help would be greatly appreciated.

Edit: The closing } is of course spurious, I initially had a longer example and forgot to remove those.

-

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},
keywordstyle=\bfseries,
moredelim=**[is][\bfseries]{@}{@}, % every character inside @ @ will be in bold face
basicstyle=\ttfamily,
}


## Code

\documentclass[preview,border=5,convert]{standalone}

\usepackage{courier}

\usepackage{listings}
\lstdefinelanguage{customR}{    % defined a new R entry
keywords={t},
keywordstyle=\bfseries,
moredelim=**[is][\bfseries]{@}{@},
}
\lstdefinelanguage{customRtwo}{
keywordstyle=\bfseries,
moredelim=**[is][\bfseries]{@}{@},
}

\lstdefinelanguage{customRthree}{
keywords={t,R},
keywordstyle=\bfseries,
moredelim=**[is][\bfseries]{@}{@},
basicstyle=\ttfamily,
}

\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

-
You rock, that looks very promising and I will take a good look. –  Dirk Eddelbuettel Jan 8 '13 at 12:07
Glad to help. BTW, I saw from your blog that you deal with R a lot. Have you tried Sweave and SweaveListingUtils? Another possibility is the minted package: \begin{minted}{r}...\end{minted} –  hpesoj626 Jan 8 '13 at 13:49
Yes, but I sometimes also like to use plain latex for faster processing. I see that the real fix you bring here is the switch to the typewriter font. I was still wrestling with this a little before work/on the commute but now at work. Can you hit my email and we try a few more things off-line (and then report back here?) –  Dirk Eddelbuettel Jan 8 '13 at 16:17
Sorry. Just saw your comment. I am on my way to work. Perhaps you can post a comment about which part you are still wrestling with and we'll see from there. Other site members may also be able to suggest further solutions if they happen upon it. Better yet, you can ask it as another question if it concerns another but related matter. –  hpesoj626 Jan 8 '13 at 22:30
Your listing four solves the issue of the mismatch between the % and < by switching to \ttfamily. Ideally, I'd retain the original font but maybe one just cannot. –  Dirk Eddelbuettel Jan 9 '13 at 4:56