Listings in Latex with UTF-8 (or at least german umlauts)

Viewed 69758

Trying to include a source-file into my latex document using the listings package, i got problems with german umlauts inside of the comments in the code. Using

\lstset{
extendedchars=\true,
inputencoding=utf8x
}

Umlauts in the source files (encoded in UTF-8 without BOM) are processed, but they are somehow moved to the beginning of the word they are contained in. So

// die Größe muss berücksichtigt werden

in the input source file, becomes

// die ößGre muss übercksichtigt werden

in the output file.

NOTE: since i found errors in my initial setup, i heavily edited this question

24 Answers

ok, found kinda workaround now:

  1. instead of listings package, use listingsutf8

    \usepackage{listingsutf8}

  2. copy listings.sty to the folder the document resides

  3. find the following lines

    \lst@CCPutMacro
        \lst@ProcessOther {"23}\#
        \lst@ProcessLetter{"24}\textdollar
        \lst@ProcessOther {"25}\%
        \lst@ProcessOther {"26}\&
  4. Enter there the following lines (each "registers" one umlaut)

    \lst@ProcessLetter{"E4}{\"a}
    \lst@ProcessLetter{"F6}{\"o}
    \lst@ProcessLetter{"FC}{\"u}
    \lst@ProcessLetter{"C4}{\"A}
    \lst@ProcessLetter{"D6}{\"O}
    \lst@ProcessLetter{"DC}{\"U}
    \lst@ProcessLetter{"DF}{\ss{}}
  5. Save the file

  6. Use

    \lstset{
        extendedchars=\true,
        inputencoding=utf8/latin1
    }

to enable utf8 character to latin1 character mapping

  1. Convert line endings of your source file from windows (\r\n) to unix (\n)
  2. enjoy

I know this is ugly in many way, but its the only solution that works for me so far.

For Polish:

\lstset{%
literate=%
 {ą}{{\k{a}}}1
 {ę}{{\k{e}}}1
 {Ą}{{\k{A}}}1
 {Ę}{{\k{E}}}1
 {ś}{{\'{s}}}1
 {Ś}{{\'{S}}}1
 {ź}{{\'{z}}}1
 {Ź}{{\'{Z}}}1
 {ń}{{\'{n}}}1
 {Ń}{{\'{N}}}1
 {ć}{{\'{c}}}1
 {Ć}{{\'{C}}}1
 {ó}{{\'{o}}}1
 {Ó}{{\'{O}}}1
 {ż}{{\.{z}}}1
 {Ż}{{\.{Z}}}1
 {ł}{{\l{}}}1
 {Ł}{{\l{}}}1
}

And here's list of symbols so you can create it for your language: https://en.wikibooks.org/wiki/LaTeX/Special_Characters

I can confirm that solution provided by Janosch works almost correctly.

In my case I needed to use Spanish characters: á,é,í,ó,ú,ñ,Á,É,Í,Ó,Ú and tried his solution without results until I compiled my file with xelatex instead of pdflatex.

Anyway you should modify listings.sty either in your local copy or directly in the common file and add the following:

\lst@CCPutMacro
    \lst@ProcessOther {"23}\#
    \lst@ProcessLetter{"24}\textdollar
    \lst@ProcessOther {"25}\%
    \lst@ProcessOther {"26}\&
        %spanish letters coded in UTF
    \lst@ProcessOther {"E1}{\'a}
    \lst@ProcessOther {"C1}{\'A}
    \lst@ProcessOther {"E9}{\'e}
    \lst@ProcessOther {"C9}{\'E}
    \lst@ProcessOther {"ED}{\'i}
    \lst@ProcessOther {"CD}{\'I}
    \lst@ProcessOther {"F3}{\'o}
    \lst@ProcessOther {"D3}{\'O}
    \lst@ProcessOther {"FA}{\'u}
    \lst@ProcessOther {"DA}{\'U}
    \lst@ProcessOther {"F1}{ñ}
    \lst@ProcessOther {"D1}{Ñ}

In my .tex file I used the following options for listings:

\usepackage{listingsutf8}
\lstset{
        inputencoding=utf8,
        extendedchars=\true}

Hope it can help anybody and maybe we can construct a listing.sty containing almost every UTF8 char... :)

My contribution for spanish language.

Tested, it works great, both importing files with the code with spanish characters, and pasting snippets of code directly in the latex document.

\usepackage[spanish, activeacute]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listings}

\lstset{literate=%
    {ñ}{{\~n}}1
    {Ñ}{{\~N}}1
    {á}{{\'a}}1
    {é}{{\'e}}1
    {í}{{\'i}}1
    {ó}{{\'o}}1
    {ú}{{\'u}}1
    {Á}{{\'A}}1
    {É}{{\'E}}1
    {Í}{{\'I}}1
    {Ó}{{\'O}}1
    {Ú}{{\'U}}1
}

Simply don't use UTF-8 in LaTex if you want to use any listings packages. Using latin1 in your documents will display German Umlauts just fine.

I'm writing my thesis in German using this setup:

% Your language, here German
\usepackage[ngerman]{babel} 
% Will work with Umlauts
\usepackage[latin1]{inputenc}
% Euro characters etc.
\usepackage{textcomp}
% Works perfectly with latin1
\usepackage{listings}

To add copyright support to listings, add this:

\lstset{
    %you may add line below to existed config
    literate={©}{{\copyright}}{4}
}

You might need to set the document's input encoding to also be UTF-8. There's a comp.text.tex thread discussing how to do this. The following accomplishes this in TeXLive on GNU/Linux:

\usepackage[utf8]{inputenc}
Related