views:

3537

answers:

8

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

A: 

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}
Michael E
My main document is in utf8. (and it works, i can even write äöü in the main document)
Janosch
listings does its character processing differently than the main document. So inputenc doesn't help, here; the listings packages needs to support utf8 input explicitly (hence listingsutf8).
Will Robertson
+3  A: 
Janosch
I think, 'extendedchars=\true' is equal to 'extendedchars=false'.
Vanuan
A: 

Author of package suggests using texcl option. Sometimes it helps, sometimes doesn't.

Vanuan
+1  A: 

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}
Alain M. Lafon
A: 

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... :)

Jaboto
+1  A: 

So geht es (should work for other languages - spanish, danish as well)

Heimo Adelsberger ([email protected])

---SNIP--- \documentclass[ a4paper, %% defines the paper size: a4paper (default), a5paper, letterpaper, ... 12pt %% set default font size to 12 point ]{scrartcl} %% article, see KOMA documentation (scrguide.dvi)

\usepackage[utf8]{inputenc}

\usepackage[T1]{fontenc} \usepackage{listings}

\lstset{language=Pascal} \lstset{literate=% {Ö}{{\"O}}1 {Ä}{{\"A}}1 {Ü}{{\"U}}1 {ß}{{\ss}}2 {ü}{{\"u}}1 {ä}{{\"a}}1 {ö}{{\"o}}1 }

\begin{document}

[Latex: kann man Umlaute in lstlisting verwenden?] \begin{lstlisting} Test für Umlaut äöü ÄÖÜ ß So geht es \end{lstlisting}

\end{document}

Heimo Adelsberger
Thank you - it worked! The same for Polish language: \lstset{literate={ą}{{\k{a}}}1 {ł}{{\l{}}}1 {ń}{{\'n}}1 {ę}{{\k{e}}}1 {ś}{{\'s}}1 {ż}{{\.z}}1 {ó}{{\'o}}1 {ź}{{\'z}}1 {Ą}{{\k{A}}}1 {Ł}{{\L{}}}1 {Ń}{{\'N}}1 {Ę}{{\k{E}}}1 {Ś}{{\'S}}1 {Ż}{{\.Z}}1 {Ó}{{\'O}}1 {Ź}{{\'Z}}1 }
GDR
+3  A: 

I found a simpler approach, which works for me:

\usepackage{listings}

\lstset{
  literate={ö}{{\"o}}1
           {ä}{{\"a}}1
           {ü}{{\"u}}1
}
yaxu
Definitely the best and most straight-forward workaround for this problem.
Koraktor
A: 

Works fine for me!

My 2 cents for the French side:

\lst@ProcessOther {"C0}{\`{A}}
\lst@ProcessOther {"C1}{\'{A}}
\lst@ProcessOther {"C2}{\^{A}}
\lst@ProcessOther {"C4}{\"{A}}
\lst@ProcessOther {"C7}{\c{C}}
\lst@ProcessOther {"C8}{\`{E}}
\lst@ProcessOther {"C9}{\'{E}}
\lst@ProcessOther {"CA}{\^{E}}
\lst@ProcessOther {"CB}{\"{E}}
\lst@ProcessOther {"CE}{\^{I}}
\lst@ProcessOther {"CF}{\"{I}}
\lst@ProcessOther {"D4}{\^{O}}
\lst@ProcessOther {"D6}{\"{O}}
\lst@ProcessOther {"D9}{\`{U}}
\lst@ProcessOther {"DB}{\^{U}}
\lst@ProcessOther {"E0}{\`{a}}
\lst@ProcessOther {"E1}{\'{a}}
\lst@ProcessOther {"E2}{\^{a}}
\lst@ProcessOther {"E4}{\"{a}}
\lst@ProcessOther {"E7}{\c{c}}
\lst@ProcessOther {"E8}{\`{e}}
\lst@ProcessOther {"E9}{\'{e}}
\lst@ProcessOther {"EA}{\^{e}}
\lst@ProcessOther {"EB}{\"{e}}
\lst@ProcessOther {"EE}{\^{\i}}
\lst@ProcessOther {"EF}{\"{\i}}
\lst@ProcessOther {"F4}{\^{o}}
\lst@ProcessOther {"F6}{\"{o}}
\lst@ProcessOther {"F9}{\`{u}}
\lst@ProcessOther {"FB}{\^{u}}
Phil