2

I am writing R codes in lstlisting:

\documentclass{article} 
\usepackage{listings}
\usepackage[swedish,english]{babel}
\usepackage{xcolor}
\usepackage{color}
\definecolor{ForestGreen}{rgb}{0.0, 0.5, 0.0}
\lstset{language=R,
basicstyle=\small\ttfamily,
stringstyle=\color{ForestGreen},
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{ForestGreen},
breaklines=true,
postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space},
}

\begin{document}
\begin{lstlisting}
   studentsNames = c("Björn","Hökan")
\end{lstlisting}
\end{document}

But, the latex returns an error and it cannot show the Swedish letters (ö, å, ä) in lstlisting. Is there any solution?

3
  • When I run your code, I get:" Package xcolor Error: Undefined color `ForestGreen'". When I remove the colour, the umlaute are printed fine.
    – FLonLon
    Aug 30, 2021 at 14:36
  • Ok right I have to add \definecolor{ForestGreen}{rgb}{0.0, 0.5, 0.0} to the code
    – Amin Kaveh
    Aug 30, 2021 at 14:38
  • This is probably a duplicate of tex.stackexchange.com/a/39645/1090 Aug 30, 2021 at 15:58

2 Answers 2

1

You can define some UTF-8 characters (encoded by two or more bytes) via the literate keyword.

\documentclass{article} 
\usepackage{listings}
\usepackage[swedish,english]{babel}
\usepackage{xcolor}
\usepackage{color}
\definecolor{ForestGreen}{rgb}{0.0, 0.5, 0.0}
\lstset{language=R,
basicstyle=\small\ttfamily,
stringstyle=\color{ForestGreen},
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{ForestGreen},
breaklines=true,
postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space},
}

\lstset{
  literate=
  {á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
  {Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
  {à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
  {À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
  {ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
  {Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
  {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
  {Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
  {œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
  {ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
  {€}{{\texteuro}}1 {£}{{\pounds}}1
  {«}{{<<}}1 {»}{{>>}}1
  {°}{{\up{o}}}1
}

\begin{document}
\begin{lstlisting}
   studentsNames = c("Björn","Hökan")
\end{lstlisting}
\end{document}
0

When I ran your code with XeLaTeX, it was fine, but in pdfLaTeX I got:

! Package inputenc Error: Invalid UTF-8 byte sequence.

The difference is that pdfLaTeX doesn't really understand UTF-8 and because lstlisting is parsing the input one character at a time, this is one of the places where the difference matters. The Unicode engines, XeLaTeX and LuaLaTeX understand that 0xC3 0xB6 is a single character, but pdfLaTeX will happily split it up and then get upset about the results.

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.