[Tex/LaTex] Unicode characters in lstlisting

listingsunicode

I need to include code in the appendix of my thesis. The code contains comments and the compilation instantly breaks returning:

Unicode char �\lst@FillFixed@ (U+5A8)
(inputenc)                not set up for use with LaTeX.
l.160       if (lastArg < 0, lastArg = 360° 
                                             + lastArg);

This is only one of many characters which produce problems. I cannot change my entire code just because this won't compile. Is there any way to include code with arbitrary unicode characters? Thank you.

Edit: complete sample code, as requested.

\RequirePackage[l2tabu,orthodox]{nag}


\documentclass[headsepline,footsepline,footinclude=false,oneside,fontsize=11pt,paper=a4,listof=totoc,bibliography=totoc]{scrbook} % one-sided


\usepackage[toc,page]{appendix}
\usepackage{listings}
\usepackage[utf8]{inputenc}

\lstset{
    inputencoding=utf8,
  extendedchars=true,
  basicstyle=\footnotesize\ttfamily,
  showstringspaces=false,
  showspaces=false,
  numbers=left,
  numberstyle=\footnotesize,
  numbersep=9pt,
  tabsize=2,
  breaklines=true,
  showtabs=false,
  captionpos=b,
    literate={ö}{{\"o}}1 {ä}{{\"a}}1 {ü}{{\"u}}1 
}

\begin{document}

\begin{appendices}
\lstinputlisting{code/power_series.html}
\end{appendices}

\end{document}

Relevant content of power_series.html:

if (lastArg < 0, lastArg = 360° + lastArg);

There really is not much to say about this problem. I only want to be able to use all unicode characters inside a listing. Thank you.

Best Answer

Define the Unicode character in the proper way.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listingsutf8}

\protected\def\dg{\ensuremath{^\circ}}
\DeclareUnicodeCharacter{05A8}{\dg}

\lstset{
    inputencoding=utf8,
  extendedchars=true,
  basicstyle=\footnotesize\ttfamily,
  showstringspaces=false,
  showspaces=false,
  numbers=left,
  numberstyle=\footnotesize,
  numbersep=9pt,
  tabsize=2,
  breaklines=true,
  showtabs=false,
  captionpos=b,
  literate={ö}{{\"o}}1 {ä}{{\"a}}1 {ü}{{\"u}}1 {°}{\dg}1
}

\begin{document}

\lstinputlisting{\jobname.html}

\end{document}

The \jobname.html file contains

if (lastArg < 0, lastArg = 360° + lastArg);

Here is the output.

enter image description here

Related Question