0

I use the package listings to include R-codes in my project.

Unfortunately, it produces (sometimes, but not always) a hanging indent in the pdf document.

Is there a possibility to erase this hanging indent (I want only justification in my print, see also a more conrete example what I meant below the code)?

Here is an extract of my LaTeX file using the listing package:

\begin{lstlisting}[language=R]
#read.in is a function which reads in the picture 
#as 620x872X3 matrix 
#(#pixels in height x #pixels in width x #channels)
#RGB channels are used for reading in .png picture

read.in<-function(pic){
  require("png")
  datpng <-img <- readPNG(system.file("img", pic , package="png"))
  dat<-apply(datpng, c(1,2), sum)


  #define grid with 10 x 14 boxes on the picture, 
    #which used later in the algorithm
  nrow<-10
  ncol<-14
  sizer<-dim(dat)[2]/nrow
  sizec<-dim(dat)[1]/ncol

  #arr is array containig precisely the boxes 
    #with handwritten numbers as subarrays a[,,k], 
  #k ranges from 1 to 10*14 
    #= number of boxes containing precisely one digits
  arr<- array(0,c(sizec, sizer, nrow*ncol))
  k<- 1

  for (i in 1:ncol){
    for (j in 1:nrow){
      arr[,,k]<-dat[(i-1)*sizec+1:sizec,(j-1)*sizer + 1:sizer]
      k<-k+1
    }
  }


  #Reshape 167x165x140-array arr into the 27555x140-array arra, 
  #hence arra[,k] are exactly the data corresponding to one scanned digit,
    #where k ranges from 1 to 140=#boxes.
  arra<-arr
  dim(arra)<-c(prod(dim(arr)[1:2]), dim(arr)[3])
  arra<-t(arra)


  #generate data frame for classification 
  classes<-c(rep(0:9, each=10),0:9, 0:9, 0:9, 0:9)
  ar<-data.frame(nr=factor(c(classes)), arra)

  return(ar)
}
\end{lstlisting}

We see that in the line starting with #define grid... and the following line starting with #which... there is a hanging indent, but I want actually that it looks like (in the sense of indent) the line starting with require("png")... and datpng..... Here we see that there is no haning indent, but there is only justification used.

  • Your code snippet doesn't replicate a "hanging indent". Could you elaborate what you mean? – Werner Dec 24 '16 at 7:14
  • @Werner Sorry I did only give an extract of my code, which may not replicate a haning indent. I edit my post with my whole code and now one sees that e.g. in the line starting with "#width...." there is a haning indent. I just want something as in the first 8 lines of my code. – bjn Dec 24 '16 at 7:19
  • Firstly, there is no line #width, only #with, #which and #where... Secondly, the indentation provided is what is replicated by listings. It's not done on purpose, it's because you supplied that input. Could you provide a picture of the current output and then also what the desired output should look like (even if you have to replicate it in another environment)? – Werner Dec 24 '16 at 7:22
  • I'm sorry I meant #with. See the edit in my post. – bjn Dec 24 '16 at 7:44

Your Answer

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

Browse other questions tagged or ask your own question.