7

EDIT: The question is not really relevant for me any more, I decided to instead use the highlight package in R to export Code as LaTeX output. This still requires manual modifications, but at least I could get it to work. Maybe the question could still be kept open and "unanswered" in case anyone comes up with a solution.

I am using the listings package to include some R code into LaTeX. Now, some R functions like date() or start are highlighted (in blue). However, some of my variables, unfortunately include strings like date or start. I would like that only the functions are highlighted, but not the stuff in the variable names. For example in the following code, only the date in the first line should be highlighted

\documentclass{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{color}

\definecolor{lightgray}{gray}{0.95}
\definecolor{darkgray}{gray}{0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}


\usepackage{listings}
\lstset{ 
  language=R,                     % the language of the code
  basicstyle=\footnotesize,       % the size of the fonts that are used for the code
  numbers=none,                   % where to put the line-numbers
  backgroundcolor=\color{lightgray},  % choose the background color. You must add \usepackage{color}
  showspaces=false,               % show spaces adding particular underscores
  showstringspaces=false,         % underline spaces within strings
  showtabs=false,                 % show tabs within strings adding particular underscores
  tabsize=2,                      % sets default tabsize to 2 spaces
  breaklines=true,                % sets automatic line breaking
  keywordstyle=\color{blue},      % keyword style
  commentstyle=\color{darkgray},   % comment style
  stringstyle=\color{mauve},      % string literal style
} 

\begin{document}

\begin{lstlisting}[language=R]
a <- date(123)
date.a <- 123
a.start <- 456
a_start <- 456
\end{lstlisting}

\end{document} 

This is what the output looks like:

enter image description here

Is there any way to exclude the other apparences of date, or general apparences of start from the highlighting? I tried adding a deletekeywords={start} option to \lstset, but it does not change the output

In case there is no such option, I would also be happy to remove any highlighting of date or start at all, but the deletekeywords={start} option does not seem to do the job.

Further, I tried the command with which "." and "_" can be added to the list of letters, but that didn't change anything as well. Maybe some weird interaction. I found a completely other solution instead, and now use the highlight package directly in R. Not really an elegant solution, but at least I could get it to work

2

In listings' definition of R (in lstlang3.sty) we find:

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

You see that _ is both a keyword itself and has category code other. Also . has category code other. If we undo this, i.e., remove the keyword (which for keywords defined with otherkeywords apparently means we have to set the whole list anew but without the keyword we want removed) and assign both characters to be letters

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

then you get the desired output. With . and _ being letters listings won't consider start or date as functions in the variables names because it now considers the whole string date.a (including .) as one word:

\documentclass{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{color}

\definecolor{lightgray}{gray}{0.95}
\definecolor{darkgray}{gray}{0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}


\usepackage{listings}
\lstdefinestyle{mystyle}{
  language=R,                     % the language of the code
  basicstyle=\ttfamily,       % the size of the fonts that are used for the code
  numbers=none,                   % where to put the line-numbers
  backgroundcolor=\color{lightgray},  % choose the background color. You must
                                % add \usepackage{color}
  columns=fullflexible,
  showspaces=false,               % show spaces adding particular underscores
  showstringspaces=false,         % underline spaces within strings
  showtabs=false,                 % show tabs within strings adding particular underscores
  tabsize=2,                      % sets default tabsize to 2 spaces
  breaklines=true,                % sets automatic line breaking
  keywordstyle=\color{blue},      % keyword style
  commentstyle=\color{darkgray},   % comment style
  stringstyle=\color{mauve},      % string literal style
  alsoletter={._},
  otherkeywords={!,!=,~,$,*,\&,\%/\%,\%*\%,\%\%,<-,<<-,/}
} 

\begin{document}

\begin{lstlisting}[style=mystyle]
a <- date(123)
date.a <- 123
a.start <- 456
a_start <- 456
\end{lstlisting}

\end{document}

enter image description here

  • That works, thanks. Also much better than my "solution" of using the highlight package in R to export the code. In the meantime, I have gone on to using Sweave / knitr, which I thought was complicated to use, but turned out to be quite straightforward and produces nice results – user3825755 Jan 23 '17 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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