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:

enter image description 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..


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


public class Main
    public Main(String args[])
        // Java logic
  • I think the literate and the extendedchars options were more important. Also, it's much better to post your solution as an answer rather than to edit your question. – MERose Jan 11 '19 at 14:08

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.

| improve this answer | |
  • 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
  • 1
    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

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.