Typesetting UTF8 listings with German Umlaute

I'm having some issues with listings that include German Umlaute. The following example code generates a completely messed up PDF.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[]{listings}
\begin{document}
\begin{lstlisting}[language={[LaTeX]TeX},inputencoding={utf8},extendedchars=false]
Staatsangehörigkeit
\end{lstlisting}
\end{document}


Does anyone have an idea how to fix this?

-

the normal listings package doesn't provide unicode support. I use the following code to work around the Problem

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

\lstset{literate=%
{Ö}{{\"O}}1
{Ä}{{\"A}}1
{Ü}{{\"U}}1
{ß}{{\ss}}1
{ü}{{\"u}}1
{ä}{{\"a}}1
{ö}{{\"o}}1
{~}{{\textasciitilde}}1
}
\begin{document}
\begin{lstlisting}[language={[LaTeX]TeX}]
Staatsangehörigkeit
\end{lstlisting}
\end{document}


I also tried listingsutf8 and listings2 which ist BETA, but for me the literate solution just works perfekt ;)

edit:sry, marco has the same solution earlier...

-

An alternative solution to that provided by marco, perhaps a touch less typing, by using the key escapeinside:

\documentclass{article}
\usepackage[ngerman,english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[]{listings}
\begin{document}
\begin{lstlisting}[language={[LaTeX]TeX},
inputencoding={utf8}, extendedchars=false,
escapeinside=]
Staatsangehörigkeit
\end{lstlisting}
\end{document}


In general utf8 and listings don't play well, especially if you importing files generated from different programs. The backticks, just enable whatever is enclosed between them to be escaped to LaTeX.

-

I use the following method in my code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[]{listings}
\lstset{literate={ö}{{\"o}}1}
\begin{document}
\begin{lstlisting}[language={[LaTeX]TeX},inputencoding={utf8},extendedchars=false]
Staatsangehörigkeit
\end{lstlisting}
\end{document}


Ulrike Fischer wrote in the German community mrunix:

Weil listings eine Verbatim-Umgebung ist, die Befehle wie \section, \bfseries und eben auch \"a nicht ausführt, sondern einfach ausdruckt.

English try: Because listings is a verbatim environment, commands like \section, \bfseries and also \"a aren't executed, but instead just printed.

-