Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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},
  otherkeywords={<<-,\%*\%},    %% cannot add <-
  keywordstyle=\bfseries
}
\lstdefinelanguage{customRtwo}{
  otherkeywords={<-,<<-,\%*\%},  %% cannot add <-
  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)

enter image description 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.

share|improve this question

1 Answer 1

up vote 8 down vote accepted

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

enter image description here

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.