2

I am using the package listings. I was writing some code of R in LaTeX. I followed the suggestions from @Alan Munn by using 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}

However, every time I write a string with spaces, the symbol

enter image description here

appears instead of space. However, when I write a comment the symbol

enter image description here

appears instead.

enter image description here

How can I make comments and strings using space, but without getting these problems?

  • You're missing the comma to separate the showstringsspaces key from the previous keys. – Alan Munn Apr 29 at 15:42
2

There is a display option for listings called showstringspaces. Set this to false and the spaces will appear as you want them to.

\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}

enter image description here

| improve this answer | |
  • Thanks for the answer I used your suggestion. However, the problem persists, and a new issue appears. I edited my question to point out your suggestion and the new problems that appear. – Ivan Apr 29 at 14:28
  • I also updated listing, and listings package, but it seems this is not the issue. – Ivan Apr 29 at 14:29
  • 1
    @Ivan You're missing a comma before the showstringsspaces in your updated code. Perhaps that's the problem? – Alan Munn Apr 29 at 15:41
  • I apologize for the mistake. @Alan Munn, thanks for the feedback. – Ivan Apr 29 at 15:49

Your Answer

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

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