I'm writing a R code in LaTeX using the listing
package. Everything works fine in the first two lines, with the code written in plain text and the comments in italic. However, after this string quote="\""
, everything goes in plain text; I observed that this is caused by the double quotation marks (latex thinks one quotation is left open, if this makes sense). However this is part of the code and cannot be changed. Any possible solutions?
Here is the code and the output, notice the change in the comments (lines with the #)
#pacchetto per realizzazione di raster plots
library(raster)
#apertura e visualizzazione dati
data<-read.table(|\color{DarkGreen}"percorso/nomefile.txt"|, quote="\"", comment.char="")
View(data)
#creazione della matrice di raster
x<-as.matrix(data)
r<-raster(x)
EDIT:
here you have the full code, ready to get used in latex
\documentclass[a4paper]{report}
\usepackage[svgnames]{xcolor} % colorare testo
\usepackage[T1]{fontenc} % codifica dei font in uscita
\usepackage[utf8]{inputenc} % lettere accentate da tastiera
\usepackage[italian]{babel} % lingua principale del documento
\usepackage{lipsum} % genera testo fittizio
\usepackage{url} % per scrivere gli indirizzi Internet
\usepackage{packages} % carica il pacchetto
\usepackage{siunitx} % pacchetto per le tabelle
\usepackage{amsmath}
\usepackage{hyperref} % inserisce collegamenti ipertestuali
%\usepackage{geometry}
\usepackage[autostyle,italian=guillemets]{csquotes}
\usepackage{fancyhdr} % intestazioni e piè di pagina
\usepackage{listings} % inserire codici di programmazione
\lstset{language=R,
basicstyle=\small\ttfamily,
breaklines=TRUE,
showspaces=FALSE,
showstringspaces=false,
escapechar={|}
}
\begin{document}
\section{Matrice ASCII}
\begin{lstlisting}[language=R]
#pacchetto per realizzazione di raster plots
library(raster)
#apertura e visualizzazione dati
data<-read.table(|\color{DarkGreen}"percorso/nomefile.txt"|, quote="\"", comment.char="")
View(data)
#creazione della matrice di raster
x<-as.matrix(data)
r<-raster(x)
#clamping dei dati al valore minimo e massimo piu' probabili, per migliorare la visualizzazione
rr<-clamp(r,|\color{blue}0,100|)
#parametrizzazione della matrice raster
xmin=0
xmax=255
ymin=0
ymax=255
extent(rr)<-extent(xmin,xmax,ymin,ymax)
#plot del raster; e' possibile modificare la scala di colori agendo sulla variabile col
plot(rr, |\color{blue}col=hcl.colors(200,"plasma")|,xlab="X",ylab="Y",xlim=c(0,255),ylim=c(0,255))
#salvataggio del plot; e' possibile modificare la scala di colori agendo sulla variabile col
png("raster.png",width=540,height=540,units="px")
plot(rr, |\color{blue}col=hcl.colors(200,"plasma"|),xlab="X",ylab="Y",xlim=c(0,255),ylim=c(0,255))
dev.off()
\end{lstlisting}
\end{document}