1

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)

output text

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}

1
  • 4
    This is not enough to see what you are doing. Please include a complete LaTeX source file, enough to run and see the output. As it stands, at most wild guesses are possible. And few people will be motivated enough to fill in the missing stuff.
    – vonbrand
    Aug 30, 2021 at 17:37

1 Answer 1

1

Not 100% sure why it be so, but the first quotation mark of the final two here was causing it:

quote="\""

The solution is to use instead:

quote="|\textbackslash"|"

Which produces this:

enter image description here

Code is here:

\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{geometry}
\usepackage[autostyle,italian=guillemets]{csquotes}
\usepackage{fancyhdr} % intestazioni e piè di pagina
\usepackage{listings} % inserire codici di programmazione
\usepackage{hyperref} % inserisce collegamenti ipertestuali

\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="|\textbackslash"|", 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}

In the future, please always include an minimal working example and further to that, please include packages as .sty files (I have no idea what \usepackage{packages} is doing nor its effect on my answer or your code for example). If my or any answer answers your question please consider ticking it as complete just next to the answer score arrows, hope this helps.

2
  • 1
    This worked perfectly, thank you! And sorry for not posting a full resource at first, it's my first quesion in this stackexchange so I didn't know exactly what was needed. Aug 30, 2021 at 22:57
  • You're welcome and do not worry, it just helps us help you as quickly as possible, glad I could help :)
    – user245306
    Aug 30, 2021 at 23:00

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