# Problem with special characters in listings

I'm having problems with my TeX document. I'm trying to import my java file in listings to get syntax colouring and stuff. My java files contains comments in Danish, which means they contain special characters (æ, ø and å). I managed to view those by adding \lstset{inputencoding=utf8, extendchars=\true} to my premable. even though I can see the characters in my document, it still isn't right. The syntax colouring isn't applied to the special characters and they aren't in the right place in the words. You can see an example here:

It's supposed to say "...du fortsætte med at åbne programmet?..."

It's like the special characters jump to the beginning of the word and then the syntax colouring isn't applied.

Hope someone is able to help.. Thanks..

EDIT

I got it working by using ansinew charset in stead of using UTF8 as inputenc. This is exactly how I did:


\documentclass[a4paper,oneside]{memoir}
\usepackage{ucs}
\usepackage[utf8]{inputenc}
\usepackage[danish]{babel}
\usepackage[T1]{fontenc}
\usepackage{listings}
\lstset{literate=%
{æ}{{\ae}}1
{å}{{\aa}}1
{ø}{{\o}}1
{Æ}{{\AE}}1
{Å}{{\AA}}1
{Ø}{{\O}}1
}
\lstset{extendedchars=\true}
\lstset{inputencoding=ansinew}

\begin{document}
\begin{lstlisting}
public class Main
{
public Main(String args[])
{
// Java logic
}
}
\end{lstlisting}
\end{document}


-
 welcome to TeX.SX! I inserted the linked external example image directly into your question. – Stefan Kottwitz♦ Apr 24 '11 at 13:51 Thanks - wan't able to myself due to low reputation score (as I'm completely new on the Tex site) – Simon Apr 24 '11 at 13:53

This thread at stackoverflow should help. Assuming you can't convert the document away from UTF-8, the simplest method appears to be treating the UTF-8 characters as literate programming.

There's also a listingsutf8 package, but that will only work for external files pulled in through \lstinputlisting, not for code defined directly in the .tex file.

-
Well I'm using \lstinputlisting, but couldn't get that listingsutf8 working in any way.. The closest right now is using the literate thing on the stackoverflow page you sent me.. till having problems though.. I use the folowing in my premable: \lstset{literate=% {å}{{\aa}}{1} {ø}{{\o}}{1} } but now it replaces all "å"'s and "ø"'s characters with "ø" in stead.. – Simon Apr 24 '11 at 14:57
Does the minimal example in this comment work as advertised on your system? If so, then there's something else going on in your particular document, or maybe you missed a step or mis-transcribed something somewhere. If the MWE given works, but yours document doesn't, then cutting your document down to the minimum code to show the error will help narrow down the problem. – Mike Renfro Apr 24 '11 at 15:09
When making an absolut minimum sample, I get the folowing error: Package inputenc Error: Unicode char \u8:�\expandafter not set up for use with LaTeX. My codeexample as folows: pastebin.com/PzraSTG2 – Simon Apr 24 '11 at 15:36
Oh, and yes - the minimal example you posted did work on my system.. – Simon Apr 24 '11 at 17:06
Can you make a minimal working example of what succeeded, and edit your post accordingly? Just the minimum amount of preamble and listings code to show that it works. Since I didn't post a complete working solution myself. – Mike Renfro Apr 25 '11 at 14:14
show 1 more comment