Sign up ×
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.

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:

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))

And here is the LaTeX code:






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.

share|improve this question

migrated from Feb 3 '12 at 20:26

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

1 Answer 1

up vote 7 down vote accepted

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.

share|improve this answer

Your Answer


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.