我正在使用包listings
。我在 LaTeX 中编写了一些 R 代码。我按照@Alan Munn 的建议使用showstringspaces=false
\documentclass{article}
\usepackage[svgnames]{graphicx}
\usepackage{listings}
\lstset{language=R,
basicstyle=\fontsize{9}{10}\selectfont\ttfamily,
stringstyle=\color{DarkGreen},
otherkeywords={0,1,2,3,4,5,6,7,8,9},
morekeywords={TRUE,FALSE},
deletekeywords={data,frame,length,as,character},
keywordstyle=\color{blue},
commentstyle=\color{DarkGreen}
showstringspaces=false
}
\begin{document}
\begin{lstlisting}
#Bar plot Top U.S. imports from Canada
us_canada_import <- data.frame(Activities=c('Mineral Fuels',
'Vehicles','Machinery','Special Other (returns)',
'Plastics', 'Rest of Economic Activities'),
Revenue=c(85,53,23,16,12,129.8))
us_canada_import_chart <- us_canada_import %>%
group_by(Activities) %>%
summarize(sum_revenue = sum(Revenue)) %>%
mutate(percent = sum_revenue/sum(sum_revenue), cum_sum = cumsum(sum_revenue)) %>%
arrange(sum_revenue) %>%
ggplot(aes(x='U.S. imports from Canada', y=sum_revenue, fill=Activities))+
geom_bar(stat='identity') +
geom_text(aes(label=paste0(sprintf("%1.1f", percent*100),"%")),
position=position_stack(vjust=0.5), colour="white") +
scale_fill_brewer(palette="Dark2")+
ggtitle("Segmentation U.S. top imports from Canada 2018") +
xlab("") +
ylab("Revenue in billions of U.S. dollars")
\end{lstlisting}
\end{document}
但是,每次我写一个带有空格的字符串时,符号
而不是空格。但是,当我写注释时,符号
出现。
如何使用空格添加注释和字符串而不出现这些问题?
答案1
有一个listings
名为的显示选项showstringspaces
。将其设置为false
,空格就会按照您希望的方式显示。
\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{listings}
\lstset{language=R,
basicstyle=\fontsize{9}{10}\selectfont\ttfamily,
stringstyle=\color{DarkGreen},
otherkeywords={0,1,2,3,4,5,6,7,8,9},
morekeywords={TRUE,FALSE},
deletekeywords={data,frame,length,as,character},
keywordstyle=\color{blue},
commentstyle=\color{DarkGreen},
showstringspaces=false
}
\begin{document}
\begin{lstlisting}
us_canada_import <- data.frame(Activities=c('Mineral Fuels',
'Vehicles','Machinery','Special Other (returns)',
'Plastics', 'Rest of Economic Activities'),
Revenue=c(85,53,23,16,12,129.8))
us_canada_import_chart <- us_canada_import %>%
group_by(Activities) %>%
summarize(sum_revenue = sum(Revenue)) %>%
mutate(percent = sum_revenue/sum(sum_revenue), cum_sum = cumsum(sum_revenue)) %>%
arrange(sum_revenue) %>%
ggplot(aes(x='U.S. imports from Canada', y=sum_revenue, fill=Activities))+
geom_bar(stat='identity') +
geom_text(aes(label=paste0(sprintf("%1.1f", percent*100),"%")),
position=position_stack(vjust=0.5), colour="white") +
scale_fill_brewer(palette="Dark2")+
ggtitle("Segmentation U.S. top imports from Canada 2018") +
xlab("") +
ylab("Revenue in billions of U.S. dollars")
\end{lstlisting}
\end{document}