Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

Umlaute

share|improve this question

3 Answers 3

up vote 10 down vote accepted

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

share|improve this answer

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.

share|improve this answer

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}

enter image description here

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.