# Using listings for R code with ## in rangeprefix

In R, comments are preceded by the symbol #. Moreover, to avoid messing with the indentation, it's usually ## instead of #. Therefore, as I want to include some R code in a LaTeX document, I am using the listings package with these instructions. However, this gives me the following error:

Illegal parameter number in definition of \lst@rangebegin


Here is the R code:

##--GOLL0----
GetObsLogLik <- function(data, w, mu, sigma){
N <- nrow(data)
S <- ncol(data)
K <- nrow(mu)
sum(sapply(1:N, function(i){
log(sum(sapply(1:K, function(k){
w[k] * dmvnorm(x=data[i,], mean=mu[k,],
sigma=sigma[k]^2 * diag(S))
})))
}))
}
##--GOLL1----


And here is the LaTeX code:

\documentclass{article}

\usepackage{listings}
\lstset{language=R,
frame=single,
basicstyle=\small,
rangeprefix=##--,
rangesuffix=----,
includerangemarker=false}

\begin{document}

\lstinputlisting[linerange=GOLL0-GOLL1]{test.R}

\end{document}


The error disappears when I replace ## by //. But this is not a solution as // are not valid in R. I also tried the solution provided here but it doesn't work either.

-

## migrated from stackoverflow.comFeb 3 '12 at 20:26

This question came from our site for professional and enthusiast programmers.

The problem isn’t the R source code, it’s the fact that you are using special characters (#) in the \lstset options. Try using \#\# instead.